src/java.base/share/classes/sun/nio/ch/SocketAdaptor.java
author rriggs
Fri, 01 Dec 2017 16:40:08 -0500
changeset 48224 be0df5ab3093
parent 47216 71c04702a3d5
child 48761 74c1fa26435a
permissions -rw-r--r--
8080225: FileInput/OutputStream/FileChannel cleanup should be improved Reviewed-by: mchung, plevart, bpb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
42923
691c320b44f7 8165664: (ch) sun.nio.ch.SocketAdaptor does not respect timeout in case of system date/time change and blocks
bpb
parents: 34774
diff changeset
     2
 * Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1152
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1152
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1152
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1152
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1152
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.nio.ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.nio.channels.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.PrivilegedExceptionAction;
42923
691c320b44f7 8165664: (ch) sun.nio.ch.SocketAdaptor does not respect timeout in case of system date/time change and blocks
bpb
parents: 34774
diff changeset
    34
import java.util.concurrent.TimeUnit;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
// Make a socket channel look like a socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
// The only aspects of java.net.Socket-hood that we don't attempt to emulate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
// here are the interrupted-I/O exceptions (which our Solaris implementations
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
// attempt to support) and the sending of urgent data.  Otherwise an adapted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
// socket should look enough like a real java.net.Socket to fool most of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
// developers most of the time, right down to the exception message strings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
// The methods in this class are defined in exactly the same order as in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
// java.net.Socket so as to simplify tracking future changes to that class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
public class SocketAdaptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    extends Socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    // The channel being adapted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private final SocketChannelImpl sc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    // Timeout "option" value for reads
34774
03b4e6dc367b 8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents: 29224
diff changeset
    56
    private volatile int timeout;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
