author | chegar |
Thu, 17 Oct 2019 20:54:25 +0100 | |
branch | datagramsocketimpl-branch |
changeset 58679 | 9c3209ff7550 |
parent 58678 | 9cf78a70fa4f |
parent 58610 | 94710bb2a5bb |
permissions | -rw-r--r-- |
2 | 1 |
/* |
53133
bf2533105a26
8215912: Various Typos in java.net Method Documentation
coffeys
parents:
52499
diff
changeset
|
2 |
* Copyright (c) 1995, 2019, 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 java.net; |
|
27 |
||
58610 | 28 |
import sun.security.util.SecurityConstants; |
29 |
||
2 | 30 |
import java.io.InputStream; |
31 |
import java.io.OutputStream; |
|
32 |
import java.io.IOException; |
|
54155
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
33 |
import java.lang.invoke.MethodHandles; |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
34 |
import java.lang.invoke.VarHandle; |
2 | 35 |
import java.nio.channels.SocketChannel; |
36 |
import java.security.AccessController; |
|
37 |
import java.security.PrivilegedAction; |
|
55081
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
38 |
import java.util.Objects; |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
39 |
import java.util.Set; |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
40 |
import java.util.Collections; |
2 | 41 |
|
42 |
/** |
|
43 |
* This class implements client sockets (also called just |
|
44 |
* "sockets"). A socket is an endpoint for communication |
|
45 |
* between two machines. |
|
46 |
* <p> |
|
47 |
* The actual work of the socket is performed by an instance of the |
|
19069 | 48 |
* {@code SocketImpl} class. An application, by changing |
2 | 49 |
* the socket factory that creates the socket implementation, |
50 |
* can configure itself to create sockets appropriate to the local |
|
51 |
* firewall. |
|
52 |
* |
|
53 |
* @author unascribed |
|
54 |
* @see java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory) |
|
55 |
* @see java.net.SocketImpl |
|
56 |
* @see java.nio.channels.SocketChannel |
|
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
24685
diff
changeset
|
57 |
* @since 1.0 |
2 | 58 |
*/ |
59 |
public |
|
60 |
class Socket implements java.io.Closeable { |
|
61 |
/** |
|
62 |
* Various states of this socket. |
|
63 |
*/ |
|
64 |
private boolean created = false; |
|
65 |
private boolean bound = false; |
|
66 |
private boolean connected = false; |
|
67 |
private boolean closed = false; |
|
68 |
private Object closeLock = new Object(); |
|
69 |
private boolean shutIn = false; |
|
70 |
private boolean shutOut = false; |
|
71 |
||
72 |
/** |
|
73 |
* The implementation of this Socket. |
|
74 |
*/ |
|
75 |
SocketImpl impl; |
|
76 |
||
77 |
/** |
|
54155
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
78 |
* Socket input/output streams |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
79 |
*/ |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
80 |
private volatile InputStream in; |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
81 |
private volatile OutputStream out; |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
82 |
private static final VarHandle IN, OUT; |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
83 |
static { |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
84 |
try { |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
85 |
MethodHandles.Lookup l = MethodHandles.lookup(); |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
86 |
IN = l.findVarHandle(Socket.class, "in", InputStream.class); |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
87 |
OUT = l.findVarHandle(Socket.class, "out", OutputStream.class); |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
88 |
} catch (Exception e) { |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
89 |
throw new InternalError(e); |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
90 |
} |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
91 |
} |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
92 |
|
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
93 |
/** |
55323
fc4042870cc4
8225426: Replace plain with system-default in Socket and ServerSocket
chegar
parents:
55081
diff
changeset
|
94 |
* Creates an unconnected Socket. |
fc4042870cc4
8225426: Replace plain with system-default in Socket and ServerSocket
chegar
parents:
55081
diff
changeset
|
95 |
* <p> |
fc4042870cc4
8225426: Replace plain with system-default in Socket and ServerSocket
chegar
parents:
55081
diff
changeset
|
96 |
* If the application has specified a client socket implementation |
fc4042870cc4
8225426: Replace plain with system-default in Socket and ServerSocket
chegar
parents:
55081
diff
changeset
|
97 |
* factory, that factory's {@code createSocketImpl} method is called to |
fc4042870cc4
8225426: Replace plain with system-default in Socket and ServerSocket
chegar
parents:
55081
diff
changeset
|
98 |
* create the actual socket implementation. Otherwise a system-default |
fc4042870cc4
8225426: Replace plain with system-default in Socket and ServerSocket
chegar
parents:
55081
diff
changeset
|
99 |
* socket implementation is created. |
2 | 100 |
* |
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
24685
diff
changeset
|
101 |
* @since 1.1 |
2 | 102 |
* @revised 1.4 |
103 |
*/ |
|
104 |
public Socket() { |
|
105 |
setImpl(); |
|
106 |
} |
|
107 |
||
108 |
/** |
|
109 |
* Creates an unconnected socket, specifying the type of proxy, if any, |
|
110 |
* that should be used regardless of any other settings. |
|
111 |
* <P> |
|
19069 | 112 |
* If there is a security manager, its {@code checkConnect} method |
2 | 113 |
* is called with the proxy host address and port number |
114 |
* as its arguments. This could result in a SecurityException. |
|
115 |
* <P> |
|
116 |
* Examples: |
|
19069 | 117 |
* <UL> <LI>{@code Socket s = new Socket(Proxy.NO_PROXY);} will create |
2 | 118 |
* a plain socket ignoring any other proxy configuration.</LI> |
19069 | 119 |
* <LI>{@code Socket s = new Socket(new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("socks.mydom.com", 1080)));} |
2 | 120 |
* will create a socket connecting through the specified SOCKS proxy |
121 |
* server.</LI> |
|
122 |
* </UL> |
|
123 |
* |
|
124 |
* @param proxy a {@link java.net.Proxy Proxy} object specifying what kind |
|
125 |
* of proxying should be used. |
|
126 |
* @throws IllegalArgumentException if the proxy is of an invalid type |
|
19069 | 127 |
* or {@code null}. |
2 | 128 |
* @throws SecurityException if a security manager is present and |
129 |
* permission to connect to the proxy is |
|
130 |
* denied. |
|
131 |
* @see java.net.ProxySelector |
|
132 |
* @see java.net.Proxy |
|
133 |
* |
|
134 |
* @since 1.5 |
|
135 |
*/ |
|
136 |
public Socket(Proxy proxy) { |
|
3442
02df74328591
6801497: Proxy is assumed to be immutable but is non-final
jccollet
parents:
475
diff
changeset
|
137 |
// Create a copy of Proxy as a security measure |
02df74328591
6801497: Proxy is assumed to be immutable but is non-final
jccollet
parents:
475
diff
changeset
|
138 |
if (proxy == null) { |
02df74328591
6801497: Proxy is assumed to be immutable but is non-final
jccollet
parents:
475
diff
changeset
|
139 |
throw new IllegalArgumentException("Invalid Proxy"); |
02df74328591
6801497: Proxy is assumed to be immutable but is non-final
jccollet
parents:
475
diff
changeset
|
140 |
} |
16061
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
10596
diff
changeset
|
141 |
Proxy p = proxy == Proxy.NO_PROXY ? Proxy.NO_PROXY |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
10596
diff
changeset
|
142 |
: sun.net.ApplicationProxy.create(proxy); |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
10596
diff
changeset
|
143 |
Proxy.Type type = p.type(); |
c80133bafef0
6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
10596
diff
changeset
|
144 |
if (type == Proxy.Type.SOCKS || type == Proxy.Type.HTTP) { |
2 | 145 |
SecurityManager security = System.getSecurityManager(); |
3442
02df74328591
6801497: Proxy is assumed to be immutable but is non-final
jccollet
parents:
475
diff
changeset
|
146 |
InetSocketAddress epoint = (InetSocketAddress) p.address(); |
5180
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
3450
diff
changeset
|
147 |
if (epoint.getAddress() != null) { |
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
3450
diff
changeset
|
148 |
checkAddress (epoint.getAddress(), "Socket"); |
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
3450
diff
changeset
|
149 |
} |
2 | 150 |
if (security != null) { |
151 |
if (epoint.isUnresolved()) |
|
9775 | 152 |
epoint = new InetSocketAddress(epoint.getHostName(), epoint.getPort()); |
153 |
if (epoint.isUnresolved()) |
|
154 |
security.checkConnect(epoint.getHostName(), epoint.getPort()); |
|
2 | 155 |
else |
156 |
security.checkConnect(epoint.getAddress().getHostAddress(), |
|
9775 | 157 |
epoint.getPort()); |
2 | 158 |
} |
54155
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
159 |
|
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
160 |
// create a SOCKS or HTTP SocketImpl that delegates to a platform SocketImpl |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
161 |
SocketImpl delegate = SocketImpl.createPlatformSocketImpl(false); |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
162 |
impl = (type == Proxy.Type.SOCKS) ? new SocksSocketImpl(p, delegate) |
54689
b28b7f631301
8216978: Drop support for pre JDK 1.4 SocketImpl implementations
michaelm
parents:
54289
diff
changeset
|
163 |
: new HttpConnectSocketImpl(p, delegate, this); |
2 | 164 |
} else { |
3442
02df74328591
6801497: Proxy is assumed to be immutable but is non-final
jccollet
parents:
475
diff
changeset
|
165 |
if (p == Proxy.NO_PROXY) { |
54155
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
166 |
// create a platform or custom SocketImpl for the DIRECT case |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
167 |
SocketImplFactory factory = Socket.factory; |
2 | 168 |
if (factory == null) { |
54155
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
169 |
impl = SocketImpl.createPlatformSocketImpl(false); |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
170 |
} else { |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
171 |
impl = factory.createSocketImpl(); |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
172 |
} |
2 | 173 |
} else |
174 |
throw new IllegalArgumentException("Invalid Proxy"); |
|
175 |
} |
|
176 |
} |
|
177 |
||
178 |
/** |
|
179 |
* Creates an unconnected Socket with a user-specified |
|
180 |
* SocketImpl. |
|
23720
7d5147c21927
8039172: Tidy warnings cleanup for java.net, java.math, java.time, java.rmi
yan
parents:
22339
diff
changeset
|
181 |
* |
2 | 182 |
* @param impl an instance of a <B>SocketImpl</B> |
183 |
* the subclass wishes to use on the Socket. |
|
184 |
* |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55323
diff
changeset
|
185 |
* @throws SocketException if there is an error in the underlying protocol, |
2 | 186 |
* such as a TCP error. |
58610 | 187 |
* |
188 |
* @throws SecurityException if {@code impl} is non-null and a security manager is set |
|
189 |
* and its {@code checkPermission} method doesn't allow {@code NetPermission("setSocketImpl")}. |
|
190 |
* |
|
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
24685
diff
changeset
|
191 |
* @since 1.1 |
2 | 192 |
*/ |
193 |
protected Socket(SocketImpl impl) throws SocketException { |
|
58610 | 194 |
checkPermission(impl); |
2 | 195 |
this.impl = impl; |
196 |
} |
|
197 |
||
58610 | 198 |
private static Void checkPermission(SocketImpl impl) { |
199 |
if (impl == null) { |
|
200 |
return null; |
|
201 |
} |
|
202 |
SecurityManager sm = System.getSecurityManager(); |
|
203 |
if (sm != null) { |
|
204 |
sm.checkPermission(SecurityConstants.SET_SOCKETIMPL_PERMISSION); |
|
205 |
} |
|
206 |
return null; |
|
207 |
} |
|
208 |
||
2 | 209 |
/** |
210 |
* Creates a stream socket and connects it to the specified port |
|
211 |
* number on the named host. |
|
212 |
* <p> |
|
19069 | 213 |
* If the specified host is {@code null} it is the equivalent of |
214 |
* specifying the address as |
|
215 |
* {@link java.net.InetAddress#getByName InetAddress.getByName}{@code (null)}. |
|
2 | 216 |
* In other words, it is equivalent to specifying an address of the |
217 |
* loopback interface. </p> |
|
218 |
* <p> |
|
55323
fc4042870cc4
8225426: Replace plain with system-default in Socket and ServerSocket
chegar
parents:
55081
diff
changeset
|
219 |
* If the application has specified a client socket implementation |
fc4042870cc4
8225426: Replace plain with system-default in Socket and ServerSocket
chegar
parents:
55081
diff
changeset
|
220 |
* factory, that factory's {@code createSocketImpl} method is called to |
fc4042870cc4
8225426: Replace plain with system-default in Socket and ServerSocket
chegar
parents:
55081
diff
changeset
|
221 |
* create the actual socket implementation. Otherwise a system-default |
fc4042870cc4
8225426: Replace plain with system-default in Socket and ServerSocket
chegar
parents:
55081
diff
changeset
|
222 |
* socket implementation is created. |
2 | 223 |
* <p> |
224 |
* If there is a security manager, its |
|
19069 | 225 |
* {@code checkConnect} method is called |
226 |
* with the host address and {@code port} |
|
2 | 227 |
* as its arguments. This could result in a SecurityException. |
228 |
* |
|
19069 | 229 |
* @param host the host name, or {@code null} for the loopback address. |
2 | 230 |
* @param port the port number. |
231 |
* |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55323
diff
changeset
|
232 |
* @throws UnknownHostException if the IP address of |
2 | 233 |
* the host could not be determined. |
234 |
* |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55323
diff
changeset
|
235 |
* @throws IOException if an I/O error occurs when creating the socket. |
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55323
diff
changeset
|
236 |
* @throws SecurityException if a security manager exists and its |
19069 | 237 |
* {@code checkConnect} method doesn't allow the operation. |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55323
diff
changeset
|
238 |
* @throws IllegalArgumentException if the port parameter is outside |
2 | 239 |
* the specified range of valid port values, which is between |
240 |
* 0 and 65535, inclusive. |
|
241 |
* @see java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory) |
|
242 |
* @see java.net.SocketImpl |
|
243 |
* @see java.net.SocketImplFactory#createSocketImpl() |
|
244 |
* @see SecurityManager#checkConnect |
|
245 |
*/ |
|
246 |
public Socket(String host, int port) |
|
247 |
throws UnknownHostException, IOException |
|
248 |
{ |
|
249 |
this(host != null ? new InetSocketAddress(host, port) : |
|
250 |
new InetSocketAddress(InetAddress.getByName(null), port), |
|
251 |
(SocketAddress) null, true); |
|
252 |
} |
|
253 |
||
254 |
/** |
|
255 |
* Creates a stream socket and connects it to the specified port |
|
256 |
* number at the specified IP address. |
|
257 |
* <p> |
|
55323
fc4042870cc4
8225426: Replace plain with system-default in Socket and ServerSocket
chegar
parents:
55081
diff
changeset
|
258 |
* If the application has specified a client socket implementation |
fc4042870cc4
8225426: Replace plain with system-default in Socket and ServerSocket
chegar
parents:
55081
diff
changeset
|
259 |
* factory, that factory's {@code createSocketImpl} method is called to |
fc4042870cc4
8225426: Replace plain with system-default in Socket and ServerSocket
chegar
parents:
55081
diff
changeset
|
260 |
* create the actual socket implementation. Otherwise a system-default |
fc4042870cc4
8225426: Replace plain with system-default in Socket and ServerSocket
chegar
parents:
55081
diff
changeset
|
261 |
* socket implementation is created. |
2 | 262 |
* <p> |
263 |
* If there is a security manager, its |
|
19069 | 264 |
* {@code checkConnect} method is called |
265 |
* with the host address and {@code port} |
|
2 | 266 |
* as its arguments. This could result in a SecurityException. |
267 |
* |
|
268 |
* @param address the IP address. |
|
269 |
* @param port the port number. |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55323
diff
changeset
|
270 |
* @throws IOException if an I/O error occurs when creating the socket. |
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55323
diff
changeset
|
271 |
* @throws SecurityException if a security manager exists and its |
19069 | 272 |
* {@code checkConnect} method doesn't allow the operation. |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55323
diff
changeset
|
273 |
* @throws IllegalArgumentException if the port parameter is outside |
2 | 274 |
* the specified range of valid port values, which is between |
275 |
* 0 and 65535, inclusive. |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55323
diff
changeset
|
276 |
* @throws NullPointerException if {@code address} is null. |
2 | 277 |
* @see java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory) |
278 |
* @see java.net.SocketImpl |
|
279 |
* @see java.net.SocketImplFactory#createSocketImpl() |
|
280 |
* @see SecurityManager#checkConnect |
|
281 |
*/ |
|
282 |
public Socket(InetAddress address, int port) throws IOException { |
|
283 |
this(address != null ? new InetSocketAddress(address, port) : null, |
|
284 |
(SocketAddress) null, true); |
|
285 |
} |
|
286 |
||
287 |
/** |
|
288 |
* Creates a socket and connects it to the specified remote host on |
|
289 |
* the specified remote port. The Socket will also bind() to the local |
|
290 |
* address and port supplied. |
|
291 |
* <p> |
|
19069 | 292 |
* If the specified host is {@code null} it is the equivalent of |
293 |
* specifying the address as |
|
294 |
* {@link java.net.InetAddress#getByName InetAddress.getByName}{@code (null)}. |
|
2 | 295 |
* In other words, it is equivalent to specifying an address of the |
296 |
* loopback interface. </p> |
|
297 |
* <p> |
|
19069 | 298 |
* A local port number of {@code zero} will let the system pick up a |
299 |
* free port in the {@code bind} operation.</p> |
|
2 | 300 |
* <p> |
301 |
* If there is a security manager, its |
|
19069 | 302 |
* {@code checkConnect} method is called |
303 |
* with the host address and {@code port} |
|
2 | 304 |
* as its arguments. This could result in a SecurityException. |
305 |
* |
|
19069 | 306 |
* @param host the name of the remote host, or {@code null} for the loopback address. |
2 | 307 |
* @param port the remote port |
475
b8b79ef97ac8
6571950: SSLSocket(raddr, rport, laddr, lport) allows null as laddr that spec doesn't reflect
xuelei
parents:
94
diff
changeset
|
308 |
* @param localAddr the local address the socket is bound to, or |
19069 | 309 |
* {@code null} for the {@code anyLocal} address. |
2 | 310 |
* @param localPort the local port the socket is bound to, or |
19069 | 311 |
* {@code zero} for a system selected free port. |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55323
diff
changeset
|
312 |
* @throws IOException if an I/O error occurs when creating the socket. |
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55323
diff
changeset
|
313 |
* @throws SecurityException if a security manager exists and its |
22339 | 314 |
* {@code checkConnect} method doesn't allow the connection |
315 |
* to the destination, or if its {@code checkListen} method |
|
316 |
* doesn't allow the bind to the local port. |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55323
diff
changeset
|
317 |
* @throws IllegalArgumentException if the port parameter or localPort |
2 | 318 |
* parameter is outside the specified range of valid port values, |
319 |
* which is between 0 and 65535, inclusive. |
|
320 |
* @see SecurityManager#checkConnect |
|
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
24685
diff
changeset
|
321 |
* @since 1.1 |
2 | 322 |
*/ |
323 |
public Socket(String host, int port, InetAddress localAddr, |
|
324 |
int localPort) throws IOException { |
|
325 |
this(host != null ? new InetSocketAddress(host, port) : |
|
326 |
new InetSocketAddress(InetAddress.getByName(null), port), |
|
327 |
new InetSocketAddress(localAddr, localPort), true); |
|
328 |
} |
|
329 |
||
330 |
/** |
|
331 |
* Creates a socket and connects it to the specified remote address on |
|
332 |
* the specified remote port. The Socket will also bind() to the local |
|
333 |
* address and port supplied. |
|
334 |
* <p> |
|
19069 | 335 |
* If the specified local address is {@code null} it is the equivalent of |
336 |
* specifying the address as the AnyLocal address |
|
337 |
* (see {@link java.net.InetAddress#isAnyLocalAddress InetAddress.isAnyLocalAddress}{@code ()}). |
|
2 | 338 |
* <p> |
19069 | 339 |
* A local port number of {@code zero} will let the system pick up a |
340 |
* free port in the {@code bind} operation.</p> |
|
2 | 341 |
* <p> |
342 |
* If there is a security manager, its |
|
19069 | 343 |
* {@code checkConnect} method is called |
344 |
* with the host address and {@code port} |
|
2 | 345 |
* as its arguments. This could result in a SecurityException. |
346 |
* |
|
347 |
* @param address the remote address |
|
348 |
* @param port the remote port |
|
349 |
* @param localAddr the local address the socket is bound to, or |
|
19069 | 350 |
* {@code null} for the {@code anyLocal} address. |
2 | 351 |
* @param localPort the local port the socket is bound to or |
19069 | 352 |
* {@code zero} for a system selected free port. |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55323
diff
changeset
|
353 |
* @throws IOException if an I/O error occurs when creating the socket. |
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55323
diff
changeset
|
354 |
* @throws SecurityException if a security manager exists and its |
22339 | 355 |
* {@code checkConnect} method doesn't allow the connection |
356 |
* to the destination, or if its {@code checkListen} method |
|
357 |
* doesn't allow the bind to the local port. |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55323
diff
changeset
|
358 |
* @throws IllegalArgumentException if the port parameter or localPort |
2 | 359 |
* parameter is outside the specified range of valid port values, |
360 |
* which is between 0 and 65535, inclusive. |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55323
diff
changeset
|
361 |
* @throws NullPointerException if {@code address} is null. |
2 | 362 |
* @see SecurityManager#checkConnect |
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
24685
diff
changeset
|
363 |
* @since 1.1 |
2 | 364 |
*/ |
365 |
public Socket(InetAddress address, int port, InetAddress localAddr, |
|
366 |
int localPort) throws IOException { |
|
367 |
this(address != null ? new InetSocketAddress(address, port) : null, |
|
368 |
new InetSocketAddress(localAddr, localPort), true); |
|
369 |
} |
|
370 |
||
371 |
/** |
|
372 |
* Creates a stream socket and connects it to the specified port |
|
373 |
* number on the named host. |
|
374 |
* <p> |
|
19069 | 375 |
* If the specified host is {@code null} it is the equivalent of |
376 |
* specifying the address as |
|
377 |
* {@link java.net.InetAddress#getByName InetAddress.getByName}{@code (null)}. |
|
2 | 378 |
* In other words, it is equivalent to specifying an address of the |
379 |
* loopback interface. </p> |
|
380 |
* <p> |
|
19069 | 381 |
* If the stream argument is {@code true}, this creates a |
382 |
* stream socket. If the stream argument is {@code false}, it |
|
2 | 383 |
* creates a datagram socket. |
384 |
* <p> |
|
55323
fc4042870cc4
8225426: Replace plain with system-default in Socket and ServerSocket
chegar
parents:
55081
diff
changeset
|
385 |
* If the application has specified a client socket implementation |
fc4042870cc4
8225426: Replace plain with system-default in Socket and ServerSocket
chegar
parents:
55081
diff
changeset
|
386 |
* factory, that factory's {@code createSocketImpl} method is called to |
fc4042870cc4
8225426: Replace plain with system-default in Socket and ServerSocket
chegar
parents:
55081
diff
changeset
|
387 |
* create the actual socket implementation. Otherwise a system-default |
fc4042870cc4
8225426: Replace plain with system-default in Socket and ServerSocket
chegar
parents:
55081
diff
changeset
|
388 |
* socket implementation is created. |
2 | 389 |
* <p> |
390 |
* If there is a security manager, its |
|
19069 | 391 |
* {@code checkConnect} method is called |
392 |
* with the host address and {@code port} |
|
2 | 393 |
* as its arguments. This could result in a SecurityException. |
394 |
* <p> |
|
395 |
* If a UDP socket is used, TCP/IP related socket options will not apply. |
|
396 |
* |
|
19069 | 397 |
* @param host the host name, or {@code null} for the loopback address. |
2 | 398 |
* @param port the port number. |
19069 | 399 |
* @param stream a {@code boolean} indicating whether this is |
2 | 400 |
* a stream socket or a datagram socket. |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55323
diff
changeset
|
401 |
* @throws IOException if an I/O error occurs when creating the socket. |
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55323
diff
changeset
|
402 |
* @throws SecurityException if a security manager exists and its |
19069 | 403 |
* {@code checkConnect} method doesn't allow the operation. |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55323
diff
changeset
|
404 |
* @throws IllegalArgumentException if the port parameter is outside |
2 | 405 |
* the specified range of valid port values, which is between |
406 |
* 0 and 65535, inclusive. |
|
407 |
* @see java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory) |
|
408 |
* @see java.net.SocketImpl |
|
409 |
* @see java.net.SocketImplFactory#createSocketImpl() |
|
410 |
* @see SecurityManager#checkConnect |
|
411 |
* @deprecated Use DatagramSocket instead for UDP transport. |
|
412 |
*/ |
|
413 |
@Deprecated |
|
414 |
public Socket(String host, int port, boolean stream) throws IOException { |
|
415 |
this(host != null ? new InetSocketAddress(host, port) : |
|
416 |
new InetSocketAddress(InetAddress.getByName(null), port), |
|
417 |
(SocketAddress) null, stream); |
|
418 |
} |
|
419 |
||
420 |
/** |
|
421 |
* Creates a socket and connects it to the specified port number at |
|
422 |
* the specified IP address. |
|
423 |
* <p> |
|
19069 | 424 |
* If the stream argument is {@code true}, this creates a |
425 |
* stream socket. If the stream argument is {@code false}, it |
|
2 | 426 |
* creates a datagram socket. |
427 |
* <p> |
|
55323
fc4042870cc4
8225426: Replace plain with system-default in Socket and ServerSocket
chegar
parents:
55081
diff
changeset
|
428 |
* If the application has specified a client socket implementation |
fc4042870cc4
8225426: Replace plain with system-default in Socket and ServerSocket
chegar
parents:
55081
diff
changeset
|
429 |
* factory, that factory's {@code createSocketImpl} method is called to |
fc4042870cc4
8225426: Replace plain with system-default in Socket and ServerSocket
chegar
parents:
55081
diff
changeset
|
430 |
* create the actual socket implementation. Otherwise a system-default |
fc4042870cc4
8225426: Replace plain with system-default in Socket and ServerSocket
chegar
parents:
55081
diff
changeset
|
431 |
* socket implementation is created. |
2 | 432 |
* |
433 |
* <p>If there is a security manager, its |
|
19069 | 434 |
* {@code checkConnect} method is called |
435 |
* with {@code host.getHostAddress()} and {@code port} |
|
2 | 436 |
* as its arguments. This could result in a SecurityException. |
437 |
* <p> |
|
438 |
* If UDP socket is used, TCP/IP related socket options will not apply. |
|
439 |
* |
|
440 |
* @param host the IP address. |
|
441 |
* @param port the port number. |
|
19069 | 442 |
* @param stream if {@code true}, create a stream socket; |
2 | 443 |
* otherwise, create a datagram socket. |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55323
diff
changeset
|
444 |
* @throws IOException if an I/O error occurs when creating the socket. |
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55323
diff
changeset
|
445 |
* @throws SecurityException if a security manager exists and its |
19069 | 446 |
* {@code checkConnect} method doesn't allow the operation. |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55323
diff
changeset
|
447 |
* @throws IllegalArgumentException if the port parameter is outside |
2 | 448 |
* the specified range of valid port values, which is between |
449 |
* 0 and 65535, inclusive. |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55323
diff
changeset
|
450 |
* @throws NullPointerException if {@code host} is null. |
2 | 451 |
* @see java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory) |
452 |
* @see java.net.SocketImpl |
|
453 |
* @see java.net.SocketImplFactory#createSocketImpl() |
|
454 |
* @see SecurityManager#checkConnect |
|
455 |
* @deprecated Use DatagramSocket instead for UDP transport. |
|
456 |
*/ |
|
457 |
@Deprecated |
|
458 |
public Socket(InetAddress host, int port, boolean stream) throws IOException { |
|
459 |
this(host != null ? new InetSocketAddress(host, port) : null, |
|
460 |
new InetSocketAddress(0), stream); |
|
461 |
} |
|
462 |
||
463 |
private Socket(SocketAddress address, SocketAddress localAddr, |
|
464 |
boolean stream) throws IOException { |
|
465 |
setImpl(); |
|
466 |
||
467 |
// backward compatibility |
|
468 |
if (address == null) |
|
469 |
throw new NullPointerException(); |
|
470 |
||
471 |
try { |
|
472 |
createImpl(stream); |
|
473 |
if (localAddr != null) |
|
474 |
bind(localAddr); |
|
10439 | 475 |
connect(address); |
476 |
} catch (IOException | IllegalArgumentException | SecurityException e) { |
|
477 |
try { |
|
478 |
close(); |
|
479 |
} catch (IOException ce) { |
|
480 |
e.addSuppressed(ce); |
|
481 |
} |
|
2 | 482 |
throw e; |
483 |
} |
|
484 |
} |
|
485 |
||
486 |
/** |
|
487 |
* Creates the socket implementation. |
|
488 |
* |
|
19069 | 489 |
* @param stream a {@code boolean} value : {@code true} for a TCP socket, |
490 |
* {@code false} for UDP. |
|
2 | 491 |
* @throws IOException if creation fails |
492 |
* @since 1.4 |
|
493 |
*/ |
|
494 |
void createImpl(boolean stream) throws SocketException { |
|
495 |
if (impl == null) |
|
496 |
setImpl(); |
|
497 |
try { |
|
498 |
impl.create(stream); |
|
499 |
created = true; |
|
500 |
} catch (IOException e) { |
|
501 |
throw new SocketException(e.getMessage()); |
|
502 |
} |
|
503 |
} |
|
504 |
||
54155
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
505 |
void setImpl(SocketImpl si) { |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
506 |
impl = si; |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
507 |
} |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
508 |
|
2 | 509 |
/** |
510 |
* Sets impl to the system-default type of SocketImpl. |
|
511 |
* @since 1.4 |
|
512 |
*/ |
|
513 |
void setImpl() { |
|
54155
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
514 |
SocketImplFactory factory = Socket.factory; |
2 | 515 |
if (factory != null) { |
516 |
impl = factory.createSocketImpl(); |
|
517 |
} else { |
|
54155
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
518 |
// create a SOCKS SocketImpl that delegates to a platform SocketImpl |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
519 |
SocketImpl delegate = SocketImpl.createPlatformSocketImpl(false); |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
520 |
impl = new SocksSocketImpl(delegate); |
2 | 521 |
} |
522 |
} |
|
523 |
||
524 |
/** |
|
19069 | 525 |
* Get the {@code SocketImpl} attached to this socket, creating |
2 | 526 |
* it if necessary. |
527 |
* |
|
19069 | 528 |
* @return the {@code SocketImpl} attached to that ServerSocket. |
2 | 529 |
* @throws SocketException if creation fails |
530 |
* @since 1.4 |
|
531 |
*/ |
|
532 |
SocketImpl getImpl() throws SocketException { |
|
533 |
if (!created) |
|
534 |
createImpl(true); |
|
535 |
return impl; |
|
536 |
} |
|
537 |
||
538 |
/** |
|
539 |
* Connects this socket to the server. |
|
540 |
* |
|
19069 | 541 |
* @param endpoint the {@code SocketAddress} |
2 | 542 |
* @throws IOException if an error occurs during the connection |
543 |
* @throws java.nio.channels.IllegalBlockingModeException |
|
544 |
* if this socket has an associated channel, |
|
545 |
* and the channel is in non-blocking mode |
|
546 |
* @throws IllegalArgumentException if endpoint is null or is a |
|
547 |
* SocketAddress subclass not supported by this socket |
|
548 |
* @since 1.4 |
|
549 |
* @spec JSR-51 |
|
550 |
*/ |
|
551 |
public void connect(SocketAddress endpoint) throws IOException { |
|
552 |
connect(endpoint, 0); |
|
553 |
} |
|
554 |
||
555 |
/** |
|
556 |
* Connects this socket to the server with a specified timeout value. |
|
557 |
* A timeout of zero is interpreted as an infinite timeout. The connection |
|
558 |
* will then block until established or an error occurs. |
|
559 |
* |
|
19069 | 560 |
* @param endpoint the {@code SocketAddress} |
2 | 561 |
* @param timeout the timeout value to be used in milliseconds. |
562 |
* @throws IOException if an error occurs during the connection |
|
563 |
* @throws SocketTimeoutException if timeout expires before connecting |
|
564 |
* @throws java.nio.channels.IllegalBlockingModeException |
|
565 |
* if this socket has an associated channel, |
|
566 |
* and the channel is in non-blocking mode |
|
567 |
* @throws IllegalArgumentException if endpoint is null or is a |
|
54289
6183f835b9b6
8219446: Specify behaviour of timeout accepting methods of Socket and ServerSocket if timeout is negative
chegar
parents:
54155
diff
changeset
|
568 |
* SocketAddress subclass not supported by this socket, or |
6183f835b9b6
8219446: Specify behaviour of timeout accepting methods of Socket and ServerSocket if timeout is negative
chegar
parents:
54155
diff
changeset
|
569 |
* if {@code timeout} is negative |
2 | 570 |
* @since 1.4 |
571 |
* @spec JSR-51 |
|
572 |
*/ |
|
573 |
public void connect(SocketAddress endpoint, int timeout) throws IOException { |
|
574 |
if (endpoint == null) |
|
575 |
throw new IllegalArgumentException("connect: The address can't be null"); |
|
576 |
||
577 |
if (timeout < 0) |
|
578 |
throw new IllegalArgumentException("connect: timeout can't be negative"); |
|
579 |
||
580 |
if (isClosed()) |
|
581 |
throw new SocketException("Socket is closed"); |
|
582 |
||
54689
b28b7f631301
8216978: Drop support for pre JDK 1.4 SocketImpl implementations
michaelm
parents:
54289
diff
changeset
|
583 |
if (isConnected()) |
2 | 584 |
throw new SocketException("already connected"); |
585 |
||
586 |
if (!(endpoint instanceof InetSocketAddress)) |
|
587 |
throw new IllegalArgumentException("Unsupported address type"); |
|
588 |
||
589 |
InetSocketAddress epoint = (InetSocketAddress) endpoint; |
|
5180
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
3450
diff
changeset
|
590 |
InetAddress addr = epoint.getAddress (); |
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
3450
diff
changeset
|
591 |
int port = epoint.getPort(); |
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
3450
diff
changeset
|
592 |
checkAddress(addr, "connect"); |
2 | 593 |
|
594 |
SecurityManager security = System.getSecurityManager(); |
|
595 |
if (security != null) { |
|
596 |
if (epoint.isUnresolved()) |
|
5180
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
3450
diff
changeset
|
597 |
security.checkConnect(epoint.getHostName(), port); |
2 | 598 |
else |
5180
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
3450
diff
changeset
|
599 |
security.checkConnect(addr.getHostAddress(), port); |
2 | 600 |
} |
601 |
if (!created) |
|
602 |
createImpl(true); |
|
54689
b28b7f631301
8216978: Drop support for pre JDK 1.4 SocketImpl implementations
michaelm
parents:
54289
diff
changeset
|
603 |
impl.connect(epoint, timeout); |
2 | 604 |
connected = true; |
605 |
/* |
|
606 |
* If the socket was not bound before the connect, it is now because |
|
607 |
* the kernel will have picked an ephemeral port & a local address |
|
608 |
*/ |
|
609 |
bound = true; |
|
610 |
} |
|
611 |
||
612 |
/** |
|
613 |
* Binds the socket to a local address. |
|
614 |
* <P> |
|
19069 | 615 |
* If the address is {@code null}, then the system will pick up |
2 | 616 |
* an ephemeral port and a valid local address to bind the socket. |
617 |
* |
|
19069 | 618 |
* @param bindpoint the {@code SocketAddress} to bind to |
2 | 619 |
* @throws IOException if the bind operation fails, or if the socket |
620 |
* is already bound. |
|
621 |
* @throws IllegalArgumentException if bindpoint is a |
|
622 |
* SocketAddress subclass not supported by this socket |
|
22339 | 623 |
* @throws SecurityException if a security manager exists and its |
624 |
* {@code checkListen} method doesn't allow the bind |
|
625 |
* to the local port. |
|
2 | 626 |
* |
627 |
* @since 1.4 |
|
628 |
* @see #isBound |
|
629 |
*/ |
|
630 |
public void bind(SocketAddress bindpoint) throws IOException { |
|
631 |
if (isClosed()) |
|
632 |
throw new SocketException("Socket is closed"); |
|
54689
b28b7f631301
8216978: Drop support for pre JDK 1.4 SocketImpl implementations
michaelm
parents:
54289
diff
changeset
|
633 |
if (isBound()) |
2 | 634 |
throw new SocketException("Already bound"); |
635 |
||
636 |
if (bindpoint != null && (!(bindpoint instanceof InetSocketAddress))) |
|
637 |
throw new IllegalArgumentException("Unsupported address type"); |
|
638 |
InetSocketAddress epoint = (InetSocketAddress) bindpoint; |
|
639 |
if (epoint != null && epoint.isUnresolved()) |
|
640 |
throw new SocketException("Unresolved address"); |
|
5180
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
3450
diff
changeset
|
641 |
if (epoint == null) { |
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
3450
diff
changeset
|
642 |
epoint = new InetSocketAddress(0); |
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
3450
diff
changeset
|
643 |
} |
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
3450
diff
changeset
|
644 |
InetAddress addr = epoint.getAddress(); |
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
3450
diff
changeset
|
645 |
int port = epoint.getPort(); |
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
3450
diff
changeset
|
646 |
checkAddress (addr, "bind"); |
22339 | 647 |
SecurityManager security = System.getSecurityManager(); |
648 |
if (security != null) { |
|
649 |
security.checkListen(port); |
|
650 |
} |
|
5180
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
3450
diff
changeset
|
651 |
getImpl().bind (addr, port); |
2 | 652 |
bound = true; |
653 |
} |
|
654 |
||
5180
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
3450
diff
changeset
|
655 |
private void checkAddress (InetAddress addr, String op) { |
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
3450
diff
changeset
|
656 |
if (addr == null) { |
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
3450
diff
changeset
|
657 |
return; |
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
3450
diff
changeset
|
658 |
} |
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
3450
diff
changeset
|
659 |
if (!(addr instanceof Inet4Address || addr instanceof Inet6Address)) { |
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
3450
diff
changeset
|
660 |
throw new IllegalArgumentException(op + ": invalid address type"); |
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
3450
diff
changeset
|
661 |
} |
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
3450
diff
changeset
|
662 |
} |
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
3450
diff
changeset
|
663 |
|
2 | 664 |
/** |
665 |
* set the flags after an accept() call. |
|
666 |
*/ |
|
667 |
final void postAccept() { |
|
668 |
connected = true; |
|
669 |
created = true; |
|
670 |
bound = true; |
|
671 |
} |
|
672 |
||
673 |
/** |
|
674 |
* Returns the address to which the socket is connected. |
|
675 |
* <p> |
|
676 |
* If the socket was connected prior to being {@link #close closed}, |
|
677 |
* then this method will continue to return the connected address |
|
678 |
* after the socket is closed. |
|
679 |
* |
|
680 |
* @return the remote IP address to which this socket is connected, |
|
19069 | 681 |
* or {@code null} if the socket is not connected. |
2 | 682 |
*/ |
683 |
public InetAddress getInetAddress() { |
|
684 |
if (!isConnected()) |
|
685 |
return null; |
|
686 |
try { |
|
687 |
return getImpl().getInetAddress(); |
|
688 |
} catch (SocketException e) { |
|
689 |
} |
|
690 |
return null; |
|
691 |
} |
|
692 |
||
693 |
/** |
|
694 |
* Gets the local address to which the socket is bound. |
|
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
16061
diff
changeset
|
695 |
* <p> |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
16061
diff
changeset
|
696 |
* If there is a security manager set, its {@code checkConnect} method is |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
16061
diff
changeset
|
697 |
* called with the local address and {@code -1} as its arguments to see |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
16061
diff
changeset
|
698 |
* if the operation is allowed. If the operation is not allowed, |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
16061
diff
changeset
|
699 |
* the {@link InetAddress#getLoopbackAddress loopback} address is returned. |
2 | 700 |
* |
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
16061
diff
changeset
|
701 |
* @return the local address to which the socket is bound, |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
16061
diff
changeset
|
702 |
* the loopback address if denied by the security manager, or |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
16061
diff
changeset
|
703 |
* the wildcard address if the socket is closed or not bound yet. |
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
24685
diff
changeset
|
704 |
* @since 1.1 |
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
16061
diff
changeset
|
705 |
* |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
16061
diff
changeset
|
706 |
* @see SecurityManager#checkConnect |
2 | 707 |
*/ |
708 |
public InetAddress getLocalAddress() { |
|
709 |
// This is for backward compatibility |
|
710 |
if (!isBound()) |
|
711 |
return InetAddress.anyLocalAddress(); |
|
712 |
InetAddress in = null; |
|
713 |
try { |
|
714 |
in = (InetAddress) getImpl().getOption(SocketOptions.SO_BINDADDR); |
|
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
16061
diff
changeset
|
715 |
SecurityManager sm = System.getSecurityManager(); |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
16061
diff
changeset
|
716 |
if (sm != null) |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
16061
diff
changeset
|
717 |
sm.checkConnect(in.getHostAddress(), -1); |
2 | 718 |
if (in.isAnyLocalAddress()) { |
719 |
in = InetAddress.anyLocalAddress(); |
|
720 |
} |
|
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
16061
diff
changeset
|
721 |
} catch (SecurityException e) { |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
16061
diff
changeset
|
722 |
in = InetAddress.getLoopbackAddress(); |
2 | 723 |
} catch (Exception e) { |
724 |
in = InetAddress.anyLocalAddress(); // "0.0.0.0" |
|
725 |
} |
|
726 |
return in; |
|
727 |
} |
|
728 |
||
729 |
/** |
|
730 |
* Returns the remote port number to which this socket is connected. |
|
731 |
* <p> |
|
732 |
* If the socket was connected prior to being {@link #close closed}, |
|
733 |
* then this method will continue to return the connected port number |
|
734 |
* after the socket is closed. |
|
735 |
* |
|
736 |
* @return the remote port number to which this socket is connected, or |
|
737 |
* 0 if the socket is not connected yet. |
|
738 |
*/ |
|
739 |
public int getPort() { |
|
740 |
if (!isConnected()) |
|
741 |
return 0; |
|
742 |
try { |
|
743 |
return getImpl().getPort(); |
|
744 |
} catch (SocketException e) { |
|
745 |
// Shouldn't happen as we're connected |
|
746 |
} |
|
747 |
return -1; |
|
748 |
} |
|
749 |
||
750 |
/** |
|
751 |
* Returns the local port number to which this socket is bound. |
|
752 |
* <p> |
|
753 |
* If the socket was bound prior to being {@link #close closed}, |
|
754 |
* then this method will continue to return the local port number |
|
755 |
* after the socket is closed. |
|
756 |
* |
|
757 |
* @return the local port number to which this socket is bound or -1 |
|
758 |
* if the socket is not bound yet. |
|
759 |
*/ |
|
760 |
public int getLocalPort() { |
|
761 |
if (!isBound()) |
|
762 |
return -1; |
|
763 |
try { |
|
764 |
return getImpl().getLocalPort(); |
|
765 |
} catch(SocketException e) { |
|
766 |
// shouldn't happen as we're bound |
|
767 |
} |
|
768 |
return -1; |
|
769 |
} |
|
770 |
||
771 |
/** |
|
772 |
* Returns the address of the endpoint this socket is connected to, or |
|
19069 | 773 |
* {@code null} if it is unconnected. |
2 | 774 |
* <p> |
775 |
* If the socket was connected prior to being {@link #close closed}, |
|
776 |
* then this method will continue to return the connected address |
|
777 |
* after the socket is closed. |
|
778 |
* |
|
94 | 779 |
|
19069 | 780 |
* @return a {@code SocketAddress} representing the remote endpoint of this |
781 |
* socket, or {@code null} if it is not connected yet. |
|
2 | 782 |
* @see #getInetAddress() |
783 |
* @see #getPort() |
|
784 |
* @see #connect(SocketAddress, int) |
|
785 |
* @see #connect(SocketAddress) |
|
786 |
* @since 1.4 |
|
787 |
*/ |
|
788 |
public SocketAddress getRemoteSocketAddress() { |
|
789 |
if (!isConnected()) |
|
790 |
return null; |
|
791 |
return new InetSocketAddress(getInetAddress(), getPort()); |
|
792 |
} |
|
793 |
||
794 |
/** |
|
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
16061
diff
changeset
|
795 |
* Returns the address of the endpoint this socket is bound to. |
2 | 796 |
* <p> |
797 |
* If a socket bound to an endpoint represented by an |
|
19069 | 798 |
* {@code InetSocketAddress } is {@link #close closed}, |
799 |
* then this method will continue to return an {@code InetSocketAddress} |
|
2 | 800 |
* after the socket is closed. In that case the returned |
19069 | 801 |
* {@code InetSocketAddress}'s address is the |
2 | 802 |
* {@link InetAddress#isAnyLocalAddress wildcard} address |
803 |
* and its port is the local port that it was bound to. |
|
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
16061
diff
changeset
|
804 |
* <p> |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
16061
diff
changeset
|
805 |
* If there is a security manager set, its {@code checkConnect} method is |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
16061
diff
changeset
|
806 |
* called with the local address and {@code -1} as its arguments to see |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
16061
diff
changeset
|
807 |
* if the operation is allowed. If the operation is not allowed, |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
16061
diff
changeset
|
808 |
* a {@code SocketAddress} representing the |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
16061
diff
changeset
|
809 |
* {@link InetAddress#getLoopbackAddress loopback} address and the local |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
16061
diff
changeset
|
810 |
* port to which this socket is bound is returned. |
2 | 811 |
* |
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
16061
diff
changeset
|
812 |
* @return a {@code SocketAddress} representing the local endpoint of |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
16061
diff
changeset
|
813 |
* this socket, or a {@code SocketAddress} representing the |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
16061
diff
changeset
|
814 |
* loopback address if denied by the security manager, or |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
16061
diff
changeset
|
815 |
* {@code null} if the socket is not bound yet. |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
16061
diff
changeset
|
816 |
* |
2 | 817 |
* @see #getLocalAddress() |
818 |
* @see #getLocalPort() |
|
819 |
* @see #bind(SocketAddress) |
|
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
16061
diff
changeset
|
820 |
* @see SecurityManager#checkConnect |
2 | 821 |
* @since 1.4 |
822 |
*/ |
|
823 |
||
824 |
public SocketAddress getLocalSocketAddress() { |
|
825 |
if (!isBound()) |
|
826 |
return null; |
|
827 |
return new InetSocketAddress(getLocalAddress(), getLocalPort()); |
|
828 |
} |
|
829 |
||
830 |
/** |
|
831 |
* Returns the unique {@link java.nio.channels.SocketChannel SocketChannel} |
|
832 |
* object associated with this socket, if any. |
|
833 |
* |
|
834 |
* <p> A socket will have a channel if, and only if, the channel itself was |
|
835 |
* created via the {@link java.nio.channels.SocketChannel#open |
|
836 |
* SocketChannel.open} or {@link |
|
837 |
* java.nio.channels.ServerSocketChannel#accept ServerSocketChannel.accept} |
|
838 |
* methods. |
|
839 |
* |
|
840 |
* @return the socket channel associated with this socket, |
|
19069 | 841 |
* or {@code null} if this socket was not created |
2 | 842 |
* for a channel |
843 |
* |
|
844 |
* @since 1.4 |
|
845 |
* @spec JSR-51 |
|
846 |
*/ |
|
847 |
public SocketChannel getChannel() { |
|
848 |
return null; |
|
849 |
} |
|
850 |
||
851 |
/** |
|
852 |
* Returns an input stream for this socket. |
|
853 |
* |
|
854 |
* <p> If this socket has an associated channel then the resulting input |
|
855 |
* stream delegates all of its operations to the channel. If the channel |
|
19069 | 856 |
* is in non-blocking mode then the input stream's {@code read} operations |
2 | 857 |
* will throw an {@link java.nio.channels.IllegalBlockingModeException}. |
858 |
* |
|
859 |
* <p>Under abnormal conditions the underlying connection may be |
|
860 |
* broken by the remote host or the network software (for example |
|
861 |
* a connection reset in the case of TCP connections). When a |
|
862 |
* broken connection is detected by the network software the |
|
863 |
* following applies to the returned input stream :- |
|
864 |
* |
|
865 |
* <ul> |
|
866 |
* |
|
867 |
* <li><p>The network software may discard bytes that are buffered |
|
868 |
* by the socket. Bytes that aren't discarded by the network |
|
869 |
* software can be read using {@link java.io.InputStream#read read}. |
|
870 |
* |
|
871 |
* <li><p>If there are no bytes buffered on the socket, or all |
|
872 |
* buffered bytes have been consumed by |
|
873 |
* {@link java.io.InputStream#read read}, then all subsequent |
|
874 |
* calls to {@link java.io.InputStream#read read} will throw an |
|
875 |
* {@link java.io.IOException IOException}. |
|
876 |
* |
|
877 |
* <li><p>If there are no bytes buffered on the socket, and the |
|
878 |
* socket has not been closed using {@link #close close}, then |
|
879 |
* {@link java.io.InputStream#available available} will |
|
19069 | 880 |
* return {@code 0}. |
2 | 881 |
* |
882 |
* </ul> |
|
883 |
* |
|
884 |
* <p> Closing the returned {@link java.io.InputStream InputStream} |
|
885 |
* will close the associated socket. |
|
886 |
* |
|
887 |
* @return an input stream for reading bytes from this socket. |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55323
diff
changeset
|
888 |
* @throws IOException if an I/O error occurs when creating the |
2 | 889 |
* input stream, the socket is closed, the socket is |
890 |
* not connected, or the socket input has been shutdown |
|
891 |
* using {@link #shutdownInput()} |
|
892 |
* |
|
893 |
* @revised 1.4 |
|
894 |
* @spec JSR-51 |
|
895 |
*/ |
|
896 |
public InputStream getInputStream() throws IOException { |
|
897 |
if (isClosed()) |
|
898 |
throw new SocketException("Socket is closed"); |
|
899 |
if (!isConnected()) |
|
900 |
throw new SocketException("Socket is not connected"); |
|
901 |
if (isInputShutdown()) |
|
902 |
throw new SocketException("Socket input is shutdown"); |
|
54155
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
903 |
InputStream in = this.in; |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
904 |
if (in == null) { |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
905 |
// wrap the input stream so that the close method closes this socket |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
906 |
in = new SocketInputStream(this, impl.getInputStream()); |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
907 |
if (!IN.compareAndSet(this, null, in)) { |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
908 |
in = this.in; |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
909 |
} |
2 | 910 |
} |
54155
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
911 |
return in; |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
912 |
} |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
913 |
|
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
914 |
/** |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
915 |
* An InputStream that delegates read/available operations to an underlying |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
916 |
* input stream. The close method is overridden to close the Socket. |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
917 |
* |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
918 |
* This class is instrumented by Java Flight Recorder (JFR) to get socket |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
919 |
* I/O events. |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
920 |
*/ |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
921 |
private static class SocketInputStream extends InputStream { |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
922 |
private final Socket parent; |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
923 |
private final InputStream in; |
54689
b28b7f631301
8216978: Drop support for pre JDK 1.4 SocketImpl implementations
michaelm
parents:
54289
diff
changeset
|
924 |
|
54155
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
925 |
SocketInputStream(Socket parent, InputStream in) { |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
926 |
this.parent = parent; |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
927 |
this.in = in; |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
928 |
} |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
929 |
@Override |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
930 |
public int read() throws IOException { |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
931 |
byte[] a = new byte[1]; |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
932 |
int n = read(a, 0, 1); |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
933 |
return (n > 0) ? (a[0] & 0xff) : -1; |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
934 |
} |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
935 |
@Override |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
936 |
public int read(byte b[], int off, int len) throws IOException { |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
937 |
return in.read(b, off, len); |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
938 |
} |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
939 |
@Override |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
940 |
public int available() throws IOException { |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
941 |
return in.available(); |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
942 |
} |
54689
b28b7f631301
8216978: Drop support for pre JDK 1.4 SocketImpl implementations
michaelm
parents:
54289
diff
changeset
|
943 |
|
54155
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
944 |
@Override |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
945 |
public void close() throws IOException { |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
946 |
parent.close(); |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
947 |
} |
2 | 948 |
} |
949 |
||
950 |
/** |
|
951 |
* Returns an output stream for this socket. |
|
952 |
* |
|
953 |
* <p> If this socket has an associated channel then the resulting output |
|
954 |
* stream delegates all of its operations to the channel. If the channel |
|
19069 | 955 |
* is in non-blocking mode then the output stream's {@code write} |
2 | 956 |
* operations will throw an {@link |
957 |
* java.nio.channels.IllegalBlockingModeException}. |
|
958 |
* |
|
959 |
* <p> Closing the returned {@link java.io.OutputStream OutputStream} |
|
960 |
* will close the associated socket. |
|
961 |
* |
|
962 |
* @return an output stream for writing bytes to this socket. |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55323
diff
changeset
|
963 |
* @throws IOException if an I/O error occurs when creating the |
2 | 964 |
* output stream or if the socket is not connected. |
965 |
* @revised 1.4 |
|
966 |
* @spec JSR-51 |
|
967 |
*/ |
|
968 |
public OutputStream getOutputStream() throws IOException { |
|
969 |
if (isClosed()) |
|
970 |
throw new SocketException("Socket is closed"); |
|
971 |
if (!isConnected()) |
|
972 |
throw new SocketException("Socket is not connected"); |
|
973 |
if (isOutputShutdown()) |
|
974 |
throw new SocketException("Socket output is shutdown"); |
|
54155
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
975 |
OutputStream out = this.out; |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
976 |
if (out == null) { |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
977 |
// wrap the output stream so that the close method closes this socket |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
978 |
out = new SocketOutputStream(this, impl.getOutputStream()); |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
979 |
if (!OUT.compareAndSet(this, null, out)) { |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
980 |
out = this.out; |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
981 |
} |
2 | 982 |
} |
54155
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
983 |
return out; |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
984 |
} |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
985 |
|
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
986 |
/** |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
987 |
* An OutputStream that delegates write operations to an underlying output |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
988 |
* stream. The close method is overridden to close the Socket. |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
989 |
* |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
990 |
* This class is instrumented by Java Flight Recorder (JFR) to get socket |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
991 |
* I/O events. |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
992 |
*/ |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
993 |
private static class SocketOutputStream extends OutputStream { |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
994 |
private final Socket parent; |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
995 |
private final OutputStream out; |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
996 |
SocketOutputStream(Socket parent, OutputStream out) { |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
997 |
this.parent = parent; |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
998 |
this.out = out; |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
999 |
} |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
1000 |
@Override |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
1001 |
public void write(int b) throws IOException { |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
1002 |
byte[] a = new byte[] { (byte) b }; |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
1003 |
write(a, 0, 1); |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
1004 |
} |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
1005 |
@Override |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
1006 |
public void write(byte b[], int off, int len) throws IOException { |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
1007 |
out.write(b, off, len); |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
1008 |
} |
54689
b28b7f631301
8216978: Drop support for pre JDK 1.4 SocketImpl implementations
michaelm
parents:
54289
diff
changeset
|
1009 |
|
54155
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
1010 |
@Override |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
1011 |
public void close() throws IOException { |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
1012 |
parent.close(); |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
1013 |
} |
2 | 1014 |
} |
1015 |
||
1016 |
/** |
|
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1017 |
* Enable/disable {@link SocketOptions#TCP_NODELAY TCP_NODELAY} |
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1018 |
* (disable/enable Nagle's algorithm). |
2 | 1019 |
* |
19069 | 1020 |
* @param on {@code true} to enable TCP_NODELAY, |
1021 |
* {@code false} to disable. |
|
2 | 1022 |
* |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55323
diff
changeset
|
1023 |
* @throws SocketException if there is an error |
2 | 1024 |
* in the underlying protocol, such as a TCP error. |
1025 |
* |
|
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
24685
diff
changeset
|
1026 |
* @since 1.1 |
2 | 1027 |
* |
1028 |
* @see #getTcpNoDelay() |
|
1029 |
*/ |
|
1030 |
public void setTcpNoDelay(boolean on) throws SocketException { |
|
1031 |
if (isClosed()) |
|
1032 |
throw new SocketException("Socket is closed"); |
|
1033 |
getImpl().setOption(SocketOptions.TCP_NODELAY, Boolean.valueOf(on)); |
|
1034 |
} |
|
1035 |
||
1036 |
/** |
|
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1037 |
* Tests if {@link SocketOptions#TCP_NODELAY TCP_NODELAY} is enabled. |
2 | 1038 |
* |
19069 | 1039 |
* @return a {@code boolean} indicating whether or not |
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1040 |
* {@link SocketOptions#TCP_NODELAY TCP_NODELAY} is enabled. |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55323
diff
changeset
|
1041 |
* @throws SocketException if there is an error |
2 | 1042 |
* in the underlying protocol, such as a TCP error. |
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
24685
diff
changeset
|
1043 |
* @since 1.1 |
2 | 1044 |
* @see #setTcpNoDelay(boolean) |
1045 |
*/ |
|
1046 |
public boolean getTcpNoDelay() throws SocketException { |
|
1047 |
if (isClosed()) |
|
1048 |
throw new SocketException("Socket is closed"); |
|
1049 |
return ((Boolean) getImpl().getOption(SocketOptions.TCP_NODELAY)).booleanValue(); |
|
1050 |
} |
|
1051 |
||
1052 |
/** |
|
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1053 |
* Enable/disable {@link SocketOptions#SO_LINGER SO_LINGER} with the |
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1054 |
* specified linger time in seconds. The maximum timeout value is platform |
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1055 |
* specific. |
2 | 1056 |
* |
1057 |
* The setting only affects socket close. |
|
1058 |
* |
|
1059 |
* @param on whether or not to linger on. |
|
1060 |
* @param linger how long to linger for, if on is true. |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55323
diff
changeset
|
1061 |
* @throws SocketException if there is an error |
2 | 1062 |
* in the underlying protocol, such as a TCP error. |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55323
diff
changeset
|
1063 |
* @throws IllegalArgumentException if the linger value is negative. |
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
24685
diff
changeset
|
1064 |
* @since 1.1 |
2 | 1065 |
* @see #getSoLinger() |
1066 |
*/ |
|
1067 |
public void setSoLinger(boolean on, int linger) throws SocketException { |
|
1068 |
if (isClosed()) |
|
1069 |
throw new SocketException("Socket is closed"); |
|
1070 |
if (!on) { |
|
24685
215fa91e1b4c
8044461: Cleanup new Boolean and single character strings
rriggs
parents:
23879
diff
changeset
|
1071 |
getImpl().setOption(SocketOptions.SO_LINGER, on); |
2 | 1072 |
} else { |
1073 |
if (linger < 0) { |
|
1074 |
throw new IllegalArgumentException("invalid value for SO_LINGER"); |
|
1075 |
} |
|
1076 |
if (linger > 65535) |
|
1077 |
linger = 65535; |
|
25522
10d789df41bb
8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents:
24865
diff
changeset
|
1078 |
getImpl().setOption(SocketOptions.SO_LINGER, linger); |
2 | 1079 |
} |
1080 |
} |
|
1081 |
||
1082 |
/** |
|
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1083 |
* Returns setting for {@link SocketOptions#SO_LINGER SO_LINGER}. |
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1084 |
* -1 returns implies that the |
2 | 1085 |
* option is disabled. |
1086 |
* |
|
1087 |
* The setting only affects socket close. |
|
1088 |
* |
|
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1089 |
* @return the setting for {@link SocketOptions#SO_LINGER SO_LINGER}. |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55323
diff
changeset
|
1090 |
* @throws SocketException if there is an error |
2 | 1091 |
* in the underlying protocol, such as a TCP error. |
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
24685
diff
changeset
|
1092 |
* @since 1.1 |
2 | 1093 |
* @see #setSoLinger(boolean, int) |
1094 |
*/ |
|
1095 |
public int getSoLinger() throws SocketException { |
|
1096 |
if (isClosed()) |
|
1097 |
throw new SocketException("Socket is closed"); |
|
1098 |
Object o = getImpl().getOption(SocketOptions.SO_LINGER); |
|
1099 |
if (o instanceof Integer) { |
|
1100 |
return ((Integer) o).intValue(); |
|
1101 |
} else { |
|
1102 |
return -1; |
|
1103 |
} |
|
1104 |
} |
|
1105 |
||
1106 |
/** |
|
1107 |
* Send one byte of urgent data on the socket. The byte to be sent is the lowest eight |
|
1108 |
* bits of the data parameter. The urgent byte is |
|
1109 |
* sent after any preceding writes to the socket OutputStream |
|
1110 |
* and before any future writes to the OutputStream. |
|
1111 |
* @param data The byte of data to send |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55323
diff
changeset
|
1112 |
* @throws IOException if there is an error |
2 | 1113 |
* sending the data. |
1114 |
* @since 1.4 |
|
1115 |
*/ |
|
1116 |
public void sendUrgentData (int data) throws IOException { |
|
1117 |
if (!getImpl().supportsUrgentData ()) { |
|
1118 |
throw new SocketException ("Urgent data not supported"); |
|
1119 |
} |
|
1120 |
getImpl().sendUrgentData (data); |
|
1121 |
} |
|
1122 |
||
1123 |
/** |
|
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1124 |
* Enable/disable {@link SocketOptions#SO_OOBINLINE SO_OOBINLINE} |
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1125 |
* (receipt of TCP urgent data) |
2 | 1126 |
* |
1127 |
* By default, this option is disabled and TCP urgent data received on a |
|
1128 |
* socket is silently discarded. If the user wishes to receive urgent data, then |
|
1129 |
* this option must be enabled. When enabled, urgent data is received |
|
1130 |
* inline with normal data. |
|
1131 |
* <p> |
|
1132 |
* Note, only limited support is provided for handling incoming urgent |
|
1133 |
* data. In particular, no notification of incoming urgent data is provided |
|
1134 |
* and there is no capability to distinguish between normal data and urgent |
|
1135 |
* data unless provided by a higher level protocol. |
|
1136 |
* |
|
19069 | 1137 |
* @param on {@code true} to enable |
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1138 |
* {@link SocketOptions#SO_OOBINLINE SO_OOBINLINE}, |
19069 | 1139 |
* {@code false} to disable. |
2 | 1140 |
* |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55323
diff
changeset
|
1141 |
* @throws SocketException if there is an error |
2 | 1142 |
* in the underlying protocol, such as a TCP error. |
1143 |
* |
|
1144 |
* @since 1.4 |
|
1145 |
* |
|
1146 |
* @see #getOOBInline() |
|
1147 |
*/ |
|
1148 |
public void setOOBInline(boolean on) throws SocketException { |
|
1149 |
if (isClosed()) |
|
1150 |
throw new SocketException("Socket is closed"); |
|
1151 |
getImpl().setOption(SocketOptions.SO_OOBINLINE, Boolean.valueOf(on)); |
|
1152 |
} |
|
1153 |
||
1154 |
/** |
|
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1155 |
* Tests if {@link SocketOptions#SO_OOBINLINE SO_OOBINLINE} is enabled. |
2 | 1156 |
* |
19069 | 1157 |
* @return a {@code boolean} indicating whether or not |
53133
bf2533105a26
8215912: Various Typos in java.net Method Documentation
coffeys
parents:
52499
diff
changeset
|
1158 |
* {@link SocketOptions#SO_OOBINLINE SO_OOBINLINE} is enabled. |
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1159 |
* |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55323
diff
changeset
|
1160 |
* @throws SocketException if there is an error |
2 | 1161 |
* in the underlying protocol, such as a TCP error. |
1162 |
* @since 1.4 |
|
1163 |
* @see #setOOBInline(boolean) |
|
1164 |
*/ |
|
1165 |
public boolean getOOBInline() throws SocketException { |
|
1166 |
if (isClosed()) |
|
1167 |
throw new SocketException("Socket is closed"); |
|
1168 |
return ((Boolean) getImpl().getOption(SocketOptions.SO_OOBINLINE)).booleanValue(); |
|
1169 |
} |
|
1170 |
||
1171 |
/** |
|
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1172 |
* Enable/disable {@link SocketOptions#SO_TIMEOUT SO_TIMEOUT} |
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1173 |
* with the specified timeout, in milliseconds. With this option set |
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1174 |
* to a non-zero timeout, a read() call on the InputStream associated with |
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1175 |
* this Socket will block for only this amount of time. If the timeout |
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1176 |
* expires, a <B>java.net.SocketTimeoutException</B> is raised, though the |
2 | 1177 |
* Socket is still valid. The option <B>must</B> be enabled |
1178 |
* prior to entering the blocking operation to have effect. The |
|
18156 | 1179 |
* timeout must be {@code > 0}. |
2 | 1180 |
* A timeout of zero is interpreted as an infinite timeout. |
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1181 |
* |
2 | 1182 |
* @param timeout the specified timeout, in milliseconds. |
54289
6183f835b9b6
8219446: Specify behaviour of timeout accepting methods of Socket and ServerSocket if timeout is negative
chegar
parents:
54155
diff
changeset
|
1183 |
* @throws SocketException if there is an error in the underlying protocol, |
6183f835b9b6
8219446: Specify behaviour of timeout accepting methods of Socket and ServerSocket if timeout is negative
chegar
parents:
54155
diff
changeset
|
1184 |
* such as a TCP error |
6183f835b9b6
8219446: Specify behaviour of timeout accepting methods of Socket and ServerSocket if timeout is negative
chegar
parents:
54155
diff
changeset
|
1185 |
* @throws IllegalArgumentException if {@code timeout} is negative |
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
24685
diff
changeset
|
1186 |
* @since 1.1 |
2 | 1187 |
* @see #getSoTimeout() |
1188 |
*/ |
|
1189 |
public synchronized void setSoTimeout(int timeout) throws SocketException { |
|
1190 |
if (isClosed()) |
|
1191 |
throw new SocketException("Socket is closed"); |
|
1192 |
if (timeout < 0) |
|
1193 |
throw new IllegalArgumentException("timeout can't be negative"); |
|
1194 |
||
25522
10d789df41bb
8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents:
24865
diff
changeset
|
1195 |
getImpl().setOption(SocketOptions.SO_TIMEOUT, timeout); |
2 | 1196 |
} |
1197 |
||
1198 |
/** |
|
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1199 |
* Returns setting for {@link SocketOptions#SO_TIMEOUT SO_TIMEOUT}. |
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1200 |
* 0 returns implies that the option is disabled (i.e., timeout of infinity). |
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1201 |
* |
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1202 |
* @return the setting for {@link SocketOptions#SO_TIMEOUT SO_TIMEOUT} |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55323
diff
changeset
|
1203 |
* @throws SocketException if there is an error |
2 | 1204 |
* in the underlying protocol, such as a TCP error. |
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1205 |
* |
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
24685
diff
changeset
|
1206 |
* @since 1.1 |
2 | 1207 |
* @see #setSoTimeout(int) |
1208 |
*/ |
|
1209 |
public synchronized int getSoTimeout() throws SocketException { |
|
1210 |
if (isClosed()) |
|
1211 |
throw new SocketException("Socket is closed"); |
|
1212 |
Object o = getImpl().getOption(SocketOptions.SO_TIMEOUT); |
|
1213 |
/* extra type safety */ |
|
1214 |
if (o instanceof Integer) { |
|
1215 |
return ((Integer) o).intValue(); |
|
1216 |
} else { |
|
1217 |
return 0; |
|
1218 |
} |
|
1219 |
} |
|
1220 |
||
1221 |
/** |
|
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1222 |
* Sets the {@link SocketOptions#SO_SNDBUF SO_SNDBUF} option to the |
19069 | 1223 |
* specified value for this {@code Socket}. |
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1224 |
* The {@link SocketOptions#SO_SNDBUF SO_SNDBUF} option is used by the |
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1225 |
* platform's networking code as a hint for the size to set the underlying |
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1226 |
* network I/O buffers. |
2 | 1227 |
* |
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1228 |
* <p>Because {@link SocketOptions#SO_SNDBUF SO_SNDBUF} is a hint, |
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1229 |
* applications that want to verify what size the buffers were set to |
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1230 |
* should call {@link #getSendBufferSize()}. |
2 | 1231 |
* |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55323
diff
changeset
|
1232 |
* @throws SocketException if there is an error |
2 | 1233 |
* in the underlying protocol, such as a TCP error. |
1234 |
* |
|
1235 |
* @param size the size to which to set the send buffer |
|
1236 |
* size. This value must be greater than 0. |
|
1237 |
* |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55323
diff
changeset
|
1238 |
* @throws IllegalArgumentException if the |
2 | 1239 |
* value is 0 or is negative. |
1240 |
* |
|
1241 |
* @see #getSendBufferSize() |
|
1242 |
* @since 1.2 |
|
1243 |
*/ |
|
1244 |
public synchronized void setSendBufferSize(int size) |
|
1245 |
throws SocketException{ |
|
1246 |
if (!(size > 0)) { |
|
1247 |
throw new IllegalArgumentException("negative send size"); |
|
1248 |
} |
|
1249 |
if (isClosed()) |
|
1250 |
throw new SocketException("Socket is closed"); |
|
25522
10d789df41bb
8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents:
24865
diff
changeset
|
1251 |
getImpl().setOption(SocketOptions.SO_SNDBUF, size); |
2 | 1252 |
} |
1253 |
||
1254 |
/** |
|
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1255 |
* Get value of the {@link SocketOptions#SO_SNDBUF SO_SNDBUF} option |
19069 | 1256 |
* for this {@code Socket}, that is the buffer size used by the platform |
1257 |
* for output on this {@code Socket}. |
|
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1258 |
* @return the value of the {@link SocketOptions#SO_SNDBUF SO_SNDBUF} |
19069 | 1259 |
* option for this {@code Socket}. |
2 | 1260 |
* |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55323
diff
changeset
|
1261 |
* @throws SocketException if there is an error |
2 | 1262 |
* in the underlying protocol, such as a TCP error. |
1263 |
* |
|
1264 |
* @see #setSendBufferSize(int) |
|
1265 |
* @since 1.2 |
|
1266 |
*/ |
|
1267 |
public synchronized int getSendBufferSize() throws SocketException { |
|
1268 |
if (isClosed()) |
|
1269 |
throw new SocketException("Socket is closed"); |
|
1270 |
int result = 0; |
|
1271 |
Object o = getImpl().getOption(SocketOptions.SO_SNDBUF); |
|
1272 |
if (o instanceof Integer) { |
|
1273 |
result = ((Integer)o).intValue(); |
|
1274 |
} |
|
1275 |
return result; |
|
1276 |
} |
|
1277 |
||
1278 |
/** |
|
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1279 |
* Sets the {@link SocketOptions#SO_RCVBUF SO_RCVBUF} option to the |
19069 | 1280 |
* specified value for this {@code Socket}. The |
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1281 |
* {@link SocketOptions#SO_RCVBUF SO_RCVBUF} option is |
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1282 |
* used by the platform's networking code as a hint for the size to set |
2 | 1283 |
* the underlying network I/O buffers. |
1284 |
* |
|
1285 |
* <p>Increasing the receive buffer size can increase the performance of |
|
1286 |
* network I/O for high-volume connection, while decreasing it can |
|
1287 |
* help reduce the backlog of incoming data. |
|
1288 |
* |
|
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1289 |
* <p>Because {@link SocketOptions#SO_RCVBUF SO_RCVBUF} is a hint, |
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1290 |
* applications that want to verify what size the buffers were set to |
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1291 |
* should call {@link #getReceiveBufferSize()}. |
2 | 1292 |
* |
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1293 |
* <p>The value of {@link SocketOptions#SO_RCVBUF SO_RCVBUF} is also used |
52499
768b1c612100
8213490: Networking area typos and inconsistencies cleanup
prappo
parents:
52456
diff
changeset
|
1294 |
* to set the TCP receive window that is advertised to the remote peer. |
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1295 |
* Generally, the window size can be modified at any time when a socket is |
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1296 |
* connected. However, if a receive window larger than 64K is required then |
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1297 |
* this must be requested <B>before</B> the socket is connected to the |
21334 | 1298 |
* remote peer. There are two cases to be aware of: |
2 | 1299 |
* <ol> |
1300 |
* <li>For sockets accepted from a ServerSocket, this must be done by calling |
|
1301 |
* {@link ServerSocket#setReceiveBufferSize(int)} before the ServerSocket |
|
23720
7d5147c21927
8039172: Tidy warnings cleanup for java.net, java.math, java.time, java.rmi
yan
parents:
22339
diff
changeset
|
1302 |
* is bound to a local address.</li> |
2 | 1303 |
* <li>For client sockets, setReceiveBufferSize() must be called before |
21334 | 1304 |
* connecting the socket to its remote peer.</li></ol> |
2 | 1305 |
* @param size the size to which to set the receive buffer |
1306 |
* size. This value must be greater than 0. |
|
1307 |
* |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55323
diff
changeset
|
1308 |
* @throws IllegalArgumentException if the value is 0 or is |
2 | 1309 |
* negative. |
1310 |
* |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55323
diff
changeset
|
1311 |
* @throws SocketException if there is an error |
2 | 1312 |
* in the underlying protocol, such as a TCP error. |
1313 |
* |
|
1314 |
* @see #getReceiveBufferSize() |
|
1315 |
* @see ServerSocket#setReceiveBufferSize(int) |
|
1316 |
* @since 1.2 |
|
1317 |
*/ |
|
1318 |
public synchronized void setReceiveBufferSize(int size) |
|
1319 |
throws SocketException{ |
|
1320 |
if (size <= 0) { |
|
1321 |
throw new IllegalArgumentException("invalid receive size"); |
|
1322 |
} |
|
1323 |
if (isClosed()) |
|
1324 |
throw new SocketException("Socket is closed"); |
|
25522
10d789df41bb
8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents:
24865
diff
changeset
|
1325 |
getImpl().setOption(SocketOptions.SO_RCVBUF, size); |
2 | 1326 |
} |
1327 |
||
1328 |
/** |
|
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1329 |
* Gets the value of the {@link SocketOptions#SO_RCVBUF SO_RCVBUF} option |
19069 | 1330 |
* for this {@code Socket}, that is the buffer size used by the platform |
1331 |
* for input on this {@code Socket}. |
|
2 | 1332 |
* |
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1333 |
* @return the value of the {@link SocketOptions#SO_RCVBUF SO_RCVBUF} |
19069 | 1334 |
* option for this {@code Socket}. |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55323
diff
changeset
|
1335 |
* @throws SocketException if there is an error |
2 | 1336 |
* in the underlying protocol, such as a TCP error. |
1337 |
* @see #setReceiveBufferSize(int) |
|
1338 |
* @since 1.2 |
|
1339 |
*/ |
|
1340 |
public synchronized int getReceiveBufferSize() |
|
1341 |
throws SocketException{ |
|
1342 |
if (isClosed()) |
|
1343 |
throw new SocketException("Socket is closed"); |
|
1344 |
int result = 0; |
|
1345 |
Object o = getImpl().getOption(SocketOptions.SO_RCVBUF); |
|
1346 |
if (o instanceof Integer) { |
|
1347 |
result = ((Integer)o).intValue(); |
|
1348 |
} |
|
1349 |
return result; |
|
1350 |
} |
|
1351 |
||
1352 |
/** |
|
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1353 |
* Enable/disable {@link SocketOptions#SO_KEEPALIVE SO_KEEPALIVE}. |
2 | 1354 |
* |
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1355 |
* @param on whether or not to have socket keep alive turned on. |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55323
diff
changeset
|
1356 |
* @throws SocketException if there is an error |
2 | 1357 |
* in the underlying protocol, such as a TCP error. |
1358 |
* @since 1.3 |
|
1359 |
* @see #getKeepAlive() |
|
1360 |
*/ |
|
1361 |
public void setKeepAlive(boolean on) throws SocketException { |
|
1362 |
if (isClosed()) |
|
1363 |
throw new SocketException("Socket is closed"); |
|
1364 |
getImpl().setOption(SocketOptions.SO_KEEPALIVE, Boolean.valueOf(on)); |
|
1365 |
} |
|
1366 |
||
1367 |
/** |
|
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1368 |
* Tests if {@link SocketOptions#SO_KEEPALIVE SO_KEEPALIVE} is enabled. |
2 | 1369 |
* |
19069 | 1370 |
* @return a {@code boolean} indicating whether or not |
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1371 |
* {@link SocketOptions#SO_KEEPALIVE SO_KEEPALIVE} is enabled. |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55323
diff
changeset
|
1372 |
* @throws SocketException if there is an error |
2 | 1373 |
* in the underlying protocol, such as a TCP error. |
1374 |
* @since 1.3 |
|
1375 |
* @see #setKeepAlive(boolean) |
|
1376 |
*/ |
|
1377 |
public boolean getKeepAlive() throws SocketException { |
|
1378 |
if (isClosed()) |
|
1379 |
throw new SocketException("Socket is closed"); |
|
1380 |
return ((Boolean) getImpl().getOption(SocketOptions.SO_KEEPALIVE)).booleanValue(); |
|
1381 |
} |
|
1382 |
||
1383 |
/** |
|
1384 |
* Sets traffic class or type-of-service octet in the IP |
|
1385 |
* header for packets sent from this Socket. |
|
1386 |
* As the underlying network implementation may ignore this |
|
1387 |
* value applications should consider it a hint. |
|
1388 |
* |
|
18156 | 1389 |
* <P> The tc <B>must</B> be in the range {@code 0 <= tc <= |
1390 |
* 255} or an IllegalArgumentException will be thrown. |
|
2 | 1391 |
* <p>Notes: |
1392 |
* <p>For Internet Protocol v4 the value consists of an |
|
19069 | 1393 |
* {@code integer}, the least significant 8 bits of which |
2 | 1394 |
* represent the value of the TOS octet in IP packets sent by |
1395 |
* the socket. |
|
1396 |
* RFC 1349 defines the TOS values as follows: |
|
21334 | 1397 |
* |
2 | 1398 |
* <UL> |
1399 |
* <LI><CODE>IPTOS_LOWCOST (0x02)</CODE></LI> |
|
1400 |
* <LI><CODE>IPTOS_RELIABILITY (0x04)</CODE></LI> |
|
1401 |
* <LI><CODE>IPTOS_THROUGHPUT (0x08)</CODE></LI> |
|
1402 |
* <LI><CODE>IPTOS_LOWDELAY (0x10)</CODE></LI> |
|
1403 |
* </UL> |
|
1404 |
* The last low order bit is always ignored as this |
|
1405 |
* corresponds to the MBZ (must be zero) bit. |
|
1406 |
* <p> |
|
1407 |
* Setting bits in the precedence field may result in a |
|
1408 |
* SocketException indicating that the operation is not |
|
1409 |
* permitted. |
|
1410 |
* <p> |
|
1411 |
* As RFC 1122 section 4.2.4.2 indicates, a compliant TCP |
|
1412 |
* implementation should, but is not required to, let application |
|
1413 |
* change the TOS field during the lifetime of a connection. |
|
1414 |
* So whether the type-of-service field can be changed after the |
|
1415 |
* TCP connection has been established depends on the implementation |
|
1416 |
* in the underlying platform. Applications should not assume that |
|
1417 |
* they can change the TOS field after the connection. |
|
1418 |
* <p> |
|
19069 | 1419 |
* For Internet Protocol v6 {@code tc} is the value that |
2 | 1420 |
* would be placed into the sin6_flowinfo field of the IP header. |
1421 |
* |
|
19069 | 1422 |
* @param tc an {@code int} value for the bitset. |
2 | 1423 |
* @throws SocketException if there is an error setting the |
1424 |
* traffic class or type-of-service |
|
1425 |
* @since 1.4 |
|
1426 |
* @see #getTrafficClass |
|
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1427 |
* @see SocketOptions#IP_TOS |
2 | 1428 |
*/ |
1429 |
public void setTrafficClass(int tc) throws SocketException { |
|
1430 |
if (tc < 0 || tc > 255) |
|
1431 |
throw new IllegalArgumentException("tc is not in range 0 -- 255"); |
|
1432 |
||
1433 |
if (isClosed()) |
|
1434 |
throw new SocketException("Socket is closed"); |
|
30963
88469d06e03f
8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents:
29986
diff
changeset
|
1435 |
try { |
88469d06e03f
8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents:
29986
diff
changeset
|
1436 |
getImpl().setOption(SocketOptions.IP_TOS, tc); |
88469d06e03f
8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents:
29986
diff
changeset
|
1437 |
} catch (SocketException se) { |
88469d06e03f
8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents:
29986
diff
changeset
|
1438 |
// not supported if socket already connected |
88469d06e03f
8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents:
29986
diff
changeset
|
1439 |
// Solaris returns error in such cases |
88469d06e03f
8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents:
29986
diff
changeset
|
1440 |
if(!isConnected()) |
88469d06e03f
8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents:
29986
diff
changeset
|
1441 |
throw se; |
88469d06e03f
8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents:
29986
diff
changeset
|
1442 |
} |
2 | 1443 |
} |
1444 |
||
1445 |
/** |
|
1446 |
* Gets traffic class or type-of-service in the IP header |
|
1447 |
* for packets sent from this Socket |
|
1448 |
* <p> |
|
1449 |
* As the underlying network implementation may ignore the |
|
1450 |
* traffic class or type-of-service set using {@link #setTrafficClass(int)} |
|
1451 |
* this method may return a different value than was previously |
|
1452 |
* set using the {@link #setTrafficClass(int)} method on this Socket. |
|
1453 |
* |
|
1454 |
* @return the traffic class or type-of-service already set |
|
1455 |
* @throws SocketException if there is an error obtaining the |
|
1456 |
* traffic class or type-of-service value. |
|
1457 |
* @since 1.4 |
|
1458 |
* @see #setTrafficClass(int) |
|
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1459 |
* @see SocketOptions#IP_TOS |
2 | 1460 |
*/ |
1461 |
public int getTrafficClass() throws SocketException { |
|
1462 |
return ((Integer) (getImpl().getOption(SocketOptions.IP_TOS))).intValue(); |
|
1463 |
} |
|
1464 |
||
1465 |
/** |
|
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1466 |
* Enable/disable the {@link SocketOptions#SO_REUSEADDR SO_REUSEADDR} |
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1467 |
* socket option. |
2 | 1468 |
* <p> |
1469 |
* When a TCP connection is closed the connection may remain |
|
1470 |
* in a timeout state for a period of time after the connection |
|
19069 | 1471 |
* is closed (typically known as the {@code TIME_WAIT} state |
1472 |
* or {@code 2MSL} wait state). |
|
2 | 1473 |
* For applications using a well known socket address or port |
1474 |
* it may not be possible to bind a socket to the required |
|
19069 | 1475 |
* {@code SocketAddress} if there is a connection in the |
2 | 1476 |
* timeout state involving the socket address or port. |
1477 |
* <p> |
|
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1478 |
* Enabling {@link SocketOptions#SO_REUSEADDR SO_REUSEADDR} |
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1479 |
* prior to binding the socket using {@link #bind(SocketAddress)} allows |
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1480 |
* the socket to be bound even though a previous connection is in a timeout |
2 | 1481 |
* state. |
1482 |
* <p> |
|
19069 | 1483 |
* When a {@code Socket} is created the initial setting |
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1484 |
* of {@link SocketOptions#SO_REUSEADDR SO_REUSEADDR} is disabled. |
2 | 1485 |
* <p> |
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1486 |
* The behaviour when {@link SocketOptions#SO_REUSEADDR SO_REUSEADDR} is |
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1487 |
* enabled or disabled after a socket is bound (See {@link #isBound()}) |
2 | 1488 |
* is not defined. |
1489 |
* |
|
1490 |
* @param on whether to enable or disable the socket option |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55323
diff
changeset
|
1491 |
* @throws SocketException if an error occurs enabling or |
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1492 |
* disabling the {@link SocketOptions#SO_REUSEADDR SO_REUSEADDR} |
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1493 |
* socket option, or the socket is closed. |
2 | 1494 |
* @since 1.4 |
1495 |
* @see #getReuseAddress() |
|
1496 |
* @see #bind(SocketAddress) |
|
1497 |
* @see #isClosed() |
|
1498 |
* @see #isBound() |
|
1499 |
*/ |
|
1500 |
public void setReuseAddress(boolean on) throws SocketException { |
|
1501 |
if (isClosed()) |
|
1502 |
throw new SocketException("Socket is closed"); |
|
1503 |
getImpl().setOption(SocketOptions.SO_REUSEADDR, Boolean.valueOf(on)); |
|
1504 |
} |
|
1505 |
||
1506 |
/** |
|
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1507 |
* Tests if {@link SocketOptions#SO_REUSEADDR SO_REUSEADDR} is enabled. |
2 | 1508 |
* |
19069 | 1509 |
* @return a {@code boolean} indicating whether or not |
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
16061
diff
changeset
|
1510 |
* {@link SocketOptions#SO_REUSEADDR SO_REUSEADDR} is enabled. |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55323
diff
changeset
|
1511 |
* @throws SocketException if there is an error |
2 | 1512 |
* in the underlying protocol, such as a TCP error. |
1513 |
* @since 1.4 |
|
1514 |
* @see #setReuseAddress(boolean) |
|
1515 |
*/ |
|
1516 |
public boolean getReuseAddress() throws SocketException { |
|
1517 |
if (isClosed()) |
|
1518 |
throw new SocketException("Socket is closed"); |
|
1519 |
return ((Boolean) (getImpl().getOption(SocketOptions.SO_REUSEADDR))).booleanValue(); |
|
1520 |
} |
|
1521 |
||
1522 |
/** |
|
1523 |
* Closes this socket. |
|
1524 |
* <p> |
|
1525 |
* Any thread currently blocked in an I/O operation upon this socket |
|
1526 |
* will throw a {@link SocketException}. |
|
1527 |
* <p> |
|
1528 |
* Once a socket has been closed, it is not available for further networking |
|
1529 |
* use (i.e. can't be reconnected or rebound). A new socket needs to be |
|
1530 |
* created. |
|
1531 |
* |
|
1532 |
* <p> Closing this socket will also close the socket's |
|
1533 |
* {@link java.io.InputStream InputStream} and |
|
1534 |
* {@link java.io.OutputStream OutputStream}. |
|
1535 |
* |
|
1536 |
* <p> If this socket has an associated channel then the channel is closed |
|
1537 |
* as well. |
|
1538 |
* |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55323
diff
changeset
|
1539 |
* @throws IOException if an I/O error occurs when closing this socket. |
2 | 1540 |
* @revised 1.4 |
1541 |
* @spec JSR-51 |
|
1542 |
* @see #isClosed |
|
1543 |
*/ |
|
1544 |
public synchronized void close() throws IOException { |
|
1545 |
synchronized(closeLock) { |
|
1546 |
if (isClosed()) |
|
1547 |
return; |
|
1548 |
if (created) |
|
1549 |
impl.close(); |
|
1550 |
closed = true; |
|
1551 |
} |
|
1552 |
} |
|
1553 |
||
1554 |
/** |
|
1555 |
* Places the input stream for this socket at "end of stream". |
|
1556 |
* Any data sent to the input stream side of the socket is acknowledged |
|
1557 |
* and then silently discarded. |
|
1558 |
* <p> |
|
10421
2ee16a0f6df5
7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
9775
diff
changeset
|
1559 |
* If you read from a socket input stream after invoking this method on the |
2ee16a0f6df5
7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
9775
diff
changeset
|
1560 |
* socket, the stream's {@code available} method will return 0, and its |
2ee16a0f6df5
7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
9775
diff
changeset
|
1561 |
* {@code read} methods will return {@code -1} (end of stream). |
2 | 1562 |
* |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55323
diff
changeset
|
1563 |
* @throws IOException if an I/O error occurs when shutting down this |
2 | 1564 |
* socket. |
1565 |
* |
|
1566 |
* @since 1.3 |
|
1567 |
* @see java.net.Socket#shutdownOutput() |
|
1568 |
* @see java.net.Socket#close() |
|
1569 |
* @see java.net.Socket#setSoLinger(boolean, int) |
|
1570 |
* @see #isInputShutdown |
|
1571 |
*/ |
|
1572 |
public void shutdownInput() throws IOException |
|
1573 |
{ |
|
1574 |
if (isClosed()) |
|
1575 |
throw new SocketException("Socket is closed"); |
|
1576 |
if (!isConnected()) |
|
1577 |
throw new SocketException("Socket is not connected"); |
|
1578 |
if (isInputShutdown()) |
|
1579 |
throw new SocketException("Socket input is already shutdown"); |
|
1580 |
getImpl().shutdownInput(); |
|
1581 |
shutIn = true; |
|
1582 |
} |
|
1583 |
||
1584 |
/** |
|
1585 |
* Disables the output stream for this socket. |
|
1586 |
* For a TCP socket, any previously written data will be sent |
|
1587 |
* followed by TCP's normal connection termination sequence. |
|
1588 |
* |
|
1589 |
* If you write to a socket output stream after invoking |
|
1590 |
* shutdownOutput() on the socket, the stream will throw |
|
1591 |
* an IOException. |
|
1592 |
* |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55323
diff
changeset
|
1593 |
* @throws IOException if an I/O error occurs when shutting down this |
2 | 1594 |
* socket. |
1595 |
* |
|
1596 |
* @since 1.3 |
|
1597 |
* @see java.net.Socket#shutdownInput() |
|
1598 |
* @see java.net.Socket#close() |
|
1599 |
* @see java.net.Socket#setSoLinger(boolean, int) |
|
1600 |
* @see #isOutputShutdown |
|
1601 |
*/ |
|
1602 |
public void shutdownOutput() throws IOException |
|
1603 |
{ |
|
1604 |
if (isClosed()) |
|
1605 |
throw new SocketException("Socket is closed"); |
|
1606 |
if (!isConnected()) |
|
1607 |
throw new SocketException("Socket is not connected"); |
|
1608 |
if (isOutputShutdown()) |
|
1609 |
throw new SocketException("Socket output is already shutdown"); |
|
1610 |
getImpl().shutdownOutput(); |
|
1611 |
shutOut = true; |
|
1612 |
} |
|
1613 |
||
1614 |
/** |
|
19069 | 1615 |
* Converts this socket to a {@code String}. |
2 | 1616 |
* |
1617 |
* @return a string representation of this socket. |
|
1618 |
*/ |
|
1619 |
public String toString() { |
|
1620 |
try { |
|
1621 |
if (isConnected()) |
|
1622 |
return "Socket[addr=" + getImpl().getInetAddress() + |
|
1623 |
",port=" + getImpl().getPort() + |
|
1624 |
",localport=" + getImpl().getLocalPort() + "]"; |
|
1625 |
} catch (SocketException e) { |
|
1626 |
} |
|
1627 |
return "Socket[unconnected]"; |
|
1628 |
} |
|
1629 |
||
1630 |
/** |
|
1631 |
* Returns the connection state of the socket. |
|
1632 |
* <p> |
|
1633 |
* Note: Closing a socket doesn't clear its connection state, which means |
|
19069 | 1634 |
* this method will return {@code true} for a closed socket |
52499
768b1c612100
8213490: Networking area typos and inconsistencies cleanup
prappo
parents:
52456
diff
changeset
|
1635 |
* (see {@link #isClosed()}) if it was successfully connected prior |
2 | 1636 |
* to being closed. |
1637 |
* |
|
52499
768b1c612100
8213490: Networking area typos and inconsistencies cleanup
prappo
parents:
52456
diff
changeset
|
1638 |
* @return true if the socket was successfully connected to a server |
2 | 1639 |
* @since 1.4 |
1640 |
*/ |
|
1641 |
public boolean isConnected() { |
|
54689
b28b7f631301
8216978: Drop support for pre JDK 1.4 SocketImpl implementations
michaelm
parents:
54289
diff
changeset
|
1642 |
return connected; |
2 | 1643 |
} |
1644 |
||
1645 |
/** |
|
1646 |
* Returns the binding state of the socket. |
|
1647 |
* <p> |
|
1648 |
* Note: Closing a socket doesn't clear its binding state, which means |
|
19069 | 1649 |
* this method will return {@code true} for a closed socket |
52499
768b1c612100
8213490: Networking area typos and inconsistencies cleanup
prappo
parents:
52456
diff
changeset
|
1650 |
* (see {@link #isClosed()}) if it was successfully bound prior |
2 | 1651 |
* to being closed. |
1652 |
* |
|
52499
768b1c612100
8213490: Networking area typos and inconsistencies cleanup
prappo
parents:
52456
diff
changeset
|
1653 |
* @return true if the socket was successfully bound to an address |
2 | 1654 |
* @since 1.4 |
1655 |
* @see #bind |
|
1656 |
*/ |
|
1657 |
public boolean isBound() { |
|
54689
b28b7f631301
8216978: Drop support for pre JDK 1.4 SocketImpl implementations
michaelm
parents:
54289
diff
changeset
|
1658 |
return bound; |
2 | 1659 |
} |
1660 |
||
1661 |
/** |
|
1662 |
* Returns the closed state of the socket. |
|
1663 |
* |
|
1664 |
* @return true if the socket has been closed |
|
1665 |
* @since 1.4 |
|
1666 |
* @see #close |
|
1667 |
*/ |
|
1668 |
public boolean isClosed() { |
|
1669 |
synchronized(closeLock) { |
|
1670 |
return closed; |
|
1671 |
} |
|
1672 |
} |
|
1673 |
||
1674 |
/** |
|
1675 |
* Returns whether the read-half of the socket connection is closed. |
|
1676 |
* |
|
1677 |
* @return true if the input of the socket has been shutdown |
|
1678 |
* @since 1.4 |
|
1679 |
* @see #shutdownInput |
|
1680 |
*/ |
|
1681 |
public boolean isInputShutdown() { |
|
1682 |
return shutIn; |
|
1683 |
} |
|
1684 |
||
1685 |
/** |
|
1686 |
* Returns whether the write-half of the socket connection is closed. |
|
1687 |
* |
|
1688 |
* @return true if the output of the socket has been shutdown |
|
1689 |
* @since 1.4 |
|
1690 |
* @see #shutdownOutput |
|
1691 |
*/ |
|
1692 |
public boolean isOutputShutdown() { |
|
1693 |
return shutOut; |
|
1694 |
} |
|
1695 |
||
1696 |
/** |
|
1697 |
* The factory for all client sockets. |
|
1698 |
*/ |
|
54155
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
1699 |
private static volatile SocketImplFactory factory; |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
1700 |
|
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
1701 |
static SocketImplFactory socketImplFactory() { |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
1702 |
return factory; |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
53133
diff
changeset
|
1703 |
} |
2 | 1704 |
|
1705 |
/** |
|
1706 |
* Sets the client socket implementation factory for the |
|
1707 |
* application. The factory can be specified only once. |
|
1708 |
* <p> |
|
1709 |
* When an application creates a new client socket, the socket |
|
19069 | 1710 |
* implementation factory's {@code createSocketImpl} method is |
2 | 1711 |
* called to create the actual socket implementation. |
1712 |
* <p> |
|
19069 | 1713 |
* Passing {@code null} to the method is a no-op unless the factory |
2 | 1714 |
* was already set. |
1715 |
* <p>If there is a security manager, this method first calls |
|
19069 | 1716 |
* the security manager's {@code checkSetFactory} method |
2 | 1717 |
* to ensure the operation is allowed. |
1718 |
* This could result in a SecurityException. |
|
1719 |
* |
|
1720 |
* @param fac the desired factory. |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55323
diff
changeset
|
1721 |
* @throws IOException if an I/O error occurs when setting the |
2 | 1722 |
* socket factory. |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55323
diff
changeset
|
1723 |
* @throws SocketException if the factory is already defined. |
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55323
diff
changeset
|
1724 |
* @throws SecurityException if a security manager exists and its |
19069 | 1725 |
* {@code checkSetFactory} method doesn't allow the operation. |
2 | 1726 |
* @see java.net.SocketImplFactory#createSocketImpl() |
1727 |
* @see SecurityManager#checkSetFactory |
|
1728 |
*/ |
|
1729 |
public static synchronized void setSocketImplFactory(SocketImplFactory fac) |
|
1730 |
throws IOException |
|
1731 |
{ |
|
1732 |
if (factory != null) { |
|
1733 |
throw new SocketException("factory already defined"); |
|
1734 |
} |
|
1735 |
SecurityManager security = System.getSecurityManager(); |
|
1736 |
if (security != null) { |
|
1737 |
security.checkSetFactory(); |
|
1738 |
} |
|
1739 |
factory = fac; |
|
1740 |
} |
|
1741 |
||
1742 |
/** |
|
1743 |
* Sets performance preferences for this socket. |
|
1744 |
* |
|
1745 |
* <p> Sockets use the TCP/IP protocol by default. Some implementations |
|
1746 |
* may offer alternative protocols which have different performance |
|
1747 |
* characteristics than TCP/IP. This method allows the application to |
|
1748 |
* express its own preferences as to how these tradeoffs should be made |
|
1749 |
* when the implementation chooses from the available protocols. |
|
1750 |
* |
|
1751 |
* <p> Performance preferences are described by three integers |
|
1752 |
* whose values indicate the relative importance of short connection time, |
|
1753 |
* low latency, and high bandwidth. The absolute values of the integers |
|
1754 |
* are irrelevant; in order to choose a protocol the values are simply |
|
1755 |
* compared, with larger values indicating stronger preferences. Negative |
|
1756 |
* values represent a lower priority than positive values. If the |
|
1757 |
* application prefers short connection time over both low latency and high |
|
1758 |
* bandwidth, for example, then it could invoke this method with the values |
|
19069 | 1759 |
* {@code (1, 0, 0)}. If the application prefers high bandwidth above low |
2 | 1760 |
* latency, and low latency above short connection time, then it could |
19069 | 1761 |
* invoke this method with the values {@code (0, 1, 2)}. |
2 | 1762 |
* |
1763 |
* <p> Invoking this method after this socket has been connected |
|
1764 |
* will have no effect. |
|
1765 |
* |
|
1766 |
* @param connectionTime |
|
19069 | 1767 |
* An {@code int} expressing the relative importance of a short |
2 | 1768 |
* connection time |
1769 |
* |
|
1770 |
* @param latency |
|
19069 | 1771 |
* An {@code int} expressing the relative importance of low |
2 | 1772 |
* latency |
1773 |
* |
|
1774 |
* @param bandwidth |
|
19069 | 1775 |
* An {@code int} expressing the relative importance of high |
2 | 1776 |
* bandwidth |
1777 |
* |
|
1778 |
* @since 1.5 |
|
1779 |
*/ |
|
1780 |
public void setPerformancePreferences(int connectionTime, |
|
1781 |
int latency, |
|
1782 |
int bandwidth) |
|
1783 |
{ |
|
1784 |
/* Not implemented yet */ |
|
1785 |
} |
|
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1786 |
|
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1787 |
|
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1788 |
/** |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1789 |
* Sets the value of a socket option. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1790 |
* |
28852
a581c7868768
8041395: Doclint regression in java.net.SocketOption
darcy
parents:
25859
diff
changeset
|
1791 |
* @param <T> The type of the socket option value |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1792 |
* @param name The socket option |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1793 |
* @param value The value of the socket option. A value of {@code null} |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1794 |
* may be valid for some options. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1795 |
* @return this Socket |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1796 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1797 |
* @throws UnsupportedOperationException if the socket does not support |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1798 |
* the option. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1799 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1800 |
* @throws IllegalArgumentException if the value is not valid for |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1801 |
* the option. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1802 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1803 |
* @throws IOException if an I/O error occurs, or if the socket is closed. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1804 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1805 |
* @throws NullPointerException if name is {@code null} |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1806 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1807 |
* @throws SecurityException if a security manager is set and if the socket |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1808 |
* option requires a security permission and if the caller does |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1809 |
* not have the required permission. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1810 |
* {@link java.net.StandardSocketOptions StandardSocketOptions} |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1811 |
* do not require any security permission. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1812 |
* |
35302
e4d2275861c3
8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents:
30963
diff
changeset
|
1813 |
* @since 9 |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1814 |
*/ |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1815 |
public <T> Socket setOption(SocketOption<T> name, T value) throws IOException { |
55081
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
1816 |
Objects.requireNonNull(name); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
1817 |
if (isClosed()) |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
1818 |
throw new SocketException("Socket is closed"); |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1819 |
getImpl().setOption(name, value); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1820 |
return this; |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1821 |
} |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1822 |
|
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1823 |
/** |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1824 |
* Returns the value of a socket option. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1825 |
* |
28852
a581c7868768
8041395: Doclint regression in java.net.SocketOption
darcy
parents:
25859
diff
changeset
|
1826 |
* @param <T> The type of the socket option value |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1827 |
* @param name The socket option |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1828 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1829 |
* @return The value of the socket option. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1830 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1831 |
* @throws UnsupportedOperationException if the socket does not support |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1832 |
* the option. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1833 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1834 |
* @throws IOException if an I/O error occurs, or if the socket is closed. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1835 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1836 |
* @throws NullPointerException if name is {@code null} |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1837 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1838 |
* @throws SecurityException if a security manager is set and if the socket |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1839 |
* option requires a security permission and if the caller does |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1840 |
* not have the required permission. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1841 |
* {@link java.net.StandardSocketOptions StandardSocketOptions} |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1842 |
* do not require any security permission. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1843 |
* |
35302
e4d2275861c3
8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents:
30963
diff
changeset
|
1844 |
* @since 9 |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1845 |
*/ |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1846 |
@SuppressWarnings("unchecked") |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1847 |
public <T> T getOption(SocketOption<T> name) throws IOException { |
55081
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
1848 |
Objects.requireNonNull(name); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
1849 |
if (isClosed()) |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
1850 |
throw new SocketException("Socket is closed"); |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1851 |
return getImpl().getOption(name); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1852 |
} |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1853 |
|
52456
90ff0e286a5e
8213418: Socket/ServerSocket supportedOptions does not work with custom SocketImpl
chegar
parents:
47216
diff
changeset
|
1854 |
// cache of unmodifiable impl options. Possibly set racy, in impl we trust |
90ff0e286a5e
8213418: Socket/ServerSocket supportedOptions does not work with custom SocketImpl
chegar
parents:
47216
diff
changeset
|
1855 |
private volatile Set<SocketOption<?>> options; |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1856 |
|
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1857 |
/** |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1858 |
* Returns a set of the socket options supported by this socket. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1859 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1860 |
* This method will continue to return the set of options even after |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1861 |
* the socket has been closed. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1862 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1863 |
* @return A set of the socket options supported by this socket. This set |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1864 |
* may be empty if the socket's SocketImpl cannot be created. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1865 |
* |
35302
e4d2275861c3
8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents:
30963
diff
changeset
|
1866 |
* @since 9 |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1867 |
*/ |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1868 |
public Set<SocketOption<?>> supportedOptions() { |
52456
90ff0e286a5e
8213418: Socket/ServerSocket supportedOptions does not work with custom SocketImpl
chegar
parents:
47216
diff
changeset
|
1869 |
Set<SocketOption<?>> so = options; |
90ff0e286a5e
8213418: Socket/ServerSocket supportedOptions does not work with custom SocketImpl
chegar
parents:
47216
diff
changeset
|
1870 |
if (so != null) |
90ff0e286a5e
8213418: Socket/ServerSocket supportedOptions does not work with custom SocketImpl
chegar
parents:
47216
diff
changeset
|
1871 |
return so; |
90ff0e286a5e
8213418: Socket/ServerSocket supportedOptions does not work with custom SocketImpl
chegar
parents:
47216
diff
changeset
|
1872 |
|
90ff0e286a5e
8213418: Socket/ServerSocket supportedOptions does not work with custom SocketImpl
chegar
parents:
47216
diff
changeset
|
1873 |
try { |
90ff0e286a5e
8213418: Socket/ServerSocket supportedOptions does not work with custom SocketImpl
chegar
parents:
47216
diff
changeset
|
1874 |
SocketImpl impl = getImpl(); |
90ff0e286a5e
8213418: Socket/ServerSocket supportedOptions does not work with custom SocketImpl
chegar
parents:
47216
diff
changeset
|
1875 |
options = Collections.unmodifiableSet(impl.supportedOptions()); |
90ff0e286a5e
8213418: Socket/ServerSocket supportedOptions does not work with custom SocketImpl
chegar
parents:
47216
diff
changeset
|
1876 |
} catch (IOException e) { |
90ff0e286a5e
8213418: Socket/ServerSocket supportedOptions does not work with custom SocketImpl
chegar
parents:
47216
diff
changeset
|
1877 |
options = Collections.emptySet(); |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1878 |
} |
52456
90ff0e286a5e
8213418: Socket/ServerSocket supportedOptions does not work with custom SocketImpl
chegar
parents:
47216
diff
changeset
|
1879 |
return options; |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1880 |
} |
2 | 1881 |
} |