author | ohair |
Wed, 06 Apr 2011 22:06:11 -0700 | |
changeset 9035 | 1255eb81cc2f |
parent 8185 | 6bf10e071117 |
child 11019 | aa969c520286 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
9035
1255eb81cc2f
7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents:
8185
diff
changeset
|
2 |
* Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
package java.net; |
|
26 |
||
27 |
import java.io.*; |
|
28 |
import java.security.PrivilegedAction; |
|
29 |
||
30 |
/* |
|
31 |
* This class PlainSocketImpl simply delegates to the appropriate real |
|
32 |
* SocketImpl. We do this because PlainSocketImpl is already extended |
|
33 |
* by SocksSocketImpl. |
|
34 |
* <p> |
|
35 |
* There are two possibilities for the real SocketImpl, |
|
36 |
* TwoStacksPlainSocketImpl or DualStackPlainSocketImpl. We use |
|
37 |
* DualStackPlainSocketImpl on systems that have a dual stack |
|
38 |
* TCP implementation. Otherwise we create an instance of |
|
39 |
* TwoStacksPlainSocketImpl and delegate to it. |
|
40 |
* |
|
41 |
* @author Chris Hegarty |
|
42 |
*/ |
|
43 |
||
44 |
class PlainSocketImpl extends AbstractPlainSocketImpl |
|
45 |
{ |
|
46 |
private AbstractPlainSocketImpl impl; |
|
47 |
||
48 |
/* the windows version. */ |
|
49 |
private static float version; |
|
50 |
||
51 |
/* java.net.preferIPv4Stack */ |
|
52 |
private static boolean preferIPv4Stack = false; |
|
53 |
||
54 |
/* If the version supports a dual stack TCP implementation */ |
|
55 |
private static boolean useDualStackImpl = false; |
|
56 |
||
57 |
static { |
|
58 |
java.security.AccessController.doPrivileged( new PrivilegedAction<Object>() { |
|
59 |
public Object run() { |
|
60 |
version = 0; |
|
61 |
try { |
|
62 |
version = Float.parseFloat(System.getProperties().getProperty("os.version")); |
|
63 |
preferIPv4Stack = Boolean.parseBoolean( |
|
64 |
System.getProperties().getProperty("java.net.preferIPv4Stack")); |
|
65 |
} catch (NumberFormatException e ) { |
|
66 |
assert false : e; |
|
67 |
} |
|
68 |
return null; // nothing to return |
|
69 |
} }); |
|
70 |
||
71 |
// (version >= 6.0) implies Vista or greater. |
|
72 |
if (version >= 6.0 && !preferIPv4Stack) { |
|
73 |
useDualStackImpl = true; |
|
74 |
} |
|
75 |
} |
|
76 |
||
77 |
/** |
|
78 |
* Constructs an empty instance. |
|
79 |
*/ |
|
80 |
PlainSocketImpl() { |
|
81 |
if (useDualStackImpl) { |
|
82 |
impl = new DualStackPlainSocketImpl(); |
|
83 |
} else { |
|
84 |
impl = new TwoStacksPlainSocketImpl(); |
|
85 |
} |
|
86 |
} |
|
87 |
||
88 |
/** |
|
89 |
* Constructs an instance with the given file descriptor. |
|
90 |
*/ |
|
91 |
PlainSocketImpl(FileDescriptor fd) { |
|
92 |
if (useDualStackImpl) { |
|
93 |
impl = new DualStackPlainSocketImpl(fd); |
|
94 |
} else { |
|
95 |
impl = new TwoStacksPlainSocketImpl(fd); |
|
96 |
} |
|
97 |
} |
|
98 |
||
99 |
// Override methods in SocketImpl that access impl's fields. |
|
100 |
||
101 |
protected FileDescriptor getFileDescriptor() { |
|
102 |
return impl.getFileDescriptor(); |
|
103 |
} |
|
104 |
||
105 |
protected InetAddress getInetAddress() { |
|
106 |
return impl.getInetAddress(); |
|
107 |
} |
|
108 |
||
109 |
protected int getPort() { |
|
110 |
return impl.getPort(); |
|
111 |
} |
|
112 |
||
113 |
protected int getLocalPort() { |
|
114 |
return impl.getLocalPort(); |
|
115 |
} |
|
116 |
||
117 |
void setSocket(Socket soc) { |
|
118 |
impl.setSocket(soc); |
|
119 |
} |
|
120 |
||
121 |
Socket getSocket() { |
|
122 |
return impl.getSocket(); |
|
123 |
} |
|
124 |
||
125 |
void setServerSocket(ServerSocket soc) { |
|
126 |
impl.setServerSocket(soc); |
|
127 |
} |
|
128 |
||
129 |
ServerSocket getServerSocket() { |
|
130 |
return impl.getServerSocket(); |
|
131 |
} |
|
132 |
||
133 |
public String toString() { |
|
134 |
return impl.toString(); |
|
135 |
} |
|
136 |
||
137 |
// Override methods in AbstractPlainSocketImpl that access impl's fields. |
|
138 |
||
139 |
protected synchronized void create(boolean stream) throws IOException { |
|
140 |
impl.create(stream); |
|
8185 | 141 |
|
142 |
// set fd to delegate's fd to be compatible with older releases |
|
143 |
this.fd = impl.fd; |
|
2 | 144 |
} |
145 |
||
146 |
protected void connect(String host, int port) |
|
147 |
throws UnknownHostException, IOException |
|
148 |
{ |
|
149 |
impl.connect(host, port); |
|
150 |
} |
|
151 |
||
152 |
protected void connect(InetAddress address, int port) throws IOException { |
|
153 |
impl.connect(address, port); |
|
154 |
} |
|
155 |
||
156 |
protected void connect(SocketAddress address, int timeout) throws IOException { |
|
157 |
impl.connect(address, timeout); |
|
158 |
} |
|
159 |
||
160 |
public void setOption(int opt, Object val) throws SocketException { |
|
161 |
impl.setOption(opt, val); |
|
162 |
} |
|
163 |
||
164 |
public Object getOption(int opt) throws SocketException { |
|
165 |
return impl.getOption(opt); |
|
166 |
} |
|
167 |
||
168 |
synchronized void doConnect(InetAddress address, int port, int timeout) throws IOException { |
|
169 |
impl.doConnect(address, port, timeout); |
|
170 |
} |
|
171 |
||
8185 | 172 |
protected synchronized void bind(InetAddress address, int lport) |
2 | 173 |
throws IOException |
174 |
{ |
|
175 |
impl.bind(address, lport); |
|
176 |
} |
|
177 |
||
178 |
protected synchronized void accept(SocketImpl s) throws IOException { |
|
179 |
// pass in the real impl not the wrapper. |
|
8185 | 180 |
SocketImpl delegate = ((PlainSocketImpl)s).impl; |
181 |
delegate.address = new InetAddress(); |
|
182 |
delegate.fd = new FileDescriptor(); |
|
183 |
impl.accept(delegate); |
|
184 |
||
185 |
// set fd to delegate's fd to be compatible with older releases |
|
186 |
s.fd = delegate.fd; |
|
2 | 187 |
} |
188 |
||
189 |
void setFileDescriptor(FileDescriptor fd) { |
|
190 |
impl.setFileDescriptor(fd); |
|
191 |
} |
|
192 |
||
193 |
void setAddress(InetAddress address) { |
|
194 |
impl.setAddress(address); |
|
195 |
} |
|
196 |
||
197 |
void setPort(int port) { |
|
198 |
impl.setPort(port); |
|
199 |
} |
|
200 |
||
201 |
void setLocalPort(int localPort) { |
|
202 |
impl.setLocalPort(localPort); |
|
203 |
} |
|
204 |
||
205 |
protected synchronized InputStream getInputStream() throws IOException { |
|
206 |
return impl.getInputStream(); |
|
207 |
} |
|
208 |
||
209 |
void setInputStream(SocketInputStream in) { |
|
210 |
impl.setInputStream(in); |
|
211 |
} |
|
212 |
||
213 |
protected synchronized OutputStream getOutputStream() throws IOException { |
|
214 |
return impl.getOutputStream(); |
|
215 |
} |
|
216 |
||
217 |
protected void close() throws IOException { |
|
8185 | 218 |
try { |
219 |
impl.close(); |
|
220 |
} finally { |
|
221 |
// set fd to delegate's fd to be compatible with older releases |
|
222 |
this.fd = null; |
|
223 |
} |
|
2 | 224 |
} |
225 |
||
226 |
void reset() throws IOException { |
|
8185 | 227 |
try { |
228 |
impl.reset(); |
|
229 |
} finally { |
|
230 |
// set fd to delegate's fd to be compatible with older releases |
|
231 |
this.fd = null; |
|
232 |
} |
|
2 | 233 |
} |
234 |
||
235 |
protected void shutdownInput() throws IOException { |
|
236 |
impl.shutdownInput(); |
|
237 |
} |
|
238 |
||
239 |
protected void shutdownOutput() throws IOException { |
|
240 |
impl.shutdownOutput(); |
|
241 |
} |
|
242 |
||
243 |
protected void sendUrgentData(int data) throws IOException { |
|
244 |
impl.sendUrgentData(data); |
|
245 |
} |
|
246 |
||
247 |
FileDescriptor acquireFD() { |
|
248 |
return impl.acquireFD(); |
|
249 |
} |
|
250 |
||
251 |
void releaseFD() { |
|
252 |
impl.releaseFD(); |
|
253 |
} |
|
254 |
||
255 |
public boolean isConnectionReset() { |
|
256 |
return impl.isConnectionReset(); |
|
257 |
} |
|
258 |
||
259 |
public boolean isConnectionResetPending() { |
|
260 |
return impl.isConnectionResetPending(); |
|
261 |
} |
|
262 |
||
263 |
public void setConnectionReset() { |
|
264 |
impl.setConnectionReset(); |
|
265 |
} |
|
266 |
||
267 |
public void setConnectionResetPending() { |
|
268 |
impl.setConnectionResetPending(); |
|
269 |
} |
|
270 |
||
271 |
public boolean isClosedOrPending() { |
|
272 |
return impl.isClosedOrPending(); |
|
273 |
} |
|
274 |
||
275 |
public int getTimeout() { |
|
276 |
return impl.getTimeout(); |
|
277 |
} |
|
278 |
||
279 |
// Override methods in AbstractPlainSocketImpl that need to be implemented. |
|
280 |
||
281 |
void socketCreate(boolean isServer) throws IOException { |
|
282 |
impl.socketCreate(isServer); |
|
283 |
} |
|
284 |
||
285 |
void socketConnect(InetAddress address, int port, int timeout) |
|
286 |
throws IOException { |
|
287 |
impl.socketConnect(address, port, timeout); |
|
288 |
} |
|
289 |
||
290 |
void socketBind(InetAddress address, int port) |
|
291 |
throws IOException { |
|
292 |
impl.socketBind(address, port); |
|
293 |
} |
|
294 |
||
295 |
void socketListen(int count) throws IOException { |
|
296 |
impl.socketListen(count); |
|
297 |
} |
|
298 |
||
299 |
void socketAccept(SocketImpl s) throws IOException { |
|
300 |
impl.socketAccept(s); |
|
301 |
} |
|
302 |
||
303 |
int socketAvailable() throws IOException { |
|
304 |
return impl.socketAvailable(); |
|
305 |
} |
|
306 |
||
307 |
void socketClose0(boolean useDeferredClose) throws IOException { |
|
308 |
impl.socketClose0(useDeferredClose); |
|
309 |
} |
|
310 |
||
311 |
void socketShutdown(int howto) throws IOException { |
|
312 |
impl.socketShutdown(howto); |
|
313 |
} |
|
314 |
||
315 |
void socketSetOption(int cmd, boolean on, Object value) |
|
316 |
throws SocketException { |
|
317 |
socketSetOption(cmd, on, value); |
|
318 |
} |
|
319 |
||
320 |
int socketGetOption(int opt, Object iaContainerObj) throws SocketException { |
|
321 |
return impl.socketGetOption(opt, iaContainerObj); |
|
322 |
} |
|
323 |
||
324 |
void socketSendUrgentData(int data) throws IOException { |
|
325 |
impl.socketSendUrgentData(data); |
|
326 |
} |
|
327 |
} |