1 /* |
|
2 * Copyright (c) 1996, 2013, Oracle and/or its affiliates. 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. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * 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 import java.security.AccessController; |
|
32 import java.security.PrivilegedAction; |
|
33 |
|
34 /** |
|
35 * The WrappedSocket class provides a general wrapper for providing an |
|
36 * extended implementation of java.net.Socket that can be attached to |
|
37 * a pre-existing Socket object. WrappedSocket itself provides a |
|
38 * constructor for specifying alternate input or output streams to be |
|
39 * returned than those of the underlying Socket. |
|
40 */ |
|
41 class WrappedSocket extends Socket { |
|
42 |
|
43 /** the underlying concrete socket */ |
|
44 protected Socket socket; |
|
45 |
|
46 /** the input stream to return for socket */ |
|
47 protected InputStream in = null; |
|
48 |
|
49 /** the output stream to return for socket */ |
|
50 protected OutputStream out = null; |
|
51 |
|
52 /** |
|
53 * Layer on top of a pre-existing Socket object, and use specified |
|
54 * input and output streams. This allows the creator of the |
|
55 * underlying socket to peek at the beginning of the input with a |
|
56 * BufferedInputStream and determine which kind of socket |
|
57 * to create, without consuming the input. |
|
58 * @param socket the pre-existing socket to use |
|
59 * @param in the InputStream to return to users (can be null) |
|
60 * @param out the OutputStream to return to users (can be null) |
|
61 */ |
|
62 public WrappedSocket(Socket socket, InputStream in, OutputStream out) |
|
63 throws IOException |
|
64 { |
|
65 super((java.net.SocketImpl)null); // no underlying SocketImpl for this object |
|
66 this.socket = socket; |
|
67 this.in = in; |
|
68 this.out = out; |
|
69 } |
|
70 |
|
71 /** |
|
72 * Get the address to which the socket is connected. |
|
73 */ |
|
74 public InetAddress getInetAddress() |
|
75 { |
|
76 return socket.getInetAddress(); |
|
77 } |
|
78 |
|
79 /** |
|
80 * Get the local address to which the socket is bound. |
|
81 */ |
|
82 public InetAddress getLocalAddress() { |
|
83 return AccessController.doPrivileged( |
|
84 new PrivilegedAction<InetAddress>() { |
|
85 @Override |
|
86 public InetAddress run() { |
|
87 return socket.getLocalAddress(); |
|
88 |
|
89 } |
|
90 }); |
|
91 } |
|
92 |
|
93 /** |
|
94 * Get the remote port to which the socket is connected. |
|
95 */ |
|
96 public int getPort() |
|
97 { |
|
98 return socket.getPort(); |
|
99 } |
|
100 |
|
101 /** |
|
102 * Get the local port to which the socket is connected. |
|
103 */ |
|
104 public int getLocalPort() |
|
105 { |
|
106 return socket.getLocalPort(); |
|
107 } |
|
108 |
|
109 /** |
|
110 * Get an InputStream for this socket. |
|
111 */ |
|
112 public InputStream getInputStream() throws IOException |
|
113 { |
|
114 if (in == null) |
|
115 in = socket.getInputStream(); |
|
116 return in; |
|
117 } |
|
118 |
|
119 /** |
|
120 * Get an OutputStream for this socket. |
|
121 */ |
|
122 public OutputStream getOutputStream() throws IOException |
|
123 { |
|
124 if (out == null) |
|
125 out = socket.getOutputStream(); |
|
126 return out; |
|
127 } |
|
128 |
|
129 /** |
|
130 * Enable/disable TCP_NODELAY. |
|
131 */ |
|
132 public void setTcpNoDelay(boolean on) throws SocketException |
|
133 { |
|
134 socket.setTcpNoDelay(on); |
|
135 } |
|
136 |
|
137 /** |
|
138 * Retrieve whether TCP_NODELAY is enabled. |
|
139 */ |
|
140 public boolean getTcpNoDelay() throws SocketException |
|
141 { |
|
142 return socket.getTcpNoDelay(); |
|
143 } |
|
144 |
|
145 /** |
|
146 * Enable/disable SO_LINGER with the specified linger time. |
|
147 */ |
|
148 public void setSoLinger(boolean on, int val) throws SocketException |
|
149 { |
|
150 socket.setSoLinger(on, val); |
|
151 } |
|
152 |
|
153 /** |
|
154 * Retrive setting for SO_LINGER. |
|
155 */ |
|
156 public int getSoLinger() throws SocketException |
|
157 { |
|
158 return socket.getSoLinger(); |
|
159 } |
|
160 |
|
161 /** |
|
162 * Enable/disable SO_TIMEOUT with the specified timeout |
|
163 */ |
|
164 public synchronized void setSoTimeout(int timeout) throws SocketException |
|
165 { |
|
166 socket.setSoTimeout(timeout); |
|
167 } |
|
168 |
|
169 /** |
|
170 * Retrive setting for SO_TIMEOUT. |
|
171 */ |
|
172 public synchronized int getSoTimeout() throws SocketException |
|
173 { |
|
174 return socket.getSoTimeout(); |
|
175 } |
|
176 |
|
177 /** |
|
178 * Close the socket. |
|
179 */ |
|
180 public synchronized void close() throws IOException |
|
181 { |
|
182 socket.close(); |
|
183 } |
|
184 |
|
185 /** |
|
186 * Return string representation of the socket. |
|
187 */ |
|
188 public String toString() |
|
189 { |
|
190 return "Wrapped" + socket.toString(); |
|
191 } |
|
192 } |
|