Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-aarch32-jdk8u
Path: blob/jdk8u272-b10-aarch32-20201026/jdk/src/share/classes/sun/nio/ch/SocketAdaptor.java
83408 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.io.InputStream;
30
import java.io.OutputStream;
31
import java.net.InetAddress;
32
import java.net.InetSocketAddress;
33
import java.net.Socket;
34
import java.net.SocketAddress;
35
import java.net.SocketException;
36
import java.net.SocketImpl;
37
import java.net.SocketOption;
38
import java.net.SocketTimeoutException;
39
import java.net.StandardSocketOptions;
40
import java.nio.ByteBuffer;
41
import java.nio.channels.Channels;
42
import java.nio.channels.ClosedChannelException;
43
import java.nio.channels.IllegalBlockingModeException;
44
import java.nio.channels.SocketChannel;
45
import java.security.AccessController;
46
import java.security.PrivilegedExceptionAction;
47
import java.util.*;
48
49
50
// Make a socket channel look like a socket.
51
//
52
// The only aspects of java.net.Socket-hood that we don't attempt to emulate
53
// here are the interrupted-I/O exceptions (which our Solaris implementations
54
// attempt to support) and the sending of urgent data. Otherwise an adapted
55
// socket should look enough like a real java.net.Socket to fool most of the
56
// developers most of the time, right down to the exception message strings.
57
//
58
// The methods in this class are defined in exactly the same order as in
59
// java.net.Socket so as to simplify tracking future changes to that class.
60
//
61
62
class SocketAdaptor
63
extends Socket
64
{
65
66
// The channel being adapted
67
private final SocketChannelImpl sc;
68
69
// Timeout "option" value for reads
70
private volatile int timeout = 0;
71
72
private SocketAdaptor(SocketChannelImpl sc) throws SocketException {
73
super((SocketImpl) null);
74
this.sc = sc;
75
}
76
77
public static Socket create(SocketChannelImpl sc) {
78
try {
79
return new SocketAdaptor(sc);
80
} catch (SocketException e) {
81
throw new InternalError("Should not reach here");
82
}
83
}
84
85
public SocketChannel getChannel() {
86
return sc;
87
}
88
89
// Override this method just to protect against changes in the superclass
90
//
91
public void connect(SocketAddress remote) throws IOException {
92
connect(remote, 0);
93
}
94
95
public void connect(SocketAddress remote, int timeout) throws IOException {
96
if (remote == null)
97
throw new IllegalArgumentException("connect: The address can't be null");
98
if (timeout < 0)
99
throw new IllegalArgumentException("connect: timeout can't be negative");
100
101
synchronized (sc.blockingLock()) {
102
if (!sc.isBlocking())
103
throw new IllegalBlockingModeException();
104
105
try {
106
if (timeout == 0) {
107
sc.connect(remote);
108
return;
109
}
110
111
sc.configureBlocking(false);
112
try {
113
if (sc.connect(remote))
114
return;
115
long to = timeout;
116
for (;;) {
117
if (!sc.isOpen())
118
throw new ClosedChannelException();
119
long st = System.currentTimeMillis();
120
121
int result = sc.poll(Net.POLLCONN, to);
122
if (result > 0 && sc.finishConnect())
123
break;
124
to -= System.currentTimeMillis() - st;
125
if (to <= 0) {
126
try {
127
sc.close();
128
} catch (IOException x) { }
129
throw new SocketTimeoutException();
130
}
131
}
132
} finally {
133
try {
134
sc.configureBlocking(true);
135
} catch (ClosedChannelException e) { }
136
}
137
138
} catch (Exception x) {
139
Net.translateException(x, true);
140
}
141
}
142
143
}
144
145
public void bind(SocketAddress local) throws IOException {
146
try {
147
sc.bind(local);
148
} catch (Exception x) {
149
Net.translateException(x);
150
}
151
}
152
153
public InetAddress getInetAddress() {
154
SocketAddress remote = sc.remoteAddress();
155
if (remote == null) {
156
return null;
157
} else {
158
return ((InetSocketAddress)remote).getAddress();
159
}
160
}
161
162
public InetAddress getLocalAddress() {
163
if (sc.isOpen()) {
164
InetSocketAddress local = sc.localAddress();
165
if (local != null) {
166
return Net.getRevealedLocalAddress(local).getAddress();
167
}
168
}
169
return new InetSocketAddress(0).getAddress();
170
}
171
172
public int getPort() {
173
SocketAddress remote = sc.remoteAddress();
174
if (remote == null) {
175
return 0;
176
} else {
177
return ((InetSocketAddress)remote).getPort();
178
}
179
}
180
181
public int getLocalPort() {
182
SocketAddress local = sc.localAddress();
183
if (local == null) {
184
return -1;
185
} else {
186
return ((InetSocketAddress)local).getPort();
187
}
188
}
189
190
private class SocketInputStream
191
extends ChannelInputStream
192
{
193
private SocketInputStream() {
194
super(sc);
195
}
196
197
protected int read(ByteBuffer bb)
198
throws IOException
199
{
200
synchronized (sc.blockingLock()) {
201
if (!sc.isBlocking())
202
throw new IllegalBlockingModeException();
203
204
if (timeout == 0)
205
return sc.read(bb);
206
207
sc.configureBlocking(false);
208
try {
209
int n;
210
if ((n = sc.read(bb)) != 0)
211
return n;
212
long to = timeout;
213
for (;;) {
214
if (!sc.isOpen())
215
throw new ClosedChannelException();
216
long st = System.currentTimeMillis();
217
int result = sc.poll(Net.POLLIN, to);
218
if (result > 0) {
219
if ((n = sc.read(bb)) != 0)
220
return n;
221
}
222
to -= System.currentTimeMillis() - st;
223
if (to <= 0)
224
throw new SocketTimeoutException();
225
}
226
} finally {
227
try {
228
sc.configureBlocking(true);
229
} catch (ClosedChannelException e) { }
230
}
231
}
232
}
233
}
234
235
private InputStream socketInputStream = null;
236
237
public InputStream getInputStream() throws IOException {
238
if (!sc.isOpen())
239
throw new SocketException("Socket is closed");
240
if (!sc.isConnected())
241
throw new SocketException("Socket is not connected");
242
if (!sc.isInputOpen())
243
throw new SocketException("Socket input is shutdown");
244
if (socketInputStream == null) {
245
try {
246
socketInputStream = AccessController.doPrivileged(
247
new PrivilegedExceptionAction<InputStream>() {
248
public InputStream run() throws IOException {
249
return new SocketInputStream();
250
}
251
});
252
} catch (java.security.PrivilegedActionException e) {
253
throw (IOException)e.getException();
254
}
255
}
256
return socketInputStream;
257
}
258
259
public OutputStream getOutputStream() throws IOException {
260
if (!sc.isOpen())
261
throw new SocketException("Socket is closed");
262
if (!sc.isConnected())
263
throw new SocketException("Socket is not connected");
264
if (!sc.isOutputOpen())
265
throw new SocketException("Socket output is shutdown");
266
OutputStream os = null;
267
try {
268
os = AccessController.doPrivileged(
269
new PrivilegedExceptionAction<OutputStream>() {
270
public OutputStream run() throws IOException {
271
return Channels.newOutputStream(sc);
272
}
273
});
274
} catch (java.security.PrivilegedActionException e) {
275
throw (IOException)e.getException();
276
}
277
return os;
278
}
279
280
private void setBooleanOption(SocketOption<Boolean> name, boolean value)
281
throws SocketException
282
{
283
try {
284
sc.setOption(name, value);
285
} catch (IOException x) {
286
Net.translateToSocketException(x);
287
}
288
}
289
290
private void setIntOption(SocketOption<Integer> name, int value)
291
throws SocketException
292
{
293
try {
294
sc.setOption(name, value);
295
} catch (IOException x) {
296
Net.translateToSocketException(x);
297
}
298
}
299
300
private boolean getBooleanOption(SocketOption<Boolean> name) throws SocketException {
301
try {
302
return sc.getOption(name).booleanValue();
303
} catch (IOException x) {
304
Net.translateToSocketException(x);
305
return false; // keep compiler happy
306
}
307
}
308
309
private int getIntOption(SocketOption<Integer> name) throws SocketException {
310
try {
311
return sc.getOption(name).intValue();
312
} catch (IOException x) {
313
Net.translateToSocketException(x);
314
return -1; // keep compiler happy
315
}
316
}
317
318
public void setTcpNoDelay(boolean on) throws SocketException {
319
setBooleanOption(StandardSocketOptions.TCP_NODELAY, on);
320
}
321
322
public boolean getTcpNoDelay() throws SocketException {
323
return getBooleanOption(StandardSocketOptions.TCP_NODELAY);
324
}
325
326
public void setSoLinger(boolean on, int linger) throws SocketException {
327
if (!on)
328
linger = -1;
329
setIntOption(StandardSocketOptions.SO_LINGER, linger);
330
}
331
332
public int getSoLinger() throws SocketException {
333
return getIntOption(StandardSocketOptions.SO_LINGER);
334
}
335
336
public void sendUrgentData(int data) throws IOException {
337
int n = sc.sendOutOfBandData((byte) data);
338
if (n == 0)
339
throw new IOException("Socket buffer full");
340
}
341
342
public void setOOBInline(boolean on) throws SocketException {
343
setBooleanOption(ExtendedSocketOption.SO_OOBINLINE, on);
344
}
345
346
public boolean getOOBInline() throws SocketException {
347
return getBooleanOption(ExtendedSocketOption.SO_OOBINLINE);
348
}
349
350
public void setSoTimeout(int timeout) throws SocketException {
351
if (timeout < 0)
352
throw new IllegalArgumentException("timeout can't be negative");
353
this.timeout = timeout;
354
}
355
356
public int getSoTimeout() throws SocketException {
357
return timeout;
358
}
359
360
public void setSendBufferSize(int size) throws SocketException {
361
// size 0 valid for SocketChannel, invalid for Socket
362
if (size <= 0)
363
throw new IllegalArgumentException("Invalid send size");
364
setIntOption(StandardSocketOptions.SO_SNDBUF, size);
365
}
366
367
public int getSendBufferSize() throws SocketException {
368
return getIntOption(StandardSocketOptions.SO_SNDBUF);
369
}
370
371
public void setReceiveBufferSize(int size) throws SocketException {
372
// size 0 valid for SocketChannel, invalid for Socket
373
if (size <= 0)
374
throw new IllegalArgumentException("Invalid receive size");
375
setIntOption(StandardSocketOptions.SO_RCVBUF, size);
376
}
377
378
public int getReceiveBufferSize() throws SocketException {
379
return getIntOption(StandardSocketOptions.SO_RCVBUF);
380
}
381
382
public void setKeepAlive(boolean on) throws SocketException {
383
setBooleanOption(StandardSocketOptions.SO_KEEPALIVE, on);
384
}
385
386
public boolean getKeepAlive() throws SocketException {
387
return getBooleanOption(StandardSocketOptions.SO_KEEPALIVE);
388
}
389
390
public void setTrafficClass(int tc) throws SocketException {
391
setIntOption(StandardSocketOptions.IP_TOS, tc);
392
}
393
394
public int getTrafficClass() throws SocketException {
395
return getIntOption(StandardSocketOptions.IP_TOS);
396
}
397
398
public void setReuseAddress(boolean on) throws SocketException {
399
setBooleanOption(StandardSocketOptions.SO_REUSEADDR, on);
400
}
401
402
public boolean getReuseAddress() throws SocketException {
403
return getBooleanOption(StandardSocketOptions.SO_REUSEADDR);
404
}
405
406
public void close() throws IOException {
407
sc.close();
408
}
409
410
public void shutdownInput() throws IOException {
411
try {
412
sc.shutdownInput();
413
} catch (Exception x) {
414
Net.translateException(x);
415
}
416
}
417
418
public void shutdownOutput() throws IOException {
419
try {
420
sc.shutdownOutput();
421
} catch (Exception x) {
422
Net.translateException(x);
423
}
424
}
425
426
public String toString() {
427
if (sc.isConnected())
428
return "Socket[addr=" + getInetAddress() +
429
",port=" + getPort() +
430
",localport=" + getLocalPort() + "]";
431
return "Socket[unconnected]";
432
}
433
434
public boolean isConnected() {
435
return sc.isConnected();
436
}
437
438
public boolean isBound() {
439
return sc.localAddress() != null;
440
}
441
442
public boolean isClosed() {
443
return !sc.isOpen();
444
}
445
446
public boolean isInputShutdown() {
447
return !sc.isInputOpen();
448
}
449
450
public boolean isOutputShutdown() {
451
return !sc.isOutputOpen();
452
}
453
454
}
455
456