author | simonis |
Mon, 20 Jan 2014 09:24:25 +0100 | |
changeset 22604 | 9b394795e216 |
parent 18212 | 22f8c33b0690 |
child 23010 | 6dadb192ad81 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
14342
8435a30053c1
7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents:
14025
diff
changeset
|
2 |
* Copyright (c) 2000, 2012, 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 |
||
11106
802bb70d8fa2
7115586: Suppress creation of SocketImpl in SocketAdaptor's constructor
chegar
parents:
9679
diff
changeset
|
60 |
private SocketAdaptor(SocketChannelImpl sc) throws SocketException { |
802bb70d8fa2
7115586: Suppress creation of SocketImpl in SocketAdaptor's constructor
chegar
parents:
9679
diff
changeset
|
61 |
super((SocketImpl) null); |
2 | 62 |
this.sc = sc; |
63 |
} |
|
64 |
||
65 |
public static Socket create(SocketChannelImpl sc) { |
|
11106
802bb70d8fa2
7115586: Suppress creation of SocketImpl in SocketAdaptor's constructor
chegar
parents:
9679
diff
changeset
|
66 |
try { |
802bb70d8fa2
7115586: Suppress creation of SocketImpl in SocketAdaptor's constructor
chegar
parents:
9679
diff
changeset
|
67 |
return new SocketAdaptor(sc); |
802bb70d8fa2
7115586: Suppress creation of SocketImpl in SocketAdaptor's constructor
chegar
parents:
9679
diff
changeset
|
68 |
} catch (SocketException e) { |
802bb70d8fa2
7115586: Suppress creation of SocketImpl in SocketAdaptor's constructor
chegar
parents:
9679
diff
changeset
|
69 |
throw new InternalError("Should not reach here"); |
802bb70d8fa2
7115586: Suppress creation of SocketImpl in SocketAdaptor's constructor
chegar
parents:
9679
diff
changeset
|
70 |
} |
2 | 71 |
} |
72 |
||
73 |
public SocketChannel getChannel() { |
|
74 |
return sc; |
|
75 |
} |
|
76 |
||
77 |
// Override this method just to protect against changes in the superclass |
|
78 |
// |
|
79 |
public void connect(SocketAddress remote) throws IOException { |
|
80 |
connect(remote, 0); |
|
81 |
} |
|
82 |
||
83 |
public void connect(SocketAddress remote, int timeout) throws IOException { |
|
84 |
if (remote == null) |
|
85 |
throw new IllegalArgumentException("connect: The address can't be null"); |
|
86 |
if (timeout < 0) |
|
87 |
throw new IllegalArgumentException("connect: timeout can't be negative"); |
|
88 |
||
89 |
synchronized (sc.blockingLock()) { |
|
90 |
if (!sc.isBlocking()) |
|
91 |
throw new IllegalBlockingModeException(); |
|
92 |
||
93 |
try { |
|
94 |
||
95 |
if (timeout == 0) { |
|
96 |
sc.connect(remote); |
|
97 |
return; |
|
98 |
} |
|
99 |
||
100 |
sc.configureBlocking(false); |
|
101 |
try { |
|
102 |
if (sc.connect(remote)) |
|
103 |
return; |
|
104 |
long to = timeout; |
|
105 |
for (;;) { |
|
106 |
if (!sc.isOpen()) |
|
107 |
throw new ClosedChannelException(); |
|
108 |
long st = System.currentTimeMillis(); |
|
14025
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
11106
diff
changeset
|
109 |
|
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
18212
diff
changeset
|
110 |
int result = sc.poll(Net.POLLCONN, to); |
14025
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
11106
diff
changeset
|
111 |
if (result > 0 && sc.finishConnect()) |
2 | 112 |
break; |
113 |
to -= System.currentTimeMillis() - st; |
|
114 |
if (to <= 0) { |
|
115 |
try { |
|
116 |
sc.close(); |
|
117 |
} catch (IOException x) { } |
|
118 |
throw new SocketTimeoutException(); |
|
119 |
} |
|
120 |
} |
|
121 |
} finally { |
|
122 |
if (sc.isOpen()) |
|
123 |
sc.configureBlocking(true); |
|
124 |
} |
|
125 |
||
126 |
} catch (Exception x) { |
|
127 |
Net.translateException(x, true); |
|
128 |
} |
|
129 |
} |
|
130 |
||
131 |
} |
|
132 |
||
133 |
public void bind(SocketAddress local) throws IOException { |
|
134 |
try { |
|
135 |
sc.bind(local); |
|
136 |
} catch (Exception x) { |
|
137 |
Net.translateException(x); |
|
138 |
} |
|
139 |
} |
|
140 |
||
141 |
public InetAddress getInetAddress() { |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
142 |
SocketAddress remote = sc.remoteAddress(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
143 |
if (remote == null) { |
2 | 144 |
return null; |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
145 |
} else { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
146 |
return ((InetSocketAddress)remote).getAddress(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
147 |
} |
2 | 148 |
} |
149 |
||
150 |
public InetAddress getLocalAddress() { |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
151 |
if (sc.isOpen()) { |
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
14342
diff
changeset
|
152 |
InetSocketAddress local = sc.localAddress(); |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
14342
diff
changeset
|
153 |
if (local != null) { |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
14342
diff
changeset
|
154 |
return Net.getRevealedLocalAddress(local).getAddress(); |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
14342
diff
changeset
|
155 |
} |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
156 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
157 |
return new InetSocketAddress(0).getAddress(); |
2 | 158 |
} |
159 |
||
160 |
public int getPort() { |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
161 |
SocketAddress remote = sc.remoteAddress(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
162 |
if (remote == null) { |
2 | 163 |
return 0; |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
164 |
} else { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
165 |
return ((InetSocketAddress)remote).getPort(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
166 |
} |
2 | 167 |
} |
168 |
||
169 |
public int getLocalPort() { |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
170 |
SocketAddress local = sc.localAddress(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
171 |
if (local == null) { |
2 | 172 |
return -1; |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
173 |
} else { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
174 |
return ((InetSocketAddress)local).getPort(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
175 |
} |
2 | 176 |
} |
177 |
||
178 |
private class SocketInputStream |
|
179 |
extends ChannelInputStream |
|
180 |
{ |
|
181 |
private SocketInputStream() { |
|
182 |
super(sc); |
|
183 |
} |
|
184 |
||
185 |
protected int read(ByteBuffer bb) |
|
186 |
throws IOException |
|
187 |
{ |
|
188 |
synchronized (sc.blockingLock()) { |
|
189 |
if (!sc.isBlocking()) |
|
190 |
throw new IllegalBlockingModeException(); |
|
191 |
if (timeout == 0) |
|
192 |
return sc.read(bb); |
|
14025
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
11106
diff
changeset
|
193 |
sc.configureBlocking(false); |
2 | 194 |
|
195 |
try { |
|
196 |
int n; |
|
197 |
if ((n = sc.read(bb)) != 0) |
|
198 |
return n; |
|
199 |
long to = timeout; |
|
200 |
for (;;) { |
|
201 |
if (!sc.isOpen()) |
|
202 |
throw new ClosedChannelException(); |
|
203 |
long st = System.currentTimeMillis(); |
|
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
18212
diff
changeset
|
204 |
int result = sc.poll(Net.POLLIN, to); |
14025
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
11106
diff
changeset
|
205 |
if (result > 0) { |
2 | 206 |
if ((n = sc.read(bb)) != 0) |
207 |
return n; |
|
208 |
} |
|
209 |
to -= System.currentTimeMillis() - st; |
|
210 |
if (to <= 0) |
|
211 |
throw new SocketTimeoutException(); |
|
212 |
} |
|
213 |
} finally { |
|
214 |
if (sc.isOpen()) |
|
215 |
sc.configureBlocking(true); |
|
216 |
} |
|
217 |
||
218 |
} |
|
219 |
} |
|
220 |
} |
|
221 |
||
222 |
private InputStream socketInputStream = null; |
|
223 |
||
224 |
public InputStream getInputStream() throws IOException { |
|
225 |
if (!sc.isOpen()) |
|
226 |
throw new SocketException("Socket is closed"); |
|
227 |
if (!sc.isConnected()) |
|
228 |
throw new SocketException("Socket is not connected"); |
|
229 |
if (!sc.isInputOpen()) |
|
230 |
throw new SocketException("Socket input is shutdown"); |
|
231 |
if (socketInputStream == null) { |
|
232 |
try { |
|
51 | 233 |
socketInputStream = AccessController.doPrivileged( |
234 |
new PrivilegedExceptionAction<InputStream>() { |
|
235 |
public InputStream run() throws IOException { |
|
2 | 236 |
return new SocketInputStream(); |
237 |
} |
|
238 |
}); |
|
239 |
} catch (java.security.PrivilegedActionException e) { |
|
240 |
throw (IOException)e.getException(); |
|
241 |
} |
|
242 |
} |
|
243 |
return socketInputStream; |
|
244 |
} |
|
245 |
||
246 |
public OutputStream getOutputStream() throws IOException { |
|
247 |
if (!sc.isOpen()) |
|
248 |
throw new SocketException("Socket is closed"); |
|
249 |
if (!sc.isConnected()) |
|
250 |
throw new SocketException("Socket is not connected"); |
|
251 |
if (!sc.isOutputOpen()) |
|
252 |
throw new SocketException("Socket output is shutdown"); |
|
253 |
OutputStream os = null; |
|
254 |
try { |
|
51 | 255 |
os = AccessController.doPrivileged( |
256 |
new PrivilegedExceptionAction<OutputStream>() { |
|
257 |
public OutputStream run() throws IOException { |
|
2 | 258 |
return Channels.newOutputStream(sc); |
259 |
} |
|
260 |
}); |
|
261 |
} catch (java.security.PrivilegedActionException e) { |
|
262 |
throw (IOException)e.getException(); |
|
263 |
} |
|
264 |
return os; |
|
265 |
} |
|
266 |
||
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
267 |
private void setBooleanOption(SocketOption<Boolean> name, boolean value) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
268 |
throws SocketException |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
269 |
{ |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
270 |
try { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
271 |
sc.setOption(name, value); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
272 |
} catch (IOException x) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
273 |
Net.translateToSocketException(x); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
274 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
275 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
276 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
277 |
private void setIntOption(SocketOption<Integer> name, int value) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
278 |
throws SocketException |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
279 |
{ |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
280 |
try { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
281 |
sc.setOption(name, value); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
282 |
} catch (IOException x) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
283 |
Net.translateToSocketException(x); |
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 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
286 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
287 |
private boolean getBooleanOption(SocketOption<Boolean> name) throws SocketException { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
288 |
try { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
289 |
return sc.getOption(name).booleanValue(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
290 |
} catch (IOException x) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
291 |
Net.translateToSocketException(x); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
292 |
return false; // keep compiler happy |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
293 |
} |
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 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
296 |
private int getIntOption(SocketOption<Integer> name) throws SocketException { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
297 |
try { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
298 |
return sc.getOption(name).intValue(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
299 |
} catch (IOException x) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
300 |
Net.translateToSocketException(x); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
301 |
return -1; // keep compiler happy |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
302 |
} |
2 | 303 |
} |
304 |
||
305 |
public void setTcpNoDelay(boolean on) throws SocketException { |
|
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
306 |
setBooleanOption(StandardSocketOptions.TCP_NODELAY, on); |
2 | 307 |
} |
308 |
||
309 |
public boolean getTcpNoDelay() throws SocketException { |
|
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
310 |
return getBooleanOption(StandardSocketOptions.TCP_NODELAY); |
2 | 311 |
} |
312 |
||
313 |
public void setSoLinger(boolean on, int linger) throws SocketException { |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
314 |
if (!on) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
315 |
linger = -1; |
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
316 |
setIntOption(StandardSocketOptions.SO_LINGER, linger); |
2 | 317 |
} |
318 |
||
319 |
public int getSoLinger() throws SocketException { |
|
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
320 |
return getIntOption(StandardSocketOptions.SO_LINGER); |
2 | 321 |
} |
322 |
||
323 |
public void sendUrgentData(int data) throws IOException { |
|
6117
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5784
diff
changeset
|
324 |
synchronized (sc.blockingLock()) { |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5784
diff
changeset
|
325 |
if (!sc.isBlocking()) |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5784
diff
changeset
|
326 |
throw new IllegalBlockingModeException(); |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5784
diff
changeset
|
327 |
int n = sc.sendOutOfBandData((byte)data); |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5784
diff
changeset
|
328 |
assert n == 1; |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5784
diff
changeset
|
329 |
} |
2 | 330 |
} |
331 |
||
332 |
public void setOOBInline(boolean on) throws SocketException { |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
333 |
setBooleanOption(ExtendedSocketOption.SO_OOBINLINE, on); |
2 | 334 |
} |
335 |
||
336 |
public boolean getOOBInline() throws SocketException { |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
337 |
return getBooleanOption(ExtendedSocketOption.SO_OOBINLINE); |
2 | 338 |
} |
339 |
||
340 |
public void setSoTimeout(int timeout) throws SocketException { |
|
341 |
if (timeout < 0) |
|
342 |
throw new IllegalArgumentException("timeout can't be negative"); |
|
343 |
this.timeout = timeout; |
|
344 |
} |
|
345 |
||
346 |
public int getSoTimeout() throws SocketException { |
|
347 |
return timeout; |
|
348 |
} |
|
349 |
||
350 |
public void setSendBufferSize(int size) throws SocketException { |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
351 |
// size 0 valid for SocketChannel, invalid for Socket |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
352 |
if (size <= 0) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
353 |
throw new IllegalArgumentException("Invalid send size"); |
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
354 |
setIntOption(StandardSocketOptions.SO_SNDBUF, size); |
2 | 355 |
} |
356 |
||
357 |
public int getSendBufferSize() throws SocketException { |
|
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
358 |
return getIntOption(StandardSocketOptions.SO_SNDBUF); |
2 | 359 |
} |
360 |
||
361 |
public void setReceiveBufferSize(int size) throws SocketException { |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
362 |
// size 0 valid for SocketChannel, invalid for Socket |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
363 |
if (size <= 0) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
364 |
throw new IllegalArgumentException("Invalid receive size"); |
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
365 |
setIntOption(StandardSocketOptions.SO_RCVBUF, size); |
2 | 366 |
} |
367 |
||
368 |
public int getReceiveBufferSize() throws SocketException { |
|
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
369 |
return getIntOption(StandardSocketOptions.SO_RCVBUF); |
2 | 370 |
} |
371 |
||
372 |
public void setKeepAlive(boolean on) throws SocketException { |
|
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
373 |
setBooleanOption(StandardSocketOptions.SO_KEEPALIVE, on); |
2 | 374 |
} |
375 |
||
376 |
public boolean getKeepAlive() throws SocketException { |
|
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
377 |
return getBooleanOption(StandardSocketOptions.SO_KEEPALIVE); |
2 | 378 |
} |
379 |
||
380 |
public void setTrafficClass(int tc) throws SocketException { |
|
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
381 |
setIntOption(StandardSocketOptions.IP_TOS, tc); |
2 | 382 |
} |
383 |
||
384 |
public int getTrafficClass() throws SocketException { |
|
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
385 |
return getIntOption(StandardSocketOptions.IP_TOS); |
2 | 386 |
} |
387 |
||
388 |
public void setReuseAddress(boolean on) throws SocketException { |
|
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
389 |
setBooleanOption(StandardSocketOptions.SO_REUSEADDR, on); |
2 | 390 |
} |
391 |
||
392 |
public boolean getReuseAddress() throws SocketException { |
|
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
393 |
return getBooleanOption(StandardSocketOptions.SO_REUSEADDR); |
2 | 394 |
} |
395 |
||
396 |
public void close() throws IOException { |
|
5784
e565c553e9fc
6938230: (so) SocketAdaptor.close() does not translate IOException resulting in Error
alanb
parents:
5506
diff
changeset
|
397 |
sc.close(); |
2 | 398 |
} |
399 |
||
400 |
public void shutdownInput() throws IOException { |
|
401 |
try { |
|
402 |
sc.shutdownInput(); |
|
403 |
} catch (Exception x) { |
|
404 |
Net.translateException(x); |
|
405 |
} |
|
406 |
} |
|
407 |
||
408 |
public void shutdownOutput() throws IOException { |
|
409 |
try { |
|
410 |
sc.shutdownOutput(); |
|
411 |
} catch (Exception x) { |
|
412 |
Net.translateException(x); |
|
413 |
} |
|
414 |
} |
|
415 |
||
416 |
public String toString() { |
|
417 |
if (sc.isConnected()) |
|
418 |
return "Socket[addr=" + getInetAddress() + |
|
419 |
",port=" + getPort() + |
|
420 |
",localport=" + getLocalPort() + "]"; |
|
421 |
return "Socket[unconnected]"; |
|
422 |
} |
|
423 |
||
424 |
public boolean isConnected() { |
|
425 |
return sc.isConnected(); |
|
426 |
} |
|
427 |
||
428 |
public boolean isBound() { |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
429 |
return sc.localAddress() != null; |
2 | 430 |
} |
431 |
||
432 |
public boolean isClosed() { |
|
433 |
return !sc.isOpen(); |
|
434 |
} |
|
435 |
||
436 |
public boolean isInputShutdown() { |
|
437 |
return !sc.isInputOpen(); |
|
438 |
} |
|
439 |
||
440 |
public boolean isOutputShutdown() { |
|
441 |
return !sc.isOutputOpen(); |
|
442 |
} |
|
443 |
||
444 |
} |