author | alanb |
Fri, 11 Jun 2010 14:47:22 +0100 | |
changeset 5784 | e565c553e9fc |
parent 5506 | 202f599c92aa |
child 6117 | 471ae95609d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2000, 2008, 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 |
||
26 |
package sun.nio.ch; |
|
27 |
||
28 |
import java.io.*; |
|
29 |
import java.lang.ref.*; |
|
30 |
import java.net.*; |
|
31 |
import java.nio.*; |
|
32 |
import java.nio.channels.*; |
|
33 |
import java.security.AccessController; |
|
34 |
import java.security.PrivilegedExceptionAction; |
|
35 |
import java.util.*; |
|
36 |
||
37 |
||
38 |
// Make a socket channel look like a socket. |
|
39 |
// |
|
40 |
// The only aspects of java.net.Socket-hood that we don't attempt to emulate |
|
41 |
// here are the interrupted-I/O exceptions (which our Solaris implementations |
|
42 |
// attempt to support) and the sending of urgent data. Otherwise an adapted |
|
43 |
// socket should look enough like a real java.net.Socket to fool most of the |
|
44 |
// developers most of the time, right down to the exception message strings. |
|
45 |
// |
|
46 |
// The methods in this class are defined in exactly the same order as in |
|
47 |
// java.net.Socket so as to simplify tracking future changes to that class. |
|
48 |
// |
|
49 |
||
50 |
public class SocketAdaptor |
|
51 |
extends Socket |
|
52 |
{ |
|
53 |
||
54 |
// The channel being adapted |
|
55 |
private final SocketChannelImpl sc; |
|
56 |
||
57 |
// Timeout "option" value for reads |
|
58 |
private volatile int timeout = 0; |
|
59 |
||
60 |
// ## super will create a useless impl |
|
61 |
private SocketAdaptor(SocketChannelImpl sc) { |
|
62 |
this.sc = sc; |
|
63 |
} |
|
64 |
||
65 |
public static Socket create(SocketChannelImpl sc) { |
|
66 |
return new SocketAdaptor(sc); |
|
67 |
} |
|
68 |
||
69 |
public SocketChannel getChannel() { |
|
70 |
return sc; |
|
71 |
} |
|
72 |
||
73 |
// Override this method just to protect against changes in the superclass |
|
74 |
// |
|
75 |
public void connect(SocketAddress remote) throws IOException { |
|
76 |
connect(remote, 0); |
|
77 |
} |
|
78 |
||
79 |
public void connect(SocketAddress remote, int timeout) throws IOException { |
|
80 |
if (remote == null) |
|
81 |
throw new IllegalArgumentException("connect: The address can't be null"); |
|
82 |
if (timeout < 0) |
|
83 |
throw new IllegalArgumentException("connect: timeout can't be negative"); |
|
84 |
||
85 |
synchronized (sc.blockingLock()) { |
|
86 |
if (!sc.isBlocking()) |
|
87 |
throw new IllegalBlockingModeException(); |
|
88 |
||
89 |
try { |
|
90 |
||
91 |
if (timeout == 0) { |
|
92 |
sc.connect(remote); |
|
93 |
return; |
|
94 |
} |
|
95 |
||
96 |
// Implement timeout with a selector |
|
97 |
SelectionKey sk = null; |
|
98 |
Selector sel = null; |
|
99 |
sc.configureBlocking(false); |
|
100 |
try { |
|
101 |
if (sc.connect(remote)) |
|
102 |
return; |
|
103 |
sel = Util.getTemporarySelector(sc); |
|
104 |
sk = sc.register(sel, SelectionKey.OP_CONNECT); |
|
105 |
long to = timeout; |
|
106 |
for (;;) { |
|
107 |
if (!sc.isOpen()) |
|
108 |
throw new ClosedChannelException(); |
|
109 |
long st = System.currentTimeMillis(); |
|
110 |
int ns = sel.select(to); |
|
111 |
if (ns > 0 && |
|
112 |
sk.isConnectable() && sc.finishConnect()) |
|
113 |
break; |
|
114 |
sel.selectedKeys().remove(sk); |
|
115 |
to -= System.currentTimeMillis() - st; |
|
116 |
if (to <= 0) { |
|
117 |
try { |
|
118 |
sc.close(); |
|
119 |
} catch (IOException x) { } |
|
120 |
throw new SocketTimeoutException(); |
|
121 |
} |
|
122 |
} |
|
123 |
} finally { |
|
124 |
if (sk != null) |
|
125 |
sk.cancel(); |
|
126 |
if (sc.isOpen()) |
|
127 |
sc.configureBlocking(true); |
|
128 |
if (sel != null) |
|
129 |
Util.releaseTemporarySelector(sel); |
|
130 |
} |
|
131 |
||
132 |
} catch (Exception x) { |
|
133 |
Net.translateException(x, true); |
|
134 |
} |
|
135 |
} |
|
136 |
||
137 |
} |
|
138 |
||
139 |
public void bind(SocketAddress local) throws IOException { |
|
140 |
try { |
|
141 |
sc.bind(local); |
|
142 |
} catch (Exception x) { |
|
143 |
Net.translateException(x); |
|
144 |
} |
|
145 |
} |
|
146 |
||
147 |
public InetAddress getInetAddress() { |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
148 |
SocketAddress remote = sc.remoteAddress(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
149 |
if (remote == null) { |
2 | 150 |
return null; |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
151 |
} else { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
152 |
return ((InetSocketAddress)remote).getAddress(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
153 |
} |
2 | 154 |
} |
155 |
||
156 |
public InetAddress getLocalAddress() { |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
157 |
if (sc.isOpen()) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
158 |
SocketAddress local = sc.localAddress(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
159 |
if (local != null) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
160 |
return ((InetSocketAddress)local).getAddress(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
161 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
162 |
return new InetSocketAddress(0).getAddress(); |
2 | 163 |
} |
164 |
||
165 |
public int getPort() { |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
166 |
SocketAddress remote = sc.remoteAddress(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
167 |
if (remote == null) { |
2 | 168 |
return 0; |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
169 |
} else { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
170 |
return ((InetSocketAddress)remote).getPort(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
171 |
} |
2 | 172 |
} |
173 |
||
174 |
public int getLocalPort() { |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
175 |
SocketAddress local = sc.localAddress(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
176 |
if (local == null) { |
2 | 177 |
return -1; |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
178 |
} else { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
179 |
return ((InetSocketAddress)local).getPort(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
180 |
} |
2 | 181 |
} |
182 |
||
183 |
private class SocketInputStream |
|
184 |
extends ChannelInputStream |
|
185 |
{ |
|
186 |
private SocketInputStream() { |
|
187 |
super(sc); |
|
188 |
} |
|
189 |
||
190 |
protected int read(ByteBuffer bb) |
|
191 |
throws IOException |
|
192 |
{ |
|
193 |
synchronized (sc.blockingLock()) { |
|
194 |
if (!sc.isBlocking()) |
|
195 |
throw new IllegalBlockingModeException(); |
|
196 |
if (timeout == 0) |
|
197 |
return sc.read(bb); |
|
198 |
||
199 |
// Implement timeout with a selector |
|
200 |
SelectionKey sk = null; |
|
201 |
Selector sel = null; |
|
202 |
sc.configureBlocking(false); |
|
203 |
try { |
|
204 |
int n; |
|
205 |
if ((n = sc.read(bb)) != 0) |
|
206 |
return n; |
|
207 |
sel = Util.getTemporarySelector(sc); |
|
208 |
sk = sc.register(sel, SelectionKey.OP_READ); |
|
209 |
long to = timeout; |
|
210 |
for (;;) { |
|
211 |
if (!sc.isOpen()) |
|
212 |
throw new ClosedChannelException(); |
|
213 |
long st = System.currentTimeMillis(); |
|
214 |
int ns = sel.select(to); |
|
215 |
if (ns > 0 && sk.isReadable()) { |
|
216 |
if ((n = sc.read(bb)) != 0) |
|
217 |
return n; |
|
218 |
} |
|
219 |
sel.selectedKeys().remove(sk); |
|
220 |
to -= System.currentTimeMillis() - st; |
|
221 |
if (to <= 0) |
|
222 |
throw new SocketTimeoutException(); |
|
223 |
} |
|
224 |
} finally { |
|
225 |
if (sk != null) |
|
226 |
sk.cancel(); |
|
227 |
if (sc.isOpen()) |
|
228 |
sc.configureBlocking(true); |
|
229 |
if (sel != null) |
|
230 |
Util.releaseTemporarySelector(sel); |
|
231 |
} |
|
232 |
||
233 |
} |
|
234 |
} |
|
235 |
} |
|
236 |
||
237 |
private InputStream socketInputStream = null; |
|
238 |
||
239 |
public InputStream getInputStream() throws IOException { |
|
240 |
if (!sc.isOpen()) |
|
241 |
throw new SocketException("Socket is closed"); |
|
242 |
if (!sc.isConnected()) |
|
243 |
throw new SocketException("Socket is not connected"); |
|
244 |
if (!sc.isInputOpen()) |
|
245 |
throw new SocketException("Socket input is shutdown"); |
|
246 |
if (socketInputStream == null) { |
|
247 |
try { |
|
51 | 248 |
socketInputStream = AccessController.doPrivileged( |
249 |
new PrivilegedExceptionAction<InputStream>() { |
|
250 |
public InputStream run() throws IOException { |
|
2 | 251 |
return new SocketInputStream(); |
252 |
} |
|
253 |
}); |
|
254 |
} catch (java.security.PrivilegedActionException e) { |
|
255 |
throw (IOException)e.getException(); |
|
256 |
} |
|
257 |
} |
|
258 |
return socketInputStream; |
|
259 |
} |
|
260 |
||
261 |
public OutputStream getOutputStream() throws IOException { |
|
262 |
if (!sc.isOpen()) |
|
263 |
throw new SocketException("Socket is closed"); |
|
264 |
if (!sc.isConnected()) |
|
265 |
throw new SocketException("Socket is not connected"); |
|
266 |
if (!sc.isOutputOpen()) |
|
267 |
throw new SocketException("Socket output is shutdown"); |
|
268 |
OutputStream os = null; |
|
269 |
try { |
|
51 | 270 |
os = AccessController.doPrivileged( |
271 |
new PrivilegedExceptionAction<OutputStream>() { |
|
272 |
public OutputStream run() throws IOException { |
|
2 | 273 |
return Channels.newOutputStream(sc); |
274 |
} |
|
275 |
}); |
|
276 |
} catch (java.security.PrivilegedActionException e) { |
|
277 |
throw (IOException)e.getException(); |
|
278 |
} |
|
279 |
return os; |
|
280 |
} |
|
281 |
||
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
282 |
private void setBooleanOption(SocketOption<Boolean> name, boolean value) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
283 |
throws SocketException |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
284 |
{ |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
285 |
try { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
286 |
sc.setOption(name, value); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
287 |
} catch (IOException x) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
288 |
Net.translateToSocketException(x); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
289 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
290 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
291 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
292 |
private void setIntOption(SocketOption<Integer> name, int value) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
293 |
throws SocketException |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
294 |
{ |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
295 |
try { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
296 |
sc.setOption(name, value); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
297 |
} catch (IOException x) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
298 |
Net.translateToSocketException(x); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
299 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
300 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
301 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
302 |
private boolean getBooleanOption(SocketOption<Boolean> name) throws SocketException { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
303 |
try { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
304 |
return sc.getOption(name).booleanValue(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
305 |
} catch (IOException x) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
306 |
Net.translateToSocketException(x); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
307 |
return false; // keep compiler happy |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
308 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
309 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
310 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
311 |
private int getIntOption(SocketOption<Integer> name) throws SocketException { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
312 |
try { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
313 |
return sc.getOption(name).intValue(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
314 |
} catch (IOException x) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
315 |
Net.translateToSocketException(x); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
316 |
return -1; // keep compiler happy |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
317 |
} |
2 | 318 |
} |
319 |
||
320 |
public void setTcpNoDelay(boolean on) throws SocketException { |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
321 |
setBooleanOption(StandardSocketOption.TCP_NODELAY, on); |
2 | 322 |
} |
323 |
||
324 |
public boolean getTcpNoDelay() throws SocketException { |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
325 |
return getBooleanOption(StandardSocketOption.TCP_NODELAY); |
2 | 326 |
} |
327 |
||
328 |
public void setSoLinger(boolean on, int linger) throws SocketException { |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
329 |
if (!on) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
330 |
linger = -1; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
331 |
setIntOption(StandardSocketOption.SO_LINGER, linger); |
2 | 332 |
} |
333 |
||
334 |
public int getSoLinger() throws SocketException { |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
335 |
return getIntOption(StandardSocketOption.SO_LINGER); |
2 | 336 |
} |
337 |
||
338 |
public void sendUrgentData(int data) throws IOException { |
|
339 |
throw new SocketException("Urgent data not supported"); |
|
340 |
} |
|
341 |
||
342 |
public void setOOBInline(boolean on) throws SocketException { |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
343 |
setBooleanOption(ExtendedSocketOption.SO_OOBINLINE, on); |
2 | 344 |
} |
345 |
||
346 |
public boolean getOOBInline() throws SocketException { |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
347 |
return getBooleanOption(ExtendedSocketOption.SO_OOBINLINE); |
2 | 348 |
} |
349 |
||
350 |
public void setSoTimeout(int timeout) throws SocketException { |
|
351 |
if (timeout < 0) |
|
352 |
throw new IllegalArgumentException("timeout can't be negative"); |
|
353 |
this.timeout = timeout; |
|
354 |
} |
|
355 |
||
356 |
public int getSoTimeout() throws SocketException { |
|
357 |
return timeout; |
|
358 |
} |
|
359 |
||
360 |
public void setSendBufferSize(int size) throws SocketException { |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
361 |
// size 0 valid for SocketChannel, invalid for Socket |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
362 |
if (size <= 0) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
363 |
throw new IllegalArgumentException("Invalid send size"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
364 |
setIntOption(StandardSocketOption.SO_SNDBUF, size); |
2 | 365 |
} |
366 |
||
367 |
public int getSendBufferSize() throws SocketException { |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
368 |
return getIntOption(StandardSocketOption.SO_SNDBUF); |
2 | 369 |
} |
370 |
||
371 |
public void setReceiveBufferSize(int size) throws SocketException { |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
372 |
// size 0 valid for SocketChannel, invalid for Socket |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
373 |
if (size <= 0) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
374 |
throw new IllegalArgumentException("Invalid receive size"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
375 |
setIntOption(StandardSocketOption.SO_RCVBUF, size); |
2 | 376 |
} |
377 |
||
378 |
public int getReceiveBufferSize() throws SocketException { |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
379 |
return getIntOption(StandardSocketOption.SO_RCVBUF); |
2 | 380 |
} |
381 |
||
382 |
public void setKeepAlive(boolean on) throws SocketException { |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
383 |
setBooleanOption(StandardSocketOption.SO_KEEPALIVE, on); |
2 | 384 |
} |
385 |
||
386 |
public boolean getKeepAlive() throws SocketException { |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
387 |
return getBooleanOption(StandardSocketOption.SO_KEEPALIVE); |
2 | 388 |
} |
389 |
||
390 |
public void setTrafficClass(int tc) throws SocketException { |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
391 |
setIntOption(StandardSocketOption.IP_TOS, tc); |
2 | 392 |
} |
393 |
||
394 |
public int getTrafficClass() throws SocketException { |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
395 |
return getIntOption(StandardSocketOption.IP_TOS); |
2 | 396 |
} |
397 |
||
398 |
public void setReuseAddress(boolean on) throws SocketException { |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
399 |
setBooleanOption(StandardSocketOption.SO_REUSEADDR, on); |
2 | 400 |
} |
401 |
||
402 |
public boolean getReuseAddress() throws SocketException { |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
403 |
return getBooleanOption(StandardSocketOption.SO_REUSEADDR); |
2 | 404 |
} |
405 |
||
406 |
public void close() throws IOException { |
|
5784
e565c553e9fc
6938230: (so) SocketAdaptor.close() does not translate IOException resulting in Error
alanb
parents:
5506
diff
changeset
|
407 |
sc.close(); |
2 | 408 |
} |
409 |
||
410 |
public void shutdownInput() throws IOException { |
|
411 |
try { |
|
412 |
sc.shutdownInput(); |
|
413 |
} catch (Exception x) { |
|
414 |
Net.translateException(x); |
|
415 |
} |
|
416 |
} |
|
417 |
||
418 |
public void shutdownOutput() throws IOException { |
|
419 |
try { |
|
420 |
sc.shutdownOutput(); |
|
421 |
} catch (Exception x) { |
|
422 |
Net.translateException(x); |
|
423 |
} |
|
424 |
} |
|
425 |
||
426 |
public String toString() { |
|
427 |
if (sc.isConnected()) |
|
428 |
return "Socket[addr=" + getInetAddress() + |
|
429 |
",port=" + getPort() + |
|
430 |
",localport=" + getLocalPort() + "]"; |
|
431 |
return "Socket[unconnected]"; |
|
432 |
} |
|
433 |
||
434 |
public boolean isConnected() { |
|
435 |
return sc.isConnected(); |
|
436 |
} |
|
437 |
||
438 |
public boolean isBound() { |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
439 |
return sc.localAddress() != null; |
2 | 440 |
} |
441 |
||
442 |
public boolean isClosed() { |
|
443 |
return !sc.isOpen(); |
|
444 |
} |
|
445 |
||
446 |
public boolean isInputShutdown() { |
|
447 |
return !sc.isInputOpen(); |
|
448 |
} |
|
449 |
||
450 |
public boolean isOutputShutdown() { |
|
451 |
return !sc.isOutputOpen(); |
|
452 |
} |
|
453 |
||
454 |
} |