Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/java/nio/channels/ServerSocketChannel.java
41159 views
1
/*
2
* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
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
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
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
*
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.
24
*/
25
26
package java.nio.channels;
27
28
import java.io.IOException;
29
import java.net.InetSocketAddress;
30
import java.net.NetPermission;
31
import java.net.ProtocolFamily;
32
import java.net.ServerSocket;
33
import java.net.SocketOption;
34
import java.net.SocketAddress;
35
import java.net.UnixDomainSocketAddress;
36
import java.nio.channels.spi.AbstractSelectableChannel;
37
import java.nio.channels.spi.SelectorProvider;
38
import static java.util.Objects.requireNonNull;
39
40
/**
41
* A selectable channel for stream-oriented listening sockets.
42
*
43
* <p> A server-socket channel is created by invoking one of the {@code open}
44
* methods of this class. The no-arg {@link #open() open} method opens a server-socket
45
* channel for an <i>Internet protocol</i> socket. The {@link #open(ProtocolFamily)}
46
* method is used to open a server-socket channel for a socket of a specified
47
* protocol family. It is not possible to create a channel for an arbitrary,
48
* pre-existing socket. A newly-created server-socket channel is open but not yet
49
* bound. An attempt to invoke the {@link #accept() accept} method of an
50
* unbound server-socket channel will cause a {@link NotYetBoundException}
51
* to be thrown. A server-socket channel can be bound by invoking one of the
52
* {@link #bind(java.net.SocketAddress, int) bind} methods defined by this class.
53
*
54
* <p> Socket options are configured using the {@link #setOption(SocketOption,Object)
55
* setOption} method. Server-socket channels for <i>Internet protocol</i> sockets
56
* support the following options:
57
* <blockquote>
58
* <table class="striped">
59
* <caption style="display:none">Socket options</caption>
60
* <thead>
61
* <tr>
62
* <th scope="col">Option Name</th>
63
* <th scope="col">Description</th>
64
* </tr>
65
* </thead>
66
* <tbody>
67
* <tr>
68
* <th scope="row"> {@link java.net.StandardSocketOptions#SO_RCVBUF SO_RCVBUF} </th>
69
* <td> The size of the socket receive buffer </td>
70
* </tr>
71
* <tr>
72
* <th scope="row"> {@link java.net.StandardSocketOptions#SO_REUSEADDR SO_REUSEADDR} </th>
73
* <td> Re-use address </td>
74
* </tr>
75
* </tbody>
76
* </table>
77
* </blockquote>
78
*
79
* <p> Server-socket channels for <i>Unix domain</i> sockets support:
80
* <blockquote>
81
* <table class="striped">
82
* <caption style="display:none">Socket options</caption>
83
* <thead>
84
* <tr>
85
* <th scope="col">Option Name</th>
86
* <th scope="col">Description</th>
87
* </tr>
88
* </thead>
89
* <tbody>
90
* <tr>
91
* <th scope="row"> {@link java.net.StandardSocketOptions#SO_RCVBUF SO_RCVBUF} </th>
92
* <td> The size of the socket receive buffer </td>
93
* </tr>
94
* </tbody>
95
* </table>
96
* </blockquote>
97
*
98
* <p> Additional (implementation specific) options may also be supported.
99
*
100
* <p> Server-socket channels are safe for use by multiple concurrent threads.
101
* </p>
102
*
103
* @author Mark Reinhold
104
* @author JSR-51 Expert Group
105
* @since 1.4
106
*/
107
108
public abstract class ServerSocketChannel
109
extends AbstractSelectableChannel
110
implements NetworkChannel
111
{
112
113
/**
114
* Initializes a new instance of this class.
115
*
116
* @param provider
117
* The provider that created this channel
118
*/
119
protected ServerSocketChannel(SelectorProvider provider) {
120
super(provider);
121
}
122
123
/**
124
* Opens a server-socket channel for an <i>Internet protocol</i> socket.
125
*
126
* <p> The new channel is created by invoking the {@link
127
* java.nio.channels.spi.SelectorProvider#openServerSocketChannel
128
* openServerSocketChannel} method of the system-wide default {@link
129
* java.nio.channels.spi.SelectorProvider} object.
130
*
131
* <p> The new channel's socket is initially unbound; it must be bound to a
132
* specific address via one of its socket's {@link
133
* java.net.ServerSocket#bind(SocketAddress) bind} methods before
134
* connections can be accepted. </p>
135
*
136
* @return A new socket channel
137
*
138
* @throws IOException
139
* If an I/O error occurs
140
*
141
* @see <a href="../../net/doc-files/net-properties.html#Ipv4IPv6">
142
* java.net.preferIPv4Stack</a> system property
143
*/
144
public static ServerSocketChannel open() throws IOException {
145
return SelectorProvider.provider().openServerSocketChannel();
146
}
147
148
/**
149
* Opens a server-socket channel. The {@code family} parameter specifies the
150
* {@link ProtocolFamily protocol family} of the channel's socket.
151
*
152
* <p> The new channel is created by invoking the {@link
153
* java.nio.channels.spi.SelectorProvider#openServerSocketChannel(ProtocolFamily)
154
* openServerSocketChannel(ProtocolFamily)} method of the system-wide default {@link
155
* java.nio.channels.spi.SelectorProvider} object. </p>
156
*
157
* @param family
158
* The protocol family
159
*
160
* @return A new socket channel
161
*
162
* @throws UnsupportedOperationException
163
* If the specified protocol family is not supported. For example,
164
* suppose the parameter is specified as {@link
165
* java.net.StandardProtocolFamily#INET6 StandardProtocolFamily.INET6}
166
* but IPv6 is not enabled on the platform.
167
* @throws IOException
168
* If an I/O error occurs
169
*
170
* @see <a href="../../net/doc-files/net-properties.html#Ipv4IPv6">
171
* java.net.preferIPv4Stack</a> system property
172
*
173
* @since 15
174
*/
175
public static ServerSocketChannel open(ProtocolFamily family) throws IOException {
176
return SelectorProvider.provider().openServerSocketChannel(requireNonNull(family));
177
}
178
179
/**
180
* Returns an operation set identifying this channel's supported
181
* operations.
182
*
183
* <p> Server-socket channels only support the accepting of new
184
* connections, so this method returns {@link SelectionKey#OP_ACCEPT}.
185
* </p>
186
*
187
* @return The valid-operation set
188
*/
189
public final int validOps() {
190
return SelectionKey.OP_ACCEPT;
191
}
192
193
194
// -- ServerSocket-specific operations --
195
196
/**
197
* Binds the channel's socket to a local address and configures the socket
198
* to listen for connections.
199
*
200
* <p> An invocation of this method is equivalent to the following:
201
* <blockquote><pre>
202
* bind(local, 0);
203
* </pre></blockquote>
204
*
205
* @param local
206
* The local address to bind the socket, or {@code null} to bind
207
* to an automatically assigned socket address
208
*
209
* @return This channel
210
*
211
* @throws AlreadyBoundException {@inheritDoc}
212
* @throws UnsupportedAddressTypeException {@inheritDoc}
213
* @throws ClosedChannelException {@inheritDoc}
214
* @throws IOException {@inheritDoc}
215
* @throws SecurityException
216
* If a security manager has been installed and it denies the
217
* operation
218
*
219
* @since 1.7
220
*/
221
public final ServerSocketChannel bind(SocketAddress local)
222
throws IOException
223
{
224
return bind(local, 0);
225
}
226
227
/**
228
* Binds the channel's socket to a local address and configures the socket to
229
* listen for connections.
230
*
231
* <p> This method is used to establish an association between the socket and
232
* a local address. For <i>Internet protocol</i> sockets, once an association
233
* is established then the socket remains bound until the channel is closed.
234
*
235
* <p> The {@code backlog} parameter is the maximum number of pending
236
* connections on the socket. Its exact semantics are implementation specific.
237
* In particular, an implementation may impose a maximum length or may choose
238
* to ignore the parameter altogether. If the {@code backlog} parameter has
239
* the value {@code 0}, or a negative value, then an implementation specific
240
* default is used.
241
*
242
* @apiNote
243
* Binding a server socket channel for a <i>Unix Domain</i> socket, creates a
244
* file corresponding to the file path in the {@link UnixDomainSocketAddress}.
245
* This file persists after the channel is closed, and must be removed before
246
* another socket can bind to the same name. Binding to a {@code null} address
247
* causes the socket to be <i>automatically</i> bound to some unique file
248
* in a system temporary location. The associated socket file also persists
249
* after the channel is closed. Its name can be obtained from the channel's
250
* local socket address.
251
*
252
* @implNote
253
* Each platform enforces an implementation specific, maximum length for the
254
* name of a <i>Unix Domain</i> socket. This limitation is enforced when a
255
* channel is bound. The maximum length is typically close to and generally
256
* not less than 100 bytes. This limitation also applies to <i>automatically</i>
257
* bound server socket channels. See the <i>Unix domain</i>
258
* <a href="../../net/doc-files/net-properties.html#Unixdomain">networking
259
* properties</a> that can be used to select the temporary directory where
260
* these sockets are created.
261
*
262
* @param local
263
* The address to bind the socket, or {@code null} to bind to
264
* an automatically assigned socket address
265
* @param backlog
266
* The maximum number of pending connections
267
*
268
* @return This channel
269
*
270
* @throws AlreadyBoundException
271
* If the socket is already bound
272
* @throws UnsupportedAddressTypeException
273
* If the type of the given address is not supported
274
* @throws ClosedChannelException
275
* If this channel is closed
276
* @throws IOException
277
* If some other I/O error occurs
278
* @throws SecurityException
279
* If a security manager has been installed and its {@link
280
* SecurityManager#checkListen checkListen} method denies
281
* the operation for an <i>Internet protocol</i> socket address,
282
* or for a <i>Unix domain</i> socket address if it denies
283
* {@link NetPermission}{@code("accessUnixDomainSocket")}.
284
*
285
* @since 1.7
286
*/
287
public abstract ServerSocketChannel bind(SocketAddress local, int backlog)
288
throws IOException;
289
290
/**
291
* @throws UnsupportedOperationException {@inheritDoc}
292
* @throws IllegalArgumentException {@inheritDoc}
293
* @throws ClosedChannelException {@inheritDoc}
294
* @throws IOException {@inheritDoc}
295
*
296
* @since 1.7
297
*/
298
public abstract <T> ServerSocketChannel setOption(SocketOption<T> name, T value)
299
throws IOException;
300
301
/**
302
* Retrieves a server socket associated with this channel.
303
*
304
* <p> The returned object will not declare any public methods that are not
305
* declared in the {@link java.net.ServerSocket} class. </p>
306
*
307
* @return A server socket associated with this channel
308
*
309
* @throws UnsupportedOperationException
310
* If the channel's socket is not an <i>Internet protocol</i> socket
311
*/
312
public abstract ServerSocket socket();
313
314
/**
315
* Accepts a connection made to this channel's socket.
316
*
317
* <p> If this channel is in non-blocking mode then this method will
318
* immediately return {@code null} if there are no pending connections.
319
* Otherwise it will block indefinitely until a new connection is available
320
* or an I/O error occurs.
321
*
322
* <p> The socket channel returned by this method, if any, will be in
323
* blocking mode regardless of the blocking mode of this channel.
324
*
325
* <p> If bound to an <i>Internet protocol</i> socket address, this method
326
* performs exactly the same security checks as the {@link
327
* java.net.ServerSocket#accept accept} method of the {@link java.net.ServerSocket}
328
* class. That is, if a security manager has been installed then for each
329
* new connection this method verifies that the address and port number
330
* of the connection's remote endpoint are permitted by the security
331
* manager's {@link java.lang.SecurityManager#checkAccept checkAccept}
332
* method. If bound to a <i>Unix Domain</i> socket address, this method checks
333
* {@link NetPermission}{@code ("accessUnixDomainSocket")}.
334
*
335
* @return The socket channel for the new connection,
336
* or {@code null} if this channel is in non-blocking mode
337
* and no connection is available to be accepted
338
*
339
* @throws ClosedChannelException
340
* If this channel is closed
341
*
342
* @throws AsynchronousCloseException
343
* If another thread closes this channel
344
* while the accept operation is in progress
345
*
346
* @throws ClosedByInterruptException
347
* If another thread interrupts the current thread
348
* while the accept operation is in progress, thereby
349
* closing the channel and setting the current thread's
350
* interrupt status
351
*
352
* @throws NotYetBoundException
353
* If this channel's socket has not yet been bound
354
*
355
* @throws SecurityException
356
* If a security manager has been installed and this
357
* channel is bound to an {@link InetSocketAddress}
358
* and the security manager denies access to the remote endpoint
359
* of the new connection, or if this channel is bound to a
360
* {@link UnixDomainSocketAddress} and the security manager
361
* denies {@link NetPermission}{@code ("accessUnixDomainSocket")}
362
*
363
* @throws IOException
364
* If some other I/O error occurs
365
*/
366
public abstract SocketChannel accept() throws IOException;
367
368
/**
369
* {@inheritDoc}
370
*
371
* If there is a security manager set, its {@code checkConnect} method is
372
* called with the local address and {@code -1} as its arguments to see
373
* if the operation is allowed. If the operation is not allowed,
374
* a {@code SocketAddress} representing the
375
* {@link java.net.InetAddress#getLoopbackAddress loopback} address and the
376
* local port of the channel's socket is returned.
377
*
378
* <p> Where the channel is bound to a <i>Unix Domain</i> socket address, the socket
379
* address is a {@link UnixDomainSocketAddress}. If there is a security manager
380
* set, its {@link SecurityManager#checkPermission(java.security.Permission)
381
* checkPermission} method is called with {@link NetPermission}{@code
382
* ("accessUnixDomainSocket")}. If the operation is not allowed an unnamed
383
* {@link UnixDomainSocketAddress} is returned.
384
*
385
* @return The {@code SocketAddress} that the socket is bound to, or the
386
* {@code SocketAddress} representing the loopback address or empty
387
* path if denied by the security manager, or {@code null} if the
388
* channel's socket is not bound
389
*
390
* @throws ClosedChannelException {@inheritDoc}
391
* @throws IOException {@inheritDoc}
392
*/
393
@Override
394
public abstract SocketAddress getLocalAddress() throws IOException;
395
}
396
397