Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/java/net/AbstractPlainSocketImpl.java
41152 views
1
/*
2
* Copyright (c) 1995, 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 java.net;
27
28
import java.io.FileDescriptor;
29
import java.io.IOException;
30
import java.io.InputStream;
31
import java.io.OutputStream;
32
33
import java.security.AccessController;
34
import java.security.PrivilegedActionException;
35
import java.security.PrivilegedExceptionAction;
36
import java.util.Collections;
37
import java.util.HashSet;
38
import java.util.Objects;
39
import java.util.Set;
40
41
import sun.net.ConnectionResetException;
42
import sun.net.NetHooks;
43
import sun.net.PlatformSocketImpl;
44
import sun.net.ResourceManager;
45
import sun.net.ext.ExtendedSocketOptions;
46
import sun.net.util.IPAddressUtil;
47
import sun.net.util.SocketExceptions;
48
49
/**
50
* Default Socket Implementation. This implementation does
51
* not implement any security checks.
52
* Note this class should <b>NOT</b> be public.
53
*
54
* @author Steven B. Byrne
55
*/
56
abstract class AbstractPlainSocketImpl extends SocketImpl implements PlatformSocketImpl {
57
/* instance variable for SO_TIMEOUT */
58
int timeout; // timeout in millisec
59
// traffic class
60
private int trafficClass;
61
62
private boolean shut_rd = false;
63
private boolean shut_wr = false;
64
65
private SocketInputStream socketInputStream = null;
66
private SocketOutputStream socketOutputStream = null;
67
68
/* number of threads using the FileDescriptor */
69
protected int fdUseCount = 0;
70
71
/* lock when increment/decrementing fdUseCount */
72
protected final Object fdLock = new Object();
73
74
/* indicates a close is pending on the file descriptor */
75
protected boolean closePending = false;
76
77
/* indicates connection reset state */
78
private volatile boolean connectionReset;
79
80
/* indicates whether impl is bound */
81
boolean isBound;
82
83
/* indicates whether impl is connected */
84
volatile boolean isConnected;
85
86
/* whether this Socket is a stream (TCP) socket or not (UDP)
87
*/
88
protected boolean stream;
89
90
/* whether this is a server or not */
91
final boolean isServer;
92
93
/**
94
* Load net library into runtime.
95
*/
96
static {
97
jdk.internal.loader.BootLoader.loadLibrary("net");
98
}
99
100
private static volatile boolean checkedReusePort;
101
private static volatile boolean isReusePortAvailable;
102
103
/**
104
* Tells whether SO_REUSEPORT is supported.
105
*/
106
static boolean isReusePortAvailable() {
107
if (!checkedReusePort) {
108
isReusePortAvailable = isReusePortAvailable0();
109
checkedReusePort = true;
110
}
111
return isReusePortAvailable;
112
}
113
114
AbstractPlainSocketImpl(boolean isServer) {
115
this.isServer = isServer;
116
}
117
118
/**
119
* Creates a socket with a boolean that specifies whether this
120
* is a stream socket (true) or an unconnected UDP socket (false).
121
*/
122
protected synchronized void create(boolean stream) throws IOException {
123
this.stream = stream;
124
if (!stream) {
125
ResourceManager.beforeUdpCreate();
126
// only create the fd after we know we will be able to create the socket
127
fd = new FileDescriptor();
128
try {
129
socketCreate(false);
130
SocketCleanable.register(fd, false);
131
} catch (IOException ioe) {
132
ResourceManager.afterUdpClose();
133
fd = null;
134
throw ioe;
135
}
136
} else {
137
fd = new FileDescriptor();
138
socketCreate(true);
139
SocketCleanable.register(fd, true);
140
}
141
}
142
143
/**
144
* Creates a socket and connects it to the specified port on
145
* the specified host.
146
* @param host the specified host
147
* @param port the specified port
148
*/
149
protected void connect(String host, int port)
150
throws UnknownHostException, IOException
151
{
152
boolean connected = false;
153
try {
154
InetAddress address = InetAddress.getByName(host);
155
// recording this.address as supplied by caller before calling connect
156
this.address = address;
157
this.port = port;
158
if (address.isLinkLocalAddress()) {
159
address = IPAddressUtil.toScopedAddress(address);
160
}
161
162
connectToAddress(address, port, timeout);
163
connected = true;
164
} finally {
165
if (!connected) {
166
try {
167
close();
168
} catch (IOException ioe) {
169
/* Do nothing. If connect threw an exception then
170
it will be passed up the call stack */
171
}
172
}
173
isConnected = connected;
174
}
175
}
176
177
/**
178
* Creates a socket and connects it to the specified address on
179
* the specified port.
180
* @param address the address
181
* @param port the specified port
182
*/
183
protected void connect(InetAddress address, int port) throws IOException {
184
// recording this.address as supplied by caller before calling connect
185
this.address = address;
186
this.port = port;
187
if (address.isLinkLocalAddress()) {
188
address = IPAddressUtil.toScopedAddress(address);
189
}
190
191
try {
192
connectToAddress(address, port, timeout);
193
isConnected = true;
194
return;
195
} catch (IOException e) {
196
// everything failed
197
close();
198
throw e;
199
}
200
}
201
202
/**
203
* Creates a socket and connects it to the specified address on
204
* the specified port.
205
* @param address the address
206
* @param timeout the timeout value in milliseconds, or zero for no timeout.
207
* @throws IOException if connection fails
208
* @throws IllegalArgumentException if address is null or is a
209
* SocketAddress subclass not supported by this socket
210
* @since 1.4
211
*/
212
protected void connect(SocketAddress address, int timeout)
213
throws IOException {
214
boolean connected = false;
215
try {
216
if (!(address instanceof InetSocketAddress addr))
217
throw new IllegalArgumentException("unsupported address type");
218
if (addr.isUnresolved())
219
throw new UnknownHostException(addr.getHostName());
220
// recording this.address as supplied by caller before calling connect
221
InetAddress ia = addr.getAddress();
222
this.address = ia;
223
this.port = addr.getPort();
224
if (ia.isLinkLocalAddress()) {
225
ia = IPAddressUtil.toScopedAddress(ia);
226
}
227
connectToAddress(ia, port, timeout);
228
connected = true;
229
} finally {
230
if (!connected) {
231
try {
232
close();
233
} catch (IOException ioe) {
234
/* Do nothing. If connect threw an exception then
235
it will be passed up the call stack */
236
}
237
}
238
isConnected = connected;
239
}
240
}
241
242
private void connectToAddress(InetAddress address, int port, int timeout) throws IOException {
243
if (address.isAnyLocalAddress()) {
244
doConnect(InetAddress.getLocalHost(), port, timeout);
245
} else {
246
doConnect(address, port, timeout);
247
}
248
}
249
250
public void setOption(int opt, Object val) throws SocketException {
251
if (isClosedOrPending()) {
252
throw new SocketException("Socket Closed");
253
}
254
boolean on = true;
255
switch (opt) {
256
/* check type safety b4 going native. These should never
257
* fail, since only java.Socket* has access to
258
* PlainSocketImpl.setOption().
259
*/
260
case SO_LINGER:
261
if (!(val instanceof Integer) && !(val instanceof Boolean))
262
throw new SocketException("Bad parameter for option");
263
if (val instanceof Boolean) {
264
/* true only if disabling - enabling should be Integer */
265
on = false;
266
}
267
break;
268
case SO_TIMEOUT:
269
if (!(val instanceof Integer))
270
throw new SocketException("Bad parameter for SO_TIMEOUT");
271
int tmp = ((Integer) val).intValue();
272
if (tmp < 0)
273
throw new IllegalArgumentException("timeout < 0");
274
timeout = tmp;
275
break;
276
case IP_TOS:
277
if (!(val instanceof Integer)) {
278
throw new SocketException("bad argument for IP_TOS");
279
}
280
trafficClass = ((Integer)val).intValue();
281
break;
282
case SO_BINDADDR:
283
throw new SocketException("Cannot re-bind socket");
284
case TCP_NODELAY:
285
if (!(val instanceof Boolean))
286
throw new SocketException("bad parameter for TCP_NODELAY");
287
on = ((Boolean)val).booleanValue();
288
break;
289
case SO_SNDBUF:
290
case SO_RCVBUF:
291
if (!(val instanceof Integer) ||
292
!(((Integer)val).intValue() > 0)) {
293
throw new SocketException("bad parameter for SO_SNDBUF " +
294
"or SO_RCVBUF");
295
}
296
break;
297
case SO_KEEPALIVE:
298
if (!(val instanceof Boolean))
299
throw new SocketException("bad parameter for SO_KEEPALIVE");
300
on = ((Boolean)val).booleanValue();
301
break;
302
case SO_OOBINLINE:
303
if (!(val instanceof Boolean))
304
throw new SocketException("bad parameter for SO_OOBINLINE");
305
on = ((Boolean)val).booleanValue();
306
break;
307
case SO_REUSEADDR:
308
if (!(val instanceof Boolean))
309
throw new SocketException("bad parameter for SO_REUSEADDR");
310
on = ((Boolean)val).booleanValue();
311
break;
312
case SO_REUSEPORT:
313
if (!(val instanceof Boolean))
314
throw new SocketException("bad parameter for SO_REUSEPORT");
315
if (!supportedOptions().contains(StandardSocketOptions.SO_REUSEPORT))
316
throw new UnsupportedOperationException("unsupported option");
317
on = ((Boolean)val).booleanValue();
318
break;
319
default:
320
throw new SocketException("unrecognized TCP option: " + opt);
321
}
322
socketSetOption(opt, on, val);
323
}
324
public Object getOption(int opt) throws SocketException {
325
if (isClosedOrPending()) {
326
throw new SocketException("Socket Closed");
327
}
328
if (opt == SO_TIMEOUT) {
329
return timeout;
330
}
331
int ret = 0;
332
/*
333
* The native socketGetOption() knows about 3 options.
334
* The 32 bit value it returns will be interpreted according
335
* to what we're asking. A return of -1 means it understands
336
* the option but its turned off. It will raise a SocketException
337
* if "opt" isn't one it understands.
338
*/
339
340
switch (opt) {
341
case TCP_NODELAY:
342
ret = socketGetOption(opt, null);
343
return Boolean.valueOf(ret != -1);
344
case SO_OOBINLINE:
345
ret = socketGetOption(opt, null);
346
return Boolean.valueOf(ret != -1);
347
case SO_LINGER:
348
ret = socketGetOption(opt, null);
349
return (ret == -1) ? Boolean.FALSE: (Object)(ret);
350
case SO_REUSEADDR:
351
ret = socketGetOption(opt, null);
352
return Boolean.valueOf(ret != -1);
353
case SO_BINDADDR:
354
InetAddressContainer in = new InetAddressContainer();
355
ret = socketGetOption(opt, in);
356
return in.addr;
357
case SO_SNDBUF:
358
case SO_RCVBUF:
359
ret = socketGetOption(opt, null);
360
return ret;
361
case IP_TOS:
362
try {
363
ret = socketGetOption(opt, null);
364
if (ret == -1) { // ipv6 tos
365
return trafficClass;
366
} else {
367
return ret;
368
}
369
} catch (SocketException se) {
370
// TODO - should make better effort to read TOS or TCLASS
371
return trafficClass; // ipv6 tos
372
}
373
case SO_KEEPALIVE:
374
ret = socketGetOption(opt, null);
375
return Boolean.valueOf(ret != -1);
376
case SO_REUSEPORT:
377
if (!supportedOptions().contains(StandardSocketOptions.SO_REUSEPORT)) {
378
throw new UnsupportedOperationException("unsupported option");
379
}
380
ret = socketGetOption(opt, null);
381
return Boolean.valueOf(ret != -1);
382
// should never get here
383
default:
384
return null;
385
}
386
}
387
388
static final ExtendedSocketOptions extendedOptions =
389
ExtendedSocketOptions.getInstance();
390
391
private static final Set<SocketOption<?>> clientSocketOptions = clientSocketOptions();
392
private static final Set<SocketOption<?>> serverSocketOptions = serverSocketOptions();
393
394
private static Set<SocketOption<?>> clientSocketOptions() {
395
HashSet<SocketOption<?>> options = new HashSet<>();
396
options.add(StandardSocketOptions.SO_KEEPALIVE);
397
options.add(StandardSocketOptions.SO_SNDBUF);
398
options.add(StandardSocketOptions.SO_RCVBUF);
399
options.add(StandardSocketOptions.SO_REUSEADDR);
400
options.add(StandardSocketOptions.SO_LINGER);
401
options.add(StandardSocketOptions.IP_TOS);
402
options.add(StandardSocketOptions.TCP_NODELAY);
403
if (isReusePortAvailable())
404
options.add(StandardSocketOptions.SO_REUSEPORT);
405
options.addAll(ExtendedSocketOptions.clientSocketOptions());
406
return Collections.unmodifiableSet(options);
407
}
408
409
private static Set<SocketOption<?>> serverSocketOptions() {
410
HashSet<SocketOption<?>> options = new HashSet<>();
411
options.add(StandardSocketOptions.SO_RCVBUF);
412
options.add(StandardSocketOptions.SO_REUSEADDR);
413
options.add(StandardSocketOptions.IP_TOS);
414
if (isReusePortAvailable())
415
options.add(StandardSocketOptions.SO_REUSEPORT);
416
options.addAll(ExtendedSocketOptions.serverSocketOptions());
417
return Collections.unmodifiableSet(options);
418
}
419
420
@Override
421
protected Set<SocketOption<?>> supportedOptions() {
422
if (isServer)
423
return serverSocketOptions;
424
else
425
return clientSocketOptions;
426
}
427
428
@Override
429
protected <T> void setOption(SocketOption<T> name, T value) throws IOException {
430
Objects.requireNonNull(name);
431
if (!supportedOptions().contains(name))
432
throw new UnsupportedOperationException("'" + name + "' not supported");
433
434
if (!name.type().isInstance(value))
435
throw new IllegalArgumentException("Invalid value '" + value + "'");
436
437
if (isClosedOrPending())
438
throw new SocketException("Socket closed");
439
440
if (name == StandardSocketOptions.SO_KEEPALIVE) {
441
setOption(SocketOptions.SO_KEEPALIVE, value);
442
} else if (name == StandardSocketOptions.SO_SNDBUF) {
443
if (((Integer)value).intValue() < 0)
444
throw new IllegalArgumentException("Invalid send buffer size:" + value);
445
setOption(SocketOptions.SO_SNDBUF, value);
446
} else if (name == StandardSocketOptions.SO_RCVBUF) {
447
if (((Integer)value).intValue() < 0)
448
throw new IllegalArgumentException("Invalid recv buffer size:" + value);
449
setOption(SocketOptions.SO_RCVBUF, value);
450
} else if (name == StandardSocketOptions.SO_REUSEADDR) {
451
setOption(SocketOptions.SO_REUSEADDR, value);
452
} else if (name == StandardSocketOptions.SO_REUSEPORT) {
453
setOption(SocketOptions.SO_REUSEPORT, value);
454
} else if (name == StandardSocketOptions.SO_LINGER ) {
455
if (((Integer)value).intValue() < 0)
456
setOption(SocketOptions.SO_LINGER, false);
457
else
458
setOption(SocketOptions.SO_LINGER, value);
459
} else if (name == StandardSocketOptions.IP_TOS) {
460
int i = ((Integer)value).intValue();
461
if (i < 0 || i > 255)
462
throw new IllegalArgumentException("Invalid IP_TOS value: " + value);
463
setOption(SocketOptions.IP_TOS, value);
464
} else if (name == StandardSocketOptions.TCP_NODELAY) {
465
setOption(SocketOptions.TCP_NODELAY, value);
466
} else if (extendedOptions.isOptionSupported(name)) {
467
extendedOptions.setOption(fd, name, value);
468
} else {
469
throw new AssertionError("unknown option: " + name);
470
}
471
}
472
473
@Override
474
@SuppressWarnings("unchecked")
475
protected <T> T getOption(SocketOption<T> name) throws IOException {
476
Objects.requireNonNull(name);
477
if (!supportedOptions().contains(name))
478
throw new UnsupportedOperationException("'" + name + "' not supported");
479
480
if (isClosedOrPending())
481
throw new SocketException("Socket closed");
482
483
if (name == StandardSocketOptions.SO_KEEPALIVE) {
484
return (T)getOption(SocketOptions.SO_KEEPALIVE);
485
} else if (name == StandardSocketOptions.SO_SNDBUF) {
486
return (T)getOption(SocketOptions.SO_SNDBUF);
487
} else if (name == StandardSocketOptions.SO_RCVBUF) {
488
return (T)getOption(SocketOptions.SO_RCVBUF);
489
} else if (name == StandardSocketOptions.SO_REUSEADDR) {
490
return (T)getOption(SocketOptions.SO_REUSEADDR);
491
} else if (name == StandardSocketOptions.SO_REUSEPORT) {
492
return (T)getOption(SocketOptions.SO_REUSEPORT);
493
} else if (name == StandardSocketOptions.SO_LINGER) {
494
Object value = getOption(SocketOptions.SO_LINGER);
495
if (value instanceof Boolean) {
496
assert ((Boolean)value).booleanValue() == false;
497
value = -1;
498
}
499
return (T)value;
500
} else if (name == StandardSocketOptions.IP_TOS) {
501
return (T)getOption(SocketOptions.IP_TOS);
502
} else if (name == StandardSocketOptions.TCP_NODELAY) {
503
return (T)getOption(SocketOptions.TCP_NODELAY);
504
} else if (extendedOptions.isOptionSupported(name)) {
505
return (T) extendedOptions.getOption(fd, name);
506
} else {
507
throw new AssertionError("unknown option: " + name);
508
}
509
}
510
511
/**
512
* The workhorse of the connection operation. Tries several times to
513
* establish a connection to the given <host, port>. If unsuccessful,
514
* throws an IOException indicating what went wrong.
515
*/
516
517
synchronized void doConnect(InetAddress address, int port, int timeout) throws IOException {
518
synchronized (fdLock) {
519
if (!closePending && !isBound) {
520
NetHooks.beforeTcpConnect(fd, address, port);
521
}
522
}
523
try {
524
acquireFD();
525
try {
526
socketConnect(address, port, timeout);
527
/* socket may have been closed during poll/select */
528
synchronized (fdLock) {
529
if (closePending) {
530
throw new SocketException ("Socket closed");
531
}
532
}
533
} finally {
534
releaseFD();
535
}
536
} catch (IOException e) {
537
close();
538
throw SocketExceptions.of(e, new InetSocketAddress(address, port));
539
}
540
}
541
542
/**
543
* Binds the socket to the specified address of the specified local port.
544
* @param address the address
545
* @param lport the port
546
*/
547
protected synchronized void bind(InetAddress address, int lport)
548
throws IOException
549
{
550
synchronized (fdLock) {
551
if (!closePending && !isBound) {
552
NetHooks.beforeTcpBind(fd, address, lport);
553
}
554
}
555
if (address.isLinkLocalAddress()) {
556
address = IPAddressUtil.toScopedAddress(address);
557
}
558
socketBind(address, lport);
559
isBound = true;
560
}
561
562
/**
563
* Listens, for a specified amount of time, for connections.
564
* @param count the amount of time to listen for connections
565
*/
566
protected synchronized void listen(int count) throws IOException {
567
socketListen(count);
568
}
569
570
/**
571
* Accepts connections.
572
* @param si the socket impl
573
*/
574
protected void accept(SocketImpl si) throws IOException {
575
si.fd = new FileDescriptor();
576
acquireFD();
577
try {
578
socketAccept(si);
579
} finally {
580
releaseFD();
581
}
582
SocketCleanable.register(si.fd, true);
583
}
584
585
/**
586
* Gets an InputStream for this socket.
587
*/
588
@SuppressWarnings("removal")
589
protected synchronized InputStream getInputStream() throws IOException {
590
synchronized (fdLock) {
591
if (isClosedOrPending())
592
throw new IOException("Socket Closed");
593
if (shut_rd)
594
throw new IOException("Socket input is shutdown");
595
if (socketInputStream == null) {
596
PrivilegedExceptionAction<SocketInputStream> pa = () -> new SocketInputStream(this);
597
try {
598
socketInputStream = AccessController.doPrivileged(pa);
599
} catch (PrivilegedActionException e) {
600
throw (IOException) e.getCause();
601
}
602
}
603
}
604
return socketInputStream;
605
}
606
607
void setInputStream(SocketInputStream in) {
608
socketInputStream = in;
609
}
610
611
/**
612
* Gets an OutputStream for this socket.
613
*/
614
@SuppressWarnings("removal")
615
protected synchronized OutputStream getOutputStream() throws IOException {
616
synchronized (fdLock) {
617
if (isClosedOrPending())
618
throw new IOException("Socket Closed");
619
if (shut_wr)
620
throw new IOException("Socket output is shutdown");
621
if (socketOutputStream == null) {
622
PrivilegedExceptionAction<SocketOutputStream> pa = () -> new SocketOutputStream(this);
623
try {
624
socketOutputStream = AccessController.doPrivileged(pa);
625
} catch (PrivilegedActionException e) {
626
throw (IOException) e.getCause();
627
}
628
}
629
}
630
return socketOutputStream;
631
}
632
633
void setFileDescriptor(FileDescriptor fd) {
634
this.fd = fd;
635
}
636
637
void setAddress(InetAddress address) {
638
this.address = address;
639
}
640
641
void setPort(int port) {
642
this.port = port;
643
}
644
645
void setLocalPort(int localport) {
646
this.localport = localport;
647
}
648
649
/**
650
* Returns the number of bytes that can be read without blocking.
651
*/
652
protected synchronized int available() throws IOException {
653
if (isClosedOrPending()) {
654
throw new IOException("Stream closed.");
655
}
656
657
/*
658
* If connection has been reset or shut down for input, then return 0
659
* to indicate there are no buffered bytes.
660
*/
661
if (isConnectionReset() || shut_rd) {
662
return 0;
663
}
664
665
/*
666
* If no bytes available and we were previously notified
667
* of a connection reset then we move to the reset state.
668
*
669
* If are notified of a connection reset then check
670
* again if there are bytes buffered on the socket.
671
*/
672
int n = 0;
673
try {
674
n = socketAvailable();
675
} catch (ConnectionResetException exc1) {
676
setConnectionReset();
677
}
678
return n;
679
}
680
681
/**
682
* Closes the socket.
683
*/
684
protected void close() throws IOException {
685
synchronized(fdLock) {
686
if (fd != null) {
687
if (fdUseCount == 0) {
688
if (closePending) {
689
return;
690
}
691
closePending = true;
692
/*
693
* We close the FileDescriptor in two-steps - first the
694
* "pre-close" which closes the socket but doesn't
695
* release the underlying file descriptor. This operation
696
* may be lengthy due to untransmitted data and a long
697
* linger interval. Once the pre-close is done we do the
698
* actual socket to release the fd.
699
*/
700
try {
701
socketPreClose();
702
} finally {
703
socketClose();
704
}
705
fd = null;
706
return;
707
} else {
708
/*
709
* If a thread has acquired the fd and a close
710
* isn't pending then use a deferred close.
711
* Also decrement fdUseCount to signal the last
712
* thread that releases the fd to close it.
713
*/
714
if (!closePending) {
715
closePending = true;
716
fdUseCount--;
717
socketPreClose();
718
}
719
}
720
}
721
}
722
}
723
724
void reset() {
725
throw new InternalError("should not get here");
726
}
727
728
/**
729
* Shutdown read-half of the socket connection;
730
*/
731
protected void shutdownInput() throws IOException {
732
if (fd != null) {
733
socketShutdown(SHUT_RD);
734
if (socketInputStream != null) {
735
socketInputStream.setEOF(true);
736
}
737
shut_rd = true;
738
}
739
}
740
741
/**
742
* Shutdown write-half of the socket connection;
743
*/
744
protected void shutdownOutput() throws IOException {
745
if (fd != null) {
746
socketShutdown(SHUT_WR);
747
shut_wr = true;
748
}
749
}
750
751
protected boolean supportsUrgentData () {
752
return true;
753
}
754
755
protected void sendUrgentData (int data) throws IOException {
756
if (fd == null) {
757
throw new IOException("Socket Closed");
758
}
759
socketSendUrgentData (data);
760
}
761
762
/*
763
* "Acquires" and returns the FileDescriptor for this impl
764
*
765
* A corresponding releaseFD is required to "release" the
766
* FileDescriptor.
767
*/
768
FileDescriptor acquireFD() {
769
synchronized (fdLock) {
770
fdUseCount++;
771
return fd;
772
}
773
}
774
775
/*
776
* "Release" the FileDescriptor for this impl.
777
*
778
* If the use count goes to -1 then the socket is closed.
779
*/
780
void releaseFD() {
781
synchronized (fdLock) {
782
fdUseCount--;
783
if (fdUseCount == -1) {
784
if (fd != null) {
785
try {
786
socketClose();
787
} catch (IOException e) {
788
} finally {
789
fd = null;
790
}
791
}
792
}
793
}
794
}
795
796
boolean isConnectionReset() {
797
return connectionReset;
798
}
799
800
void setConnectionReset() {
801
connectionReset = true;
802
}
803
804
/*
805
* Return true if already closed or close is pending
806
*/
807
public boolean isClosedOrPending() {
808
/*
809
* Lock on fdLock to ensure that we wait if a
810
* close is in progress.
811
*/
812
synchronized (fdLock) {
813
if (closePending || (fd == null)) {
814
return true;
815
} else {
816
return false;
817
}
818
}
819
}
820
821
/*
822
* Return the current value of SO_TIMEOUT
823
*/
824
public int getTimeout() {
825
return timeout;
826
}
827
828
/*
829
* "Pre-close" a socket by dup'ing the file descriptor - this enables
830
* the socket to be closed without releasing the file descriptor.
831
*/
832
private void socketPreClose() throws IOException {
833
socketClose0(true);
834
}
835
836
/*
837
* Close the socket (and release the file descriptor).
838
*/
839
protected void socketClose() throws IOException {
840
SocketCleanable.unregister(fd);
841
try {
842
socketClose0(false);
843
} finally {
844
if (!stream) {
845
ResourceManager.afterUdpClose();
846
}
847
}
848
}
849
850
abstract void socketCreate(boolean stream) throws IOException;
851
abstract void socketConnect(InetAddress address, int port, int timeout)
852
throws IOException;
853
abstract void socketBind(InetAddress address, int port)
854
throws IOException;
855
abstract void socketListen(int count)
856
throws IOException;
857
abstract void socketAccept(SocketImpl s)
858
throws IOException;
859
abstract int socketAvailable()
860
throws IOException;
861
abstract void socketClose0(boolean useDeferredClose)
862
throws IOException;
863
abstract void socketShutdown(int howto)
864
throws IOException;
865
abstract void socketSetOption(int cmd, boolean on, Object value)
866
throws SocketException;
867
abstract int socketGetOption(int opt, Object iaContainerObj) throws SocketException;
868
abstract void socketSendUrgentData(int data)
869
throws IOException;
870
871
public static final int SHUT_RD = 0;
872
public static final int SHUT_WR = 1;
873
874
private static native boolean isReusePortAvailable0();
875
}
876
877