author | michaelm |
Mon, 11 Feb 2019 15:17:31 +0000 | |
branch | niosocketimpl-branch |
changeset 57176 | 726630bc6a4c |
parent 57174 | 899641440751 |
child 57177 | 0b5ebb227a8d |
permissions | -rw-r--r-- |
2 | 1 |
/* |
36115 | 2 |
* Copyright (c) 1995, 2016, 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 |
||
57173 | 28 |
import java.io.FileDescriptor; |
29 |
import java.io.IOException; |
|
30 |
import java.io.InputStream; |
|
31 |
import java.io.OutputStream; |
|
57174 | 32 |
import java.security.AccessController; |
33 |
import java.security.PrivilegedAction; |
|
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
34 |
import java.util.Set; |
2 | 35 |
|
57174 | 36 |
import sun.net.NetProperties; |
57167
82874527373e
Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents:
47478
diff
changeset
|
37 |
import sun.nio.ch.NioSocketImpl; |
57173 | 38 |
import sun.security.action.GetPropertyAction; |
57167
82874527373e
Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents:
47478
diff
changeset
|
39 |
|
2 | 40 |
/** |
19069 | 41 |
* The abstract class {@code SocketImpl} is a common superclass |
2 | 42 |
* of all classes that actually implement sockets. It is used to |
43 |
* create both client and server sockets. |
|
44 |
* <p> |
|
45 |
* A "plain" socket implements these methods exactly as |
|
46 |
* described, without attempting to go through a firewall or proxy. |
|
47 |
* |
|
48 |
* @author unascribed |
|
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
24046
diff
changeset
|
49 |
* @since 1.0 |
2 | 50 |
*/ |
51 |
public abstract class SocketImpl implements SocketOptions { |
|
57173 | 52 |
private static final boolean USE_PLAINSOCKETIMPL = usePlainSocketImpl(); |
53 |
||
54 |
private static boolean usePlainSocketImpl() { |
|
57176 | 55 |
PrivilegedAction<Boolean> pa = () -> NetProperties.getBoolean("jdk.net.usePlainSocketImpl"); |
56 |
Boolean val = AccessController.doPrivileged(pa); |
|
57 |
return val == null ? false : val; |
|
57173 | 58 |
} |
59 |
||
60 |
/** |
|
61 |
* Creates a instance of platform's SocketImpl |
|
62 |
*/ |
|
57174 | 63 |
@SuppressWarnings("unchecked") |
57173 | 64 |
static SocketImpl createSocketImpl(boolean server) { |
65 |
if (USE_PLAINSOCKETIMPL) { |
|
66 |
return new PlainSocketImpl(); |
|
67 |
} else { |
|
68 |
return new NioSocketImpl(server); |
|
69 |
} |
|
70 |
} |
|
71 |
||
2 | 72 |
/** |
73 |
* The actual Socket object. |
|
74 |
*/ |
|
75 |
Socket socket = null; |
|
76 |
ServerSocket serverSocket = null; |
|
77 |
||
78 |
/** |
|
79 |
* The file descriptor object for this socket. |
|
80 |
*/ |
|
81 |
protected FileDescriptor fd; |
|
82 |
||
83 |
/** |
|
84 |
* The IP address of the remote end of this socket. |
|
85 |
*/ |
|
86 |
protected InetAddress address; |
|
87 |
||
88 |
/** |
|
89 |
* The port number on the remote host to which this socket is connected. |
|
90 |
*/ |
|
91 |
protected int port; |
|
92 |
||
93 |
/** |
|
94 |
* The local port number to which this socket is connected. |
|
95 |
*/ |
|
96 |
protected int localport; |
|
97 |
||
98 |
/** |
|
99 |
* Creates either a stream or a datagram socket. |
|
100 |
* |
|
19069 | 101 |
* @param stream if {@code true}, create a stream socket; |
2 | 102 |
* otherwise, create a datagram socket. |
103 |
* @exception IOException if an I/O error occurs while creating the |
|
104 |
* socket. |
|
105 |
*/ |
|
106 |
protected abstract void create(boolean stream) throws IOException; |
|
107 |
||
108 |
/** |
|
109 |
* Connects this socket to the specified port on the named host. |
|
110 |
* |
|
111 |
* @param host the name of the remote host. |
|
112 |
* @param port the port number. |
|
113 |
* @exception IOException if an I/O error occurs when connecting to the |
|
114 |
* remote host. |
|
115 |
*/ |
|
116 |
protected abstract void connect(String host, int port) throws IOException; |
|
117 |
||
118 |
/** |
|
119 |
* Connects this socket to the specified port number on the specified host. |
|
120 |
* |
|
121 |
* @param address the IP address of the remote host. |
|
122 |
* @param port the port number. |
|
123 |
* @exception IOException if an I/O error occurs when attempting a |
|
124 |
* connection. |
|
125 |
*/ |
|
126 |
protected abstract void connect(InetAddress address, int port) throws IOException; |
|
127 |
||
128 |
/** |
|
129 |
* Connects this socket to the specified port number on the specified host. |
|
130 |
* A timeout of zero is interpreted as an infinite timeout. The connection |
|
131 |
* will then block until established or an error occurs. |
|
132 |
* |
|
133 |
* @param address the Socket address of the remote host. |
|
134 |
* @param timeout the timeout value, in milliseconds, or zero for no timeout. |
|
135 |
* @exception IOException if an I/O error occurs when attempting a |
|
136 |
* connection. |
|
137 |
* @since 1.4 |
|
138 |
*/ |
|
139 |
protected abstract void connect(SocketAddress address, int timeout) throws IOException; |
|
140 |
||
141 |
/** |
|
142 |
* Binds this socket to the specified local IP address and port number. |
|
143 |
* |
|
144 |
* @param host an IP address that belongs to a local interface. |
|
145 |
* @param port the port number. |
|
146 |
* @exception IOException if an I/O error occurs when binding this socket. |
|
147 |
*/ |
|
148 |
protected abstract void bind(InetAddress host, int port) throws IOException; |
|
149 |
||
150 |
/** |
|
151 |
* Sets the maximum queue length for incoming connection indications |
|
19069 | 152 |
* (a request to connect) to the {@code count} argument. If a |
2 | 153 |
* connection indication arrives when the queue is full, the |
154 |
* connection is refused. |
|
155 |
* |
|
156 |
* @param backlog the maximum length of the queue. |
|
157 |
* @exception IOException if an I/O error occurs when creating the queue. |
|
158 |
*/ |
|
159 |
protected abstract void listen(int backlog) throws IOException; |
|
160 |
||
161 |
/** |
|
162 |
* Accepts a connection. |
|
163 |
* |
|
164 |
* @param s the accepted connection. |
|
165 |
* @exception IOException if an I/O error occurs when accepting the |
|
166 |
* connection. |
|
167 |
*/ |
|
168 |
protected abstract void accept(SocketImpl s) throws IOException; |
|
169 |
||
170 |
/** |
|
171 |
* Returns an input stream for this socket. |
|
172 |
* |
|
173 |
* @return a stream for reading from this socket. |
|
174 |
* @exception IOException if an I/O error occurs when creating the |
|
175 |
* input stream. |
|
176 |
*/ |
|
177 |
protected abstract InputStream getInputStream() throws IOException; |
|
178 |
||
179 |
/** |
|
180 |
* Returns an output stream for this socket. |
|
181 |
* |
|
182 |
* @return an output stream for writing to this socket. |
|
183 |
* @exception IOException if an I/O error occurs when creating the |
|
184 |
* output stream. |
|
185 |
*/ |
|
186 |
protected abstract OutputStream getOutputStream() throws IOException; |
|
187 |
||
188 |
/** |
|
189 |
* Returns the number of bytes that can be read from this socket |
|
190 |
* without blocking. |
|
191 |
* |
|
192 |
* @return the number of bytes that can be read from this socket |
|
193 |
* without blocking. |
|
194 |
* @exception IOException if an I/O error occurs when determining the |
|
195 |
* number of bytes available. |
|
196 |
*/ |
|
197 |
protected abstract int available() throws IOException; |
|
198 |
||
199 |
/** |
|
200 |
* Closes this socket. |
|
201 |
* |
|
202 |
* @exception IOException if an I/O error occurs when closing this socket. |
|
203 |
*/ |
|
204 |
protected abstract void close() throws IOException; |
|
205 |
||
206 |
/** |
|
207 |
* Places the input stream for this socket at "end of stream". |
|
208 |
* Any data sent to this socket is acknowledged and then |
|
209 |
* silently discarded. |
|
210 |
* |
|
10421
2ee16a0f6df5
7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
5506
diff
changeset
|
211 |
* 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:
5506
diff
changeset
|
212 |
* socket, the stream's {@code available} method will return 0, and its |
2ee16a0f6df5
7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
5506
diff
changeset
|
213 |
* {@code read} methods will return {@code -1} (end of stream). |
2 | 214 |
* |
215 |
* @exception IOException if an I/O error occurs when shutting down this |
|
216 |
* socket. |
|
217 |
* @see java.net.Socket#shutdownOutput() |
|
218 |
* @see java.net.Socket#close() |
|
219 |
* @see java.net.Socket#setSoLinger(boolean, int) |
|
220 |
* @since 1.3 |
|
221 |
*/ |
|
222 |
protected void shutdownInput() throws IOException { |
|
223 |
throw new IOException("Method not implemented!"); |
|
224 |
} |
|
225 |
||
226 |
/** |
|
227 |
* Disables the output stream for this socket. |
|
228 |
* For a TCP socket, any previously written data will be sent |
|
229 |
* followed by TCP's normal connection termination sequence. |
|
230 |
* |
|
231 |
* If you write to a socket output stream after invoking |
|
232 |
* shutdownOutput() on the socket, the stream will throw |
|
233 |
* an IOException. |
|
234 |
* |
|
235 |
* @exception IOException if an I/O error occurs when shutting down this |
|
236 |
* socket. |
|
237 |
* @see java.net.Socket#shutdownInput() |
|
238 |
* @see java.net.Socket#close() |
|
239 |
* @see java.net.Socket#setSoLinger(boolean, int) |
|
240 |
* @since 1.3 |
|
241 |
*/ |
|
242 |
protected void shutdownOutput() throws IOException { |
|
243 |
throw new IOException("Method not implemented!"); |
|
244 |
} |
|
245 |
||
246 |
/** |
|
19069 | 247 |
* Returns the value of this socket's {@code fd} field. |
2 | 248 |
* |
19069 | 249 |
* @return the value of this socket's {@code fd} field. |
2 | 250 |
* @see java.net.SocketImpl#fd |
251 |
*/ |
|
252 |
protected FileDescriptor getFileDescriptor() { |
|
253 |
return fd; |
|
254 |
} |
|
255 |
||
256 |
/** |
|
19069 | 257 |
* Returns the value of this socket's {@code address} field. |
2 | 258 |
* |
19069 | 259 |
* @return the value of this socket's {@code address} field. |
2 | 260 |
* @see java.net.SocketImpl#address |
261 |
*/ |
|
262 |
protected InetAddress getInetAddress() { |
|
263 |
return address; |
|
264 |
} |
|
265 |
||
266 |
/** |
|
19069 | 267 |
* Returns the value of this socket's {@code port} field. |
2 | 268 |
* |
19069 | 269 |
* @return the value of this socket's {@code port} field. |
2 | 270 |
* @see java.net.SocketImpl#port |
271 |
*/ |
|
272 |
protected int getPort() { |
|
273 |
return port; |
|
274 |
} |
|
275 |
||
276 |
/** |
|
277 |
* Returns whether or not this SocketImpl supports sending |
|
278 |
* urgent data. By default, false is returned |
|
279 |
* unless the method is overridden in a sub-class |
|
280 |
* |
|
281 |
* @return true if urgent data supported |
|
282 |
* @see java.net.SocketImpl#address |
|
283 |
* @since 1.4 |
|
284 |
*/ |
|
285 |
protected boolean supportsUrgentData () { |
|
286 |
return false; // must be overridden in sub-class |
|
287 |
} |
|
288 |
||
289 |
/** |
|
290 |
* Send one byte of urgent data on the socket. |
|
291 |
* The byte to be sent is the low eight bits of the parameter |
|
292 |
* @param data The byte of data to send |
|
293 |
* @exception IOException if there is an error |
|
294 |
* sending the data. |
|
295 |
* @since 1.4 |
|
296 |
*/ |
|
297 |
protected abstract void sendUrgentData (int data) throws IOException; |
|
298 |
||
299 |
/** |
|
19069 | 300 |
* Returns the value of this socket's {@code localport} field. |
2 | 301 |
* |
19069 | 302 |
* @return the value of this socket's {@code localport} field. |
2 | 303 |
* @see java.net.SocketImpl#localport |
304 |
*/ |
|
305 |
protected int getLocalPort() { |
|
306 |
return localport; |
|
307 |
} |
|
308 |
||
309 |
void setSocket(Socket soc) { |
|
310 |
this.socket = soc; |
|
311 |
} |
|
312 |
||
313 |
Socket getSocket() { |
|
314 |
return socket; |
|
315 |
} |
|
316 |
||
317 |
void setServerSocket(ServerSocket soc) { |
|
318 |
this.serverSocket = soc; |
|
319 |
} |
|
320 |
||
321 |
ServerSocket getServerSocket() { |
|
322 |
return serverSocket; |
|
323 |
} |
|
324 |
||
325 |
/** |
|
19069 | 326 |
* Returns the address and port of this socket as a {@code String}. |
2 | 327 |
* |
328 |
* @return a string representation of this socket. |
|
329 |
*/ |
|
330 |
public String toString() { |
|
331 |
return "Socket[addr=" + getInetAddress() + |
|
332 |
",port=" + getPort() + ",localport=" + getLocalPort() + "]"; |
|
333 |
} |
|
334 |
||
335 |
void reset() throws IOException { |
|
336 |
address = null; |
|
337 |
port = 0; |
|
338 |
localport = 0; |
|
339 |
} |
|
340 |
||
341 |
/** |
|
342 |
* Sets performance preferences for this socket. |
|
343 |
* |
|
344 |
* <p> Sockets use the TCP/IP protocol by default. Some implementations |
|
345 |
* may offer alternative protocols which have different performance |
|
346 |
* characteristics than TCP/IP. This method allows the application to |
|
347 |
* express its own preferences as to how these tradeoffs should be made |
|
348 |
* when the implementation chooses from the available protocols. |
|
349 |
* |
|
350 |
* <p> Performance preferences are described by three integers |
|
351 |
* whose values indicate the relative importance of short connection time, |
|
352 |
* low latency, and high bandwidth. The absolute values of the integers |
|
353 |
* are irrelevant; in order to choose a protocol the values are simply |
|
354 |
* compared, with larger values indicating stronger preferences. Negative |
|
355 |
* values represent a lower priority than positive values. If the |
|
356 |
* application prefers short connection time over both low latency and high |
|
357 |
* bandwidth, for example, then it could invoke this method with the values |
|
19069 | 358 |
* {@code (1, 0, 0)}. If the application prefers high bandwidth above low |
2 | 359 |
* latency, and low latency above short connection time, then it could |
19069 | 360 |
* invoke this method with the values {@code (0, 1, 2)}. |
2 | 361 |
* |
47478 | 362 |
* By default, this method does nothing, unless it is overridden in |
2 | 363 |
* a sub-class. |
364 |
* |
|
365 |
* @param connectionTime |
|
19069 | 366 |
* An {@code int} expressing the relative importance of a short |
2 | 367 |
* connection time |
368 |
* |
|
369 |
* @param latency |
|
19069 | 370 |
* An {@code int} expressing the relative importance of low |
2 | 371 |
* latency |
372 |
* |
|
373 |
* @param bandwidth |
|
19069 | 374 |
* An {@code int} expressing the relative importance of high |
2 | 375 |
* bandwidth |
376 |
* |
|
377 |
* @since 1.5 |
|
378 |
*/ |
|
379 |
protected void setPerformancePreferences(int connectionTime, |
|
380 |
int latency, |
|
381 |
int bandwidth) |
|
382 |
{ |
|
383 |
/* Not implemented yet */ |
|
384 |
} |
|
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
385 |
|
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
386 |
/** |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
387 |
* Called to set a socket option. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
388 |
* |
28852
a581c7868768
8041395: Doclint regression in java.net.SocketOption
darcy
parents:
27341
diff
changeset
|
389 |
* @param <T> The type of the socket option value |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
390 |
* @param name The socket option |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
391 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
392 |
* @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:
19069
diff
changeset
|
393 |
* may be valid for some options. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
394 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
395 |
* @throws UnsupportedOperationException if the SocketImpl does not |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
396 |
* support the option |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
397 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
398 |
* @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:
19069
diff
changeset
|
399 |
* |
35302
e4d2275861c3
8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents:
34968
diff
changeset
|
400 |
* @since 9 |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
401 |
*/ |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
402 |
protected <T> void setOption(SocketOption<T> name, T value) throws IOException { |
34968
93b315c61ca3
8143554: UnsupportedOperationException is not thrown for unsupported options
kshefov
parents:
28852
diff
changeset
|
403 |
if (name == StandardSocketOptions.SO_KEEPALIVE && |
93b315c61ca3
8143554: UnsupportedOperationException is not thrown for unsupported options
kshefov
parents:
28852
diff
changeset
|
404 |
(getSocket() != null)) { |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
405 |
setOption(SocketOptions.SO_KEEPALIVE, value); |
34968
93b315c61ca3
8143554: UnsupportedOperationException is not thrown for unsupported options
kshefov
parents:
28852
diff
changeset
|
406 |
} else if (name == StandardSocketOptions.SO_SNDBUF && |
93b315c61ca3
8143554: UnsupportedOperationException is not thrown for unsupported options
kshefov
parents:
28852
diff
changeset
|
407 |
(getSocket() != null)) { |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
408 |
setOption(SocketOptions.SO_SNDBUF, value); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
409 |
} else if (name == StandardSocketOptions.SO_RCVBUF) { |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
410 |
setOption(SocketOptions.SO_RCVBUF, value); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
411 |
} else if (name == StandardSocketOptions.SO_REUSEADDR) { |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
412 |
setOption(SocketOptions.SO_REUSEADDR, value); |
36115 | 413 |
} else if (name == StandardSocketOptions.SO_REUSEPORT && |
414 |
supportedOptions().contains(name)) { |
|
415 |
setOption(SocketOptions.SO_REUSEPORT, value); |
|
34968
93b315c61ca3
8143554: UnsupportedOperationException is not thrown for unsupported options
kshefov
parents:
28852
diff
changeset
|
416 |
} else if (name == StandardSocketOptions.SO_LINGER && |
93b315c61ca3
8143554: UnsupportedOperationException is not thrown for unsupported options
kshefov
parents:
28852
diff
changeset
|
417 |
(getSocket() != null)) { |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
418 |
setOption(SocketOptions.SO_LINGER, value); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
419 |
} else if (name == StandardSocketOptions.IP_TOS) { |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
420 |
setOption(SocketOptions.IP_TOS, value); |
34968
93b315c61ca3
8143554: UnsupportedOperationException is not thrown for unsupported options
kshefov
parents:
28852
diff
changeset
|
421 |
} else if (name == StandardSocketOptions.TCP_NODELAY && |
93b315c61ca3
8143554: UnsupportedOperationException is not thrown for unsupported options
kshefov
parents:
28852
diff
changeset
|
422 |
(getSocket() != null)) { |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
423 |
setOption(SocketOptions.TCP_NODELAY, value); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
424 |
} else { |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
425 |
throw new UnsupportedOperationException("unsupported option"); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
426 |
} |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
427 |
} |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
428 |
|
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
429 |
/** |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
430 |
* Called to get a socket option. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
431 |
* |
28852
a581c7868768
8041395: Doclint regression in java.net.SocketOption
darcy
parents:
27341
diff
changeset
|
432 |
* @param <T> The type of the socket option value |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
433 |
* @param name The socket option |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
434 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
435 |
* @return the value of the named option |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
436 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
437 |
* @throws UnsupportedOperationException if the SocketImpl does not |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
438 |
* support the option. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
439 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
440 |
* @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:
19069
diff
changeset
|
441 |
* |
35302
e4d2275861c3
8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents:
34968
diff
changeset
|
442 |
* @since 9 |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
443 |
*/ |
24046
d1f6da65881a
8041397: Lint regression in java.net.SocketOption
michaelm
parents:
23879
diff
changeset
|
444 |
@SuppressWarnings("unchecked") |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
445 |
protected <T> T getOption(SocketOption<T> name) throws IOException { |
34968
93b315c61ca3
8143554: UnsupportedOperationException is not thrown for unsupported options
kshefov
parents:
28852
diff
changeset
|
446 |
if (name == StandardSocketOptions.SO_KEEPALIVE && |
93b315c61ca3
8143554: UnsupportedOperationException is not thrown for unsupported options
kshefov
parents:
28852
diff
changeset
|
447 |
(getSocket() != null)) { |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
448 |
return (T)getOption(SocketOptions.SO_KEEPALIVE); |
34968
93b315c61ca3
8143554: UnsupportedOperationException is not thrown for unsupported options
kshefov
parents:
28852
diff
changeset
|
449 |
} else if (name == StandardSocketOptions.SO_SNDBUF && |
93b315c61ca3
8143554: UnsupportedOperationException is not thrown for unsupported options
kshefov
parents:
28852
diff
changeset
|
450 |
(getSocket() != null)) { |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
451 |
return (T)getOption(SocketOptions.SO_SNDBUF); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
452 |
} else if (name == StandardSocketOptions.SO_RCVBUF) { |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
453 |
return (T)getOption(SocketOptions.SO_RCVBUF); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
454 |
} else if (name == StandardSocketOptions.SO_REUSEADDR) { |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
455 |
return (T)getOption(SocketOptions.SO_REUSEADDR); |
36115 | 456 |
} else if (name == StandardSocketOptions.SO_REUSEPORT && |
457 |
supportedOptions().contains(name)) { |
|
458 |
return (T)getOption(SocketOptions.SO_REUSEPORT); |
|
34968
93b315c61ca3
8143554: UnsupportedOperationException is not thrown for unsupported options
kshefov
parents:
28852
diff
changeset
|
459 |
} else if (name == StandardSocketOptions.SO_LINGER && |
93b315c61ca3
8143554: UnsupportedOperationException is not thrown for unsupported options
kshefov
parents:
28852
diff
changeset
|
460 |
(getSocket() != null)) { |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
461 |
return (T)getOption(SocketOptions.SO_LINGER); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
462 |
} else if (name == StandardSocketOptions.IP_TOS) { |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
463 |
return (T)getOption(SocketOptions.IP_TOS); |
34968
93b315c61ca3
8143554: UnsupportedOperationException is not thrown for unsupported options
kshefov
parents:
28852
diff
changeset
|
464 |
} else if (name == StandardSocketOptions.TCP_NODELAY && |
93b315c61ca3
8143554: UnsupportedOperationException is not thrown for unsupported options
kshefov
parents:
28852
diff
changeset
|
465 |
(getSocket() != null)) { |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
466 |
return (T)getOption(SocketOptions.TCP_NODELAY); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
467 |
} else { |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
468 |
throw new UnsupportedOperationException("unsupported option"); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
469 |
} |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
470 |
} |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
471 |
|
36228
a16a22218e23
8148609: socket impl supportedOptions() should return an immutable set
vtewari
parents:
36115
diff
changeset
|
472 |
private static final Set<SocketOption<?>> socketOptions; |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
473 |
|
36228
a16a22218e23
8148609: socket impl supportedOptions() should return an immutable set
vtewari
parents:
36115
diff
changeset
|
474 |
private static final Set<SocketOption<?>> serverSocketOptions; |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
475 |
|
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
476 |
static { |
36228
a16a22218e23
8148609: socket impl supportedOptions() should return an immutable set
vtewari
parents:
36115
diff
changeset
|
477 |
socketOptions = Set.of(StandardSocketOptions.SO_KEEPALIVE, |
a16a22218e23
8148609: socket impl supportedOptions() should return an immutable set
vtewari
parents:
36115
diff
changeset
|
478 |
StandardSocketOptions.SO_SNDBUF, |
a16a22218e23
8148609: socket impl supportedOptions() should return an immutable set
vtewari
parents:
36115
diff
changeset
|
479 |
StandardSocketOptions.SO_RCVBUF, |
a16a22218e23
8148609: socket impl supportedOptions() should return an immutable set
vtewari
parents:
36115
diff
changeset
|
480 |
StandardSocketOptions.SO_REUSEADDR, |
a16a22218e23
8148609: socket impl supportedOptions() should return an immutable set
vtewari
parents:
36115
diff
changeset
|
481 |
StandardSocketOptions.SO_LINGER, |
a16a22218e23
8148609: socket impl supportedOptions() should return an immutable set
vtewari
parents:
36115
diff
changeset
|
482 |
StandardSocketOptions.IP_TOS, |
a16a22218e23
8148609: socket impl supportedOptions() should return an immutable set
vtewari
parents:
36115
diff
changeset
|
483 |
StandardSocketOptions.TCP_NODELAY); |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
484 |
|
36228
a16a22218e23
8148609: socket impl supportedOptions() should return an immutable set
vtewari
parents:
36115
diff
changeset
|
485 |
serverSocketOptions = Set.of(StandardSocketOptions.SO_RCVBUF, |
a16a22218e23
8148609: socket impl supportedOptions() should return an immutable set
vtewari
parents:
36115
diff
changeset
|
486 |
StandardSocketOptions.SO_REUSEADDR, |
a16a22218e23
8148609: socket impl supportedOptions() should return an immutable set
vtewari
parents:
36115
diff
changeset
|
487 |
StandardSocketOptions.IP_TOS); |
a16a22218e23
8148609: socket impl supportedOptions() should return an immutable set
vtewari
parents:
36115
diff
changeset
|
488 |
} |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
489 |
|
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
490 |
/** |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
491 |
* Returns a set of SocketOptions supported by this impl |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
492 |
* and by this impl's socket (Socket or ServerSocket) |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
493 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
494 |
* @return a Set of SocketOptions |
36228
a16a22218e23
8148609: socket impl supportedOptions() should return an immutable set
vtewari
parents:
36115
diff
changeset
|
495 |
* |
a16a22218e23
8148609: socket impl supportedOptions() should return an immutable set
vtewari
parents:
36115
diff
changeset
|
496 |
* @since 9 |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
497 |
*/ |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
498 |
protected Set<SocketOption<?>> supportedOptions() { |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
499 |
if (getSocket() != null) { |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
500 |
return socketOptions; |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
501 |
} else { |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
502 |
return serverSocketOptions; |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
503 |
} |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
504 |
} |
2 | 505 |
} |