J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1996-2003 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | package sun.rmi.transport.proxy; |
| 26 | |
| 27 | import java.io.*; |
| 28 | import java.net.InetAddress; |
| 29 | import java.net.Socket; |
| 30 | import java.net.SocketException; |
| 31 | |
| 32 | /** |
| 33 | * The WrappedSocket class provides a general wrapper for providing an |
| 34 | * extended implementation of java.net.Socket that can be attached to |
| 35 | * a pre-existing Socket object. WrappedSocket itself provides a |
| 36 | * constructor for specifying alternate input or output streams to be |
| 37 | * returned than those of the underlying Socket. |
| 38 | */ |
| 39 | class WrappedSocket extends Socket { |
| 40 | |
| 41 | /** the underlying concrete socket */ |
| 42 | protected Socket socket; |
| 43 | |
| 44 | /** the input stream to return for socket */ |
| 45 | protected InputStream in = null; |
| 46 | |
| 47 | /** the output stream to return for socket */ |
| 48 | protected OutputStream out = null; |
| 49 | |
| 50 | /** |
| 51 | * Layer on top of a pre-existing Socket object, and use specified |
| 52 | * input and output streams. This allows the creator of the |
| 53 | * underlying socket to peek at the beginning of the input with a |
| 54 | * BufferedInputStream and determine which kind of socket |
| 55 | * to create, without consuming the input. |
| 56 | * @param socket the pre-existing socket to use |
| 57 | * @param in the InputStream to return to users (can be null) |
| 58 | * @param out the OutputStream to return to users (can be null) |
| 59 | */ |
| 60 | public WrappedSocket(Socket socket, InputStream in, OutputStream out) |
| 61 | throws IOException |
| 62 | { |
| 63 | super((java.net.SocketImpl)null); // no underlying SocketImpl for this object |
| 64 | this.socket = socket; |
| 65 | this.in = in; |
| 66 | this.out = out; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Get the address to which the socket is connected. |
| 71 | */ |
| 72 | public InetAddress getInetAddress() |
| 73 | { |
| 74 | return socket.getInetAddress(); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Get the local address to which the socket is bound. |
| 79 | */ |
| 80 | public InetAddress getLocalAddress() { |
| 81 | return socket.getLocalAddress(); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Get the remote port to which the socket is connected. |
| 86 | */ |
| 87 | public int getPort() |
| 88 | { |
| 89 | return socket.getPort(); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Get the local port to which the socket is connected. |
| 94 | */ |
| 95 | public int getLocalPort() |
| 96 | { |
| 97 | return socket.getLocalPort(); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Get an InputStream for this socket. |
| 102 | */ |
| 103 | public InputStream getInputStream() throws IOException |
| 104 | { |
| 105 | if (in == null) |
| 106 | in = socket.getInputStream(); |
| 107 | return in; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Get an OutputStream for this socket. |
| 112 | */ |
| 113 | public OutputStream getOutputStream() throws IOException |
| 114 | { |
| 115 | if (out == null) |
| 116 | out = socket.getOutputStream(); |
| 117 | return out; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Enable/disable TCP_NODELAY. |
| 122 | */ |
| 123 | public void setTcpNoDelay(boolean on) throws SocketException |
| 124 | { |
| 125 | socket.setTcpNoDelay(on); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Retrieve whether TCP_NODELAY is enabled. |
| 130 | */ |
| 131 | public boolean getTcpNoDelay() throws SocketException |
| 132 | { |
| 133 | return socket.getTcpNoDelay(); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Enable/disable SO_LINGER with the specified linger time. |
| 138 | */ |
| 139 | public void setSoLinger(boolean on, int val) throws SocketException |
| 140 | { |
| 141 | socket.setSoLinger(on, val); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Retrive setting for SO_LINGER. |
| 146 | */ |
| 147 | public int getSoLinger() throws SocketException |
| 148 | { |
| 149 | return socket.getSoLinger(); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Enable/disable SO_TIMEOUT with the specified timeout |
| 154 | */ |
| 155 | public synchronized void setSoTimeout(int timeout) throws SocketException |
| 156 | { |
| 157 | socket.setSoTimeout(timeout); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Retrive setting for SO_TIMEOUT. |
| 162 | */ |
| 163 | public synchronized int getSoTimeout() throws SocketException |
| 164 | { |
| 165 | return socket.getSoTimeout(); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Close the socket. |
| 170 | */ |
| 171 | public synchronized void close() throws IOException |
| 172 | { |
| 173 | socket.close(); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Return string representation of the socket. |
| 178 | */ |
| 179 | public String toString() |
| 180 | { |
| 181 | return "Wrapped" + socket.toString(); |
| 182 | } |
| 183 | } |