Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/nio/ch/ServerSocketAdaptor.java
38918 views
1
/*
2
* Copyright (c) 2000, 2018, 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.SocketTimeoutException;
36
import java.net.StandardSocketOptions;
37
import java.nio.channels.ClosedChannelException;
38
import java.nio.channels.IllegalBlockingModeException;
39
import java.nio.channels.NotYetBoundException;
40
import java.nio.channels.ServerSocketChannel;
41
import java.nio.channels.SocketChannel;
42
43
44
// Make a server-socket channel look like a server socket.
45
//
46
// The methods in this class are defined in exactly the same order as in
47
// java.net.ServerSocket so as to simplify tracking future changes to that
48
// class.
49
//
50
51
class ServerSocketAdaptor // package-private
52
extends ServerSocket
53
{
54
55
// The channel being adapted
56
private final ServerSocketChannelImpl ssc;
57
58
// Timeout "option" value for accepts
59
private volatile int timeout = 0;
60
61
public static ServerSocket create(ServerSocketChannelImpl ssc) {
62
try {
63
return new ServerSocketAdaptor(ssc);
64
} catch (IOException x) {
65
throw new Error(x);
66
}
67
}
68
69
// ## super will create a useless impl
70
private ServerSocketAdaptor(ServerSocketChannelImpl ssc)
71
throws IOException
72
{
73
this.ssc = ssc;
74
}
75
76
77
public void bind(SocketAddress local) throws IOException {
78
bind(local, 50);
79
}
80
81
public void bind(SocketAddress local, int backlog) throws IOException {
82
if (local == null)
83
local = new InetSocketAddress(0);
84
try {
85
ssc.bind(local, backlog);
86
} catch (Exception x) {
87
Net.translateException(x);
88
}
89
}
90
91
public InetAddress getInetAddress() {
92
if (!ssc.isBound())
93
return null;
94
return Net.getRevealedLocalAddress(ssc.localAddress()).getAddress();
95
96
}
97
98
public int getLocalPort() {
99
if (!ssc.isBound())
100
return -1;
101
return Net.asInetSocketAddress(ssc.localAddress()).getPort();
102
}
103
104
105
public Socket accept() throws IOException {
106
synchronized (ssc.blockingLock()) {
107
if (!ssc.isBound())
108
throw new IllegalBlockingModeException();
109
try {
110
if (timeout == 0) {
111
// for compatibility reasons: accept connection if available
112
// when configured non-blocking
113
SocketChannel sc = ssc.accept();
114
if (sc == null && !ssc.isBlocking())
115
throw new IllegalBlockingModeException();
116
return sc.socket();
117
}
118
119
if (!ssc.isBlocking())
120
throw new IllegalBlockingModeException();
121
ssc.configureBlocking(false);
122
try {
123
SocketChannel sc;
124
if ((sc = ssc.accept()) != null)
125
return sc.socket();
126
long to = timeout;
127
for (;;) {
128
if (!ssc.isOpen())
129
throw new ClosedChannelException();
130
long st = System.currentTimeMillis();
131
int result = ssc.poll(Net.POLLIN, to);
132
if (result > 0 && ((sc = ssc.accept()) != null))
133
return sc.socket();
134
to -= System.currentTimeMillis() - st;
135
if (to <= 0)
136
throw new SocketTimeoutException();
137
}
138
} finally {
139
try {
140
ssc.configureBlocking(true);
141
} catch (ClosedChannelException e) { }
142
}
143
} catch (Exception x) {
144
Net.translateException(x);
145
assert false;
146
return null; // Never happens
147
}
148
}
149
}
150
151
public void close() throws IOException {
152
ssc.close();
153
}
154
155
public ServerSocketChannel getChannel() {
156
return ssc;
157
}
158
159
public boolean isBound() {
160
return ssc.isBound();
161
}
162
163
public boolean isClosed() {
164
return !ssc.isOpen();
165
}
166
167
public void setSoTimeout(int timeout) throws SocketException {
168
this.timeout = timeout;
169
}
170
171
public int getSoTimeout() throws SocketException {
172
return timeout;
173
}
174
175
public void setReuseAddress(boolean on) throws SocketException {
176
try {
177
ssc.setOption(StandardSocketOptions.SO_REUSEADDR, on);
178
} catch (IOException x) {
179
Net.translateToSocketException(x);
180
}
181
}
182
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
public String toString() {
193
if (!isBound())
194
return "ServerSocket[unbound]";
195
return "ServerSocket[addr=" + getInetAddress() +
196
",localport=" + getLocalPort() + "]";
197
}
198
199
public void setReceiveBufferSize(int size) throws SocketException {
200
// size 0 valid for ServerSocketChannel, invalid for ServerSocket
201
if (size <= 0)
202
throw new IllegalArgumentException("size cannot be 0 or negative");
203
try {
204
ssc.setOption(StandardSocketOptions.SO_RCVBUF, size);
205
} catch (IOException x) {
206
Net.translateToSocketException(x);
207
}
208
}
209
210
public int getReceiveBufferSize() throws SocketException {
211
try {
212
return ssc.getOption(StandardSocketOptions.SO_RCVBUF).intValue();
213
} catch (IOException x) {
214
Net.translateToSocketException(x);
215
return -1; // Never happens
216
}
217
}
218
219
}
220
221