Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/sun/nio/ch/ServerSocketAdaptor.java
41159 views
1
/*
2
* Copyright (c) 2000, 2021, 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 sun.nio.ch;
27
28
import java.io.IOException;
29
import java.net.InetAddress;
30
import java.net.InetSocketAddress;
31
import java.net.ServerSocket;
32
import java.net.Socket;
33
import java.net.SocketAddress;
34
import java.net.SocketException;
35
import java.net.SocketOption;
36
import java.net.StandardSocketOptions;
37
import java.nio.channels.IllegalBlockingModeException;
38
import java.nio.channels.ServerSocketChannel;
39
import java.nio.channels.SocketChannel;
40
import java.security.AccessController;
41
import java.security.PrivilegedActionException;
42
import java.security.PrivilegedExceptionAction;
43
import java.util.Set;
44
45
import static java.util.concurrent.TimeUnit.MILLISECONDS;
46
47
48
// Make a server-socket channel look like a server socket.
49
//
50
// The methods in this class are defined in exactly the same order as in
51
// java.net.ServerSocket so as to simplify tracking future changes to that
52
// class.
53
//
54
55
class ServerSocketAdaptor // package-private
56
extends ServerSocket
57
{
58
// The channel being adapted
59
private final ServerSocketChannelImpl ssc;
60
61
// Timeout "option" value for accepts
62
private volatile int timeout;
63
64
@SuppressWarnings("removal")
65
static ServerSocket create(ServerSocketChannelImpl ssc) {
66
PrivilegedExceptionAction<ServerSocket> pa = () -> new ServerSocketAdaptor(ssc);
67
try {
68
return AccessController.doPrivileged(pa);
69
} catch (PrivilegedActionException pae) {
70
throw new InternalError("Should not reach here", pae);
71
}
72
}
73
74
private ServerSocketAdaptor(ServerSocketChannelImpl ssc) {
75
super(DummySocketImpl.create());
76
this.ssc = ssc;
77
}
78
79
@Override
80
public void bind(SocketAddress local) throws IOException {
81
bind(local, 50);
82
}
83
84
@Override
85
public void bind(SocketAddress local, int backlog) throws IOException {
86
if (local == null)
87
local = new InetSocketAddress(0);
88
try {
89
ssc.bind(local, backlog);
90
} catch (Exception x) {
91
Net.translateException(x);
92
}
93
}
94
95
@Override
96
public InetAddress getInetAddress() {
97
SocketAddress local = ssc.localAddress();
98
if (local == null) {
99
return null;
100
} else {
101
return Net.getRevealedLocalAddress(local).getAddress();
102
}
103
}
104
105
@Override
106
public int getLocalPort() {
107
InetSocketAddress local = (InetSocketAddress) ssc.localAddress();
108
if (local == null) {
109
return -1;
110
} else {
111
return local.getPort();
112
}
113
}
114
115
@Override
116
public Socket accept() throws IOException {
117
SocketChannel sc = null;
118
try {
119
int timeout = this.timeout;
120
if (timeout > 0) {
121
long nanos = MILLISECONDS.toNanos(timeout);
122
sc = ssc.blockingAccept(nanos);
123
} else {
124
// accept connection if possible when non-blocking (to preserve
125
// long standing behavior)
126
sc = ssc.accept();
127
if (sc == null) {
128
throw new IllegalBlockingModeException();
129
}
130
}
131
} catch (Exception e) {
132
Net.translateException(e);
133
}
134
return sc.socket();
135
}
136
137
@Override
138
public void close() throws IOException {
139
ssc.close();
140
}
141
142
@Override
143
public ServerSocketChannel getChannel() {
144
return ssc;
145
}
146
147
@Override
148
public boolean isBound() {
149
return ssc.isBound();
150
}
151
152
@Override
153
public boolean isClosed() {
154
return !ssc.isOpen();
155
}
156
157
@Override
158
public void setSoTimeout(int timeout) throws SocketException {
159
if (!ssc.isOpen())
160
throw new SocketException("Socket is closed");
161
if (timeout < 0)
162
throw new IllegalArgumentException("timeout < 0");
163
this.timeout = timeout;
164
}
165
166
@Override
167
public int getSoTimeout() throws SocketException {
168
if (!ssc.isOpen())
169
throw new SocketException("Socket is closed");
170
return timeout;
171
}
172
173
@Override
174
public void setReuseAddress(boolean on) throws SocketException {
175
try {
176
ssc.setOption(StandardSocketOptions.SO_REUSEADDR, on);
177
} catch (IOException x) {
178
Net.translateToSocketException(x);
179
}
180
}
181
182
@Override
183
public boolean getReuseAddress() throws SocketException {
184
try {
185
return ssc.getOption(StandardSocketOptions.SO_REUSEADDR).booleanValue();
186
} catch (IOException x) {
187
Net.translateToSocketException(x);
188
return false; // Never happens
189
}
190
}
191
192
@Override
193
public String toString() {
194
if (!isBound())
195
return "ServerSocket[unbound]";
196
return "ServerSocket[addr=" + getInetAddress() +
197
",localport=" + getLocalPort() + "]";
198
}
199
200
@Override
201
public void setReceiveBufferSize(int size) throws SocketException {
202
// size 0 valid for ServerSocketChannel, invalid for ServerSocket
203
if (size <= 0)
204
throw new IllegalArgumentException("size cannot be 0 or negative");
205
try {
206
ssc.setOption(StandardSocketOptions.SO_RCVBUF, size);
207
} catch (IOException x) {
208
Net.translateToSocketException(x);
209
}
210
}
211
212
@Override
213
public int getReceiveBufferSize() throws SocketException {
214
try {
215
return ssc.getOption(StandardSocketOptions.SO_RCVBUF).intValue();
216
} catch (IOException x) {
217
Net.translateToSocketException(x);
218
return -1; // Never happens
219
}
220
}
221
222
@Override
223
public <T> ServerSocket setOption(SocketOption<T> name, T value) throws IOException {
224
ssc.setOption(name, value);
225
return this;
226
}
227
228
@Override
229
public <T> T getOption(SocketOption<T> name) throws IOException {
230
return ssc.getOption(name);
231
}
232
233
@Override
234
public Set<SocketOption<?>> supportedOptions() {
235
return ssc.supportedOptions();
236
}
237
}
238
239