11106
802bb70d8fa2 7115586: Suppress creation of SocketImpl in SocketAdaptor's constructor
chegar
parents: 9679
diff changeset
    58
    private SocketAdaptor(SocketChannelImpl sc) throws SocketException {
802bb70d8fa2 7115586: Suppress creation of SocketImpl in SocketAdaptor's constructor
chegar
parents: 9679
diff changeset
    59
        super((SocketImpl) null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        this.sc = sc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public static Socket create(SocketChannelImpl sc) {
11106
802bb70d8fa2 7115586: Suppress creation of SocketImpl in SocketAdaptor's constructor
chegar
parents: 9679
diff changeset
    64
        try {
802bb70d8fa2 7115586: Suppress creation of SocketImpl in SocketAdaptor's constructor
chegar
parents: 9679
diff changeset
    65
            return new SocketAdaptor(sc);
802bb70d8fa2 7115586: Suppress creation of SocketImpl in SocketAdaptor's constructor
chegar
parents: 9679
diff changeset
    66
        } catch (SocketException e) {
802bb70d8fa2 7115586: Suppress creation of SocketImpl in SocketAdaptor's constructor
chegar
parents: 9679
diff changeset
    67
            throw new InternalError("Should not reach here");
802bb70d8fa2 7115586: Suppress creation of SocketImpl in SocketAdaptor's constructor
chegar
parents: 9679
diff changeset
    68
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    public SocketChannel getChannel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        return sc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    // Override this method just to protect against changes in the superclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public void connect(SocketAddress remote) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        connect(remote, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public void connect(SocketAddress remote, int timeout) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        if (remote == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            throw new IllegalArgumentException("connect: The address can't be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        if (timeout < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            throw new IllegalArgumentException("connect: timeout can't be negative");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        synchronized (sc.blockingLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            if (!sc.isBlocking())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                throw new IllegalBlockingModeException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                if (timeout == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    sc.connect(remote);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                sc.configureBlocking(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                    if (sc.connect(remote))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                        return;
42923
691c320b44f7 8165664: (ch) sun.nio.ch.SocketAdaptor does not respect timeout in case of system date/time change and blocks
bpb
parents: 34774
diff changeset
   102
                    long timeoutNanos =
691c320b44f7 8165664: (ch) sun.nio.ch.SocketAdaptor does not respect timeout in case of system date/time change and blocks
bpb
parents: 34774
diff changeset
   103
                        TimeUnit.NANOSECONDS.convert(timeout,
691c320b44f7 8165664: (ch) sun.nio.ch.SocketAdaptor does not respect timeout in case of system date/time change and blocks
bpb
parents: 34774
diff changeset
   104
                            TimeUnit.MILLISECONDS);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                    for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                        if (!sc.isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                            throw new ClosedChannelException();
42923
691c320b44f7 8165664: (ch) sun.nio.ch.SocketAdaptor does not respect timeout in case of system date/time change and blocks
bpb
parents: 34774
diff changeset
   108
                        long startTime = System.nanoTime();
14025
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents: 11106
diff changeset
   109
42923
691c320b44f7 8165664: (ch) sun.nio.ch.SocketAdaptor does not respect timeout in case of system date/time change and blocks
bpb
parents: 34774
diff changeset
   110
                        int result = sc.poll(Net.POLLCONN, timeout);
14025
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents: 11106
diff changeset
   111
                        if (result > 0 && sc.finishConnect())
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                            break;
42923
691c320b44f7 8165664: (ch) sun.nio.ch.SocketAdaptor does not respect timeout in case of system date/time change and blocks
bpb
parents: 34774
diff changeset
   113
                        timeoutNanos -= System.nanoTime() - startTime;
691c320b44f7 8165664: (ch) sun.nio.ch.SocketAdaptor does not respect timeout in case of system date/time change and blocks
bpb
parents: 34774
diff changeset
   114
                        if (timeoutNanos <= 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                                sc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                            } catch (IOException x) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                            throw new SocketTimeoutException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                    if (sc.isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                        sc.configureBlocking(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                Net.translateException(x, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    public void bind(SocketAddress local) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            sc.bind(local);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            Net.translateException(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    public InetAddress getInetAddress() {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   142
        SocketAddress remote = sc.remoteAddress();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   143
        if (remote == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            return null;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   145
        } else {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   146
            return ((InetSocketAddress)remote).getAddress();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   147
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    public InetAddress getLocalAddress() {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   151
        if (sc.isOpen()) {
18212
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   152
            InetSocketAddress local = sc.localAddress();
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   153
            if (local != null) {
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   154
                return Net.getRevealedLocalAddress(local).getAddress();
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   155
            }
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   156
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   157
        return new InetSocketAddress(0).getAddress();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public int getPort() {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   161
        SocketAddress remote = sc.remoteAddress();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   162
        if (remote == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            return 0;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   164
        } else {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   165
            return ((InetSocketAddress)remote).getPort();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   166
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public int getLocalPort() {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   170
        SocketAddress local = sc.localAddress();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   171
        if (local == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            return -1;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   173
        } else {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   174
            return ((InetSocketAddress)local).getPort();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   175
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    private class SocketInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        extends ChannelInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        private SocketInputStream() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            super(sc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        protected int read(ByteBuffer bb)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            synchronized (sc.blockingLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                if (!sc.isBlocking())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                    throw new IllegalBlockingModeException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                if (timeout == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                    return sc.read(bb);
14025
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents: 11106
diff changeset
   193
                sc.configureBlocking(false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                    int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                    if ((n = sc.read(bb)) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                        return n;
42923
691c320b44f7 8165664: (ch) sun.nio.ch.SocketAdaptor does not respect timeout in case of system date/time change and blocks
bpb
parents: 34774
diff changeset
   199
                    long timeoutNanos =
691c320b44f7 8165664: (ch) sun.nio.ch.SocketAdaptor does not respect timeout in case of system date/time change and blocks
bpb
parents: 34774
diff changeset
   200
                        TimeUnit.NANOSECONDS.convert(timeout,
691c320b44f7 8165664: (ch) sun.nio.ch.SocketAdaptor does not respect timeout in case of system date/time change and blocks
bpb
parents: 34774
diff changeset
   201
                            TimeUnit.MILLISECONDS);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                    for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                        if (!sc.isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                            throw new ClosedChannelException();
42923
691c320b44f7 8165664: (ch) sun.nio.ch.SocketAdaptor does not respect timeout in case of system date/time change and blocks
bpb
parents: 34774
diff changeset
   205
                        long startTime = System.nanoTime();
691c320b44f7 8165664: (ch) sun.nio.ch.SocketAdaptor does not respect timeout in case of system date/time change and blocks
bpb
parents: 34774
diff changeset
   206
                        int result = sc.poll(Net.POLLIN, timeout);
14025
fbebe005a3ee 7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents: 11106
diff changeset
   207
                        if (result > 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                            if ((n = sc.read(bb)) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                                return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                        }
42923
691c320b44f7 8165664: (ch) sun.nio.ch.SocketAdaptor does not respect timeout in case of system date/time change and blocks
bpb
parents: 34774
diff changeset
   211
                        timeoutNanos -= System.nanoTime() - startTime;
691c320b44f7 8165664: (ch) sun.nio.ch.SocketAdaptor does not respect timeout in case of system date/time change and blocks
bpb
parents: 34774
diff changeset
   212
                        if (timeoutNanos <= 0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                            throw new SocketTimeoutException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                    if (sc.isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                        sc.configureBlocking(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    private InputStream socketInputStream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    public InputStream getInputStream() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        if (!sc.isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        if (!sc.isConnected())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            throw new SocketException("Socket is not connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        if (!sc.isInputOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            throw new SocketException("Socket input is shutdown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        if (socketInputStream == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            try {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   235
                socketInputStream = AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   236
                    new PrivilegedExceptionAction<InputStream>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   237
                        public InputStream run() throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                            return new SocketInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            } catch (java.security.PrivilegedActionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                throw (IOException)e.getException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        return socketInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    public OutputStream getOutputStream() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        if (!sc.isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        if (!sc.isConnected())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            throw new SocketException("Socket is not connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        if (!sc.isOutputOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            throw new SocketException("Socket output is shutdown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        OutputStream os = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        try {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   257
            os = AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   258
                new PrivilegedExceptionAction<OutputStream>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   259
                    public OutputStream run() throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                        return Channels.newOutputStream(sc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        } catch (java.security.PrivilegedActionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            throw (IOException)e.getException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        return os;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   269
    private void setBooleanOption(SocketOption<Boolean> name, boolean value)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   270
        throws SocketException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   271
    {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   272
        try {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   273
            sc.setOption(name, value);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   274
        } catch (IOException x) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   275
            Net.translateToSocketException(x);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   276
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   277
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   278
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   279
    private void setIntOption(SocketOption<Integer> name, int value)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   280
        throws SocketException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   281
    {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   282
        try {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   283
            sc.setOption(name, value);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   284
        } catch (IOException x) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   285
            Net.translateToSocketException(x);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   286
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   287
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   288
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   289
    private boolean getBooleanOption(SocketOption<Boolean> name) throws SocketException {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   290
        try {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   291
            return sc.getOption(name).booleanValue();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   292
        } catch (IOException x) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   293
            Net.translateToSocketException(x);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   294
            return false;       // keep compiler happy
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   295
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   296
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   297
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   298
    private int getIntOption(SocketOption<Integer> name) throws SocketException {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   299
        try {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   300
            return sc.getOption(name).intValue();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   301
        } catch (IOException x) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   302
            Net.translateToSocketException(x);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   303
            return -1;          // keep compiler happy
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   304
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    public void setTcpNoDelay(boolean on) throws SocketException {
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   308
        setBooleanOption(StandardSocketOptions.TCP_NODELAY, on);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    public boolean getTcpNoDelay() throws SocketException {
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   312
        return getBooleanOption(StandardSocketOptions.TCP_NODELAY);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    public void setSoLinger(boolean on, int linger) throws SocketException {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   316
        if (!on)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   317
            linger = -1;
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   318
        setIntOption(StandardSocketOptions.SO_LINGER, linger);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    public int getSoLinger() throws SocketException {
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   322
        return getIntOption(StandardSocketOptions.SO_LINGER);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    public void sendUrgentData(int data) throws IOException {
29224
8433f5f46142 8071599: (so) Socket adapter sendUrgentData throws IllegalBlockingMode when channel configured non-blocking
bpb
parents: 25859
diff changeset
   326
        int n = sc.sendOutOfBandData((byte) data);
8433f5f46142 8071599: (so) Socket adapter sendUrgentData throws IllegalBlockingMode when channel configured non-blocking
bpb
parents: 25859
diff changeset
   327
        if (n == 0)
8433f5f46142 8071599: (so) Socket adapter sendUrgentData throws IllegalBlockingMode when channel configured non-blocking
bpb
parents: 25859
diff changeset
   328
            throw new IOException("Socket buffer full");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    public void setOOBInline(boolean on) throws SocketException {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   332
        setBooleanOption(ExtendedSocketOption.SO_OOBINLINE, on);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    public boolean getOOBInline() throws SocketException {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   336
        return getBooleanOption(ExtendedSocketOption.SO_OOBINLINE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    public void setSoTimeout(int timeout) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        if (timeout < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            throw new IllegalArgumentException("timeout can't be negative");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        this.timeout = timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    public int getSoTimeout() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        return timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    public void setSendBufferSize(int size) throws SocketException {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   350
        // size 0 valid for SocketChannel, invalid for Socket
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   351
        if (size <= 0)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   352
            throw new IllegalArgumentException("Invalid send size");
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   353
        setIntOption(StandardSocketOptions.SO_SNDBUF, size);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    public int getSendBufferSize() throws SocketException {
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   357
        return getIntOption(StandardSocketOptions.SO_SNDBUF);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    public void setReceiveBufferSize(int size) throws SocketException {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   361
        // size 0 valid for SocketChannel, invalid for Socket
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   362
        if (size <= 0)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   363
            throw new IllegalArgumentException("Invalid receive size");
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   364
        setIntOption(StandardSocketOptions.SO_RCVBUF, size);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    public int getReceiveBufferSize() throws SocketException {
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   368
        return getIntOption(StandardSocketOptions.SO_RCVBUF);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    public void setKeepAlive(boolean on) throws SocketException {
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   372
        setBooleanOption(StandardSocketOptions.SO_KEEPALIVE, on);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    public boolean getKeepAlive() throws SocketException {
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   376
        return getBooleanOption(StandardSocketOptions.SO_KEEPALIVE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    public void setTrafficClass(int tc) throws SocketException {
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   380
        setIntOption(StandardSocketOptions.IP_TOS, tc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    public int getTrafficClass() throws SocketException {
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   384
        return getIntOption(StandardSocketOptions.IP_TOS);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    public void setReuseAddress(boolean on) throws SocketException {
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   388
        setBooleanOption(StandardSocketOptions.SO_REUSEADDR, on);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    public boolean getReuseAddress() throws SocketException {
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   392
        return getBooleanOption(StandardSocketOptions.SO_REUSEADDR);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    public void close() throws IOException {
5784
e565c553e9fc 6938230: (so) SocketAdaptor.close() does not translate IOException resulting in Error
alanb
parents: 5506
diff changeset
   396
        sc.close();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    public void shutdownInput() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            sc.shutdownInput();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            Net.translateException(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    public void shutdownOutput() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            sc.shutdownOutput();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            Net.translateException(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        if (sc.isConnected())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            return "Socket[addr=" + getInetAddress() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                ",port=" + getPort() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                ",localport=" + getLocalPort() + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        return "Socket[unconnected]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    public boolean isConnected() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        return sc.isConnected();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    public boolean isBound() {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   428
        return sc.localAddress() != null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    public boolean isClosed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        return !sc.isOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    public boolean isInputShutdown() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        return !sc.isInputOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    public boolean isOutputShutdown() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        return !sc.isOutputOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
}