src/java.base/share/classes/sun/nio/ch/SocketAdaptor.java
author chegar
Wed, 29 May 2019 13:58:05 +0100
changeset 55081 dd321e3596c0
parent 54620 13b67c1420b8
child 58614 29624901d8bc
permissions -rw-r--r--
8224477: java.net socket types new-style socket option methods - spec and impl mismatch Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
54246
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 49001
diff changeset
     2
 * Copyright (c) 2000, 2019, 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
48761
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 47216
diff changeset
    28
import java.io.IOException;
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 47216
diff changeset
    29
import java.io.InputStream;
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 47216
diff changeset
    30
import java.io.OutputStream;
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 47216
diff changeset
    31
import java.net.InetAddress;
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 47216
diff changeset
    32
import java.net.InetSocketAddress;
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 47216
diff changeset
    33
import java.net.Socket;
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 47216
diff changeset
    34
import java.net.SocketAddress;
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 47216
diff changeset
    35
import java.net.SocketException;
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 47216
diff changeset
    36
import java.net.SocketOption;
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 47216
diff changeset
    37
import java.net.StandardSocketOptions;
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 47216
diff changeset
    38
import java.nio.channels.SocketChannel;
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
    39
import java.util.Set;
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
    40
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
    41
import static java.util.concurrent.TimeUnit.MILLISECONDS;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
// Make a socket channel look like a socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
// The methods in this class are defined in exactly the same order as in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
// java.net.Socket so as to simplify tracking future changes to that class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
48761
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 47216
diff changeset
    49
class SocketAdaptor
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    extends Socket
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 {
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
    59
        super(DummySocketImpl.create());
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
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
    63
    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
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
    71
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public void connect(SocketAddress remote) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        connect(remote, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
    76
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public void connect(SocketAddress remote, int timeout) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        if (remote == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            throw new IllegalArgumentException("connect: The address can't be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        if (timeout < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            throw new IllegalArgumentException("connect: timeout can't be negative");
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
    82
        try {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
    83
            if (timeout > 0) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
    84
                long nanos = MILLISECONDS.toNanos(timeout);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
    85
                sc.blockingConnect(remote, nanos);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
    86
            } else {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
    87
                sc.blockingConnect(remote, Long.MAX_VALUE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            }
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
    89
        } catch (Exception e) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
    90
            Net.translateException(e, true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
    94
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public void bind(SocketAddress local) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            sc.bind(local);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            Net.translateException(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   103
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public InetAddress getInetAddress() {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   105
        InetSocketAddress remote = sc.remoteAddress();
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   106
        if (remote == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            return null;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   108
        } else {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   109
            return remote.getAddress();
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   110
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   113
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public InetAddress getLocalAddress() {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   115
        if (sc.isOpen()) {
18212
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   116
            InetSocketAddress local = sc.localAddress();
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   117
            if (local != null) {
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   118
                return Net.getRevealedLocalAddress(local).getAddress();
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   119
            }
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   120
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   121
        return new InetSocketAddress(0).getAddress();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   124
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public int getPort() {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   126
        InetSocketAddress remote = sc.remoteAddress();
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   127
        if (remote == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            return 0;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   129
        } else {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   130
            return remote.getPort();
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   131
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   134
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    public int getLocalPort() {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   136
        InetSocketAddress local = sc.localAddress();
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   137
        if (local == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            return -1;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   139
        } else {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   140
            return local.getPort();
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   141
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   144
    @Override
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   145
    public SocketAddress getRemoteSocketAddress() {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   146
        return sc.remoteAddress();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   147
    }
48761
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 47216
diff changeset
   148
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   149
    @Override
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   150
    public SocketAddress getLocalSocketAddress() {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   151
        InetSocketAddress local = sc.localAddress();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   152
        if (local != null) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   153
            return Net.getRevealedLocalAddress(local);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   154
        } else {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   155
            return null;
54246
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 49001
diff changeset
   156
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   159
    @Override
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   160
    public SocketChannel getChannel() {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   161
        return sc;
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   162
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   164
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    public InputStream getInputStream() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        if (!sc.isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        if (!sc.isConnected())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            throw new SocketException("Socket is not connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        if (!sc.isInputOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            throw new SocketException("Socket input is shutdown");
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   172
        return new InputStream() {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   173
            @Override
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   174
            public int read() throws IOException {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   175
                byte[] a = new byte[1];
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   176
                int n = read(a, 0, 1);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   177
                return (n > 0) ? (a[0] & 0xff) : -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            }
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   179
            @Override
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   180
            public int read(byte[] b, int off, int len) throws IOException {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   181
                int timeout = SocketAdaptor.this.timeout;
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   182
                if (timeout > 0) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   183
                    long nanos = MILLISECONDS.toNanos(timeout);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   184
                    return sc.blockingRead(b, off, len, nanos);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   185
                } else {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   186
                    return sc.blockingRead(b, off, len, 0);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   187
                }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   188
            }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   189
            @Override
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   190
            public int available() throws IOException {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   191
                return sc.available();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   192
            }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   193
            @Override
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   194
            public void close() throws IOException {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   195
                sc.close();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   196
            }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   197
        };
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   200
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    public OutputStream getOutputStream() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        if (!sc.isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        if (!sc.isConnected())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            throw new SocketException("Socket is not connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        if (!sc.isOutputOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            throw new SocketException("Socket output is shutdown");
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   208
        return new OutputStream() {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   209
            @Override
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   210
            public void write(int b) throws IOException {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   211
                byte[] a = new byte[]{(byte) b};
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   212
                write(a, 0, 1);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   213
            }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   214
            @Override
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   215
            public void write(byte[] b, int off, int len) throws IOException {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   216
                sc.blockingWriteFully(b, off, len);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   217
            }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   218
            @Override
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   219
            public void close() throws IOException {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   220
                sc.close();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   221
            }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   222
        };
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   225
    private void setBooleanOption(SocketOption<Boolean> name, boolean value)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   226
        throws SocketException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   227
    {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   228
        try {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   229
            sc.setOption(name, value);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   230
        } catch (IOException x) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   231
            Net.translateToSocketException(x);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   232
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   233
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   234
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   235
    private void setIntOption(SocketOption<Integer> name, int value)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   236
        throws SocketException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   237
    {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   238
        try {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   239
            sc.setOption(name, value);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   240
        } catch (IOException x) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   241
            Net.translateToSocketException(x);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   242
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   243
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   244
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   245
    private boolean getBooleanOption(SocketOption<Boolean> name) throws SocketException {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   246
        try {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   247
            return sc.getOption(name).booleanValue();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   248
        } catch (IOException x) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   249
            Net.translateToSocketException(x);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   250
            return false;       // keep compiler happy
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   251
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   252
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   253
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   254
    private int getIntOption(SocketOption<Integer> name) throws SocketException {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   255
        try {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   256
            return sc.getOption(name).intValue();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   257
        } catch (IOException x) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   258
            Net.translateToSocketException(x);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   259
            return -1;          // keep compiler happy
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   260
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   263
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    public void setTcpNoDelay(boolean on) throws SocketException {
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   265
        setBooleanOption(StandardSocketOptions.TCP_NODELAY, on);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   268
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    public boolean getTcpNoDelay() throws SocketException {
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   270
        return getBooleanOption(StandardSocketOptions.TCP_NODELAY);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   273
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    public void setSoLinger(boolean on, int linger) throws SocketException {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   275
        if (!on)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   276
            linger = -1;
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   277
        setIntOption(StandardSocketOptions.SO_LINGER, linger);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   280
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    public int getSoLinger() throws SocketException {
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   282
        return getIntOption(StandardSocketOptions.SO_LINGER);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   285
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    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
   287
        int n = sc.sendOutOfBandData((byte) data);
8433f5f46142 8071599: (so) Socket adapter sendUrgentData throws IllegalBlockingMode when channel configured non-blocking
bpb
parents: 25859
diff changeset
   288
        if (n == 0)
8433f5f46142 8071599: (so) Socket adapter sendUrgentData throws IllegalBlockingMode when channel configured non-blocking
bpb
parents: 25859
diff changeset
   289
            throw new IOException("Socket buffer full");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   292
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    public void setOOBInline(boolean on) throws SocketException {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   294
        setBooleanOption(ExtendedSocketOption.SO_OOBINLINE, on);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   297
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    public boolean getOOBInline() throws SocketException {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   299
        return getBooleanOption(ExtendedSocketOption.SO_OOBINLINE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   302
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    public void setSoTimeout(int timeout) throws SocketException {
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   304
        if (!sc.isOpen())
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   305
            throw new SocketException("Socket is closed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        if (timeout < 0)
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   307
            throw new IllegalArgumentException("timeout < 0");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        this.timeout = timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   311
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    public int getSoTimeout() throws SocketException {
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   313
        if (!sc.isOpen())
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   314
            throw new SocketException("Socket is closed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        return timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   318
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    public void setSendBufferSize(int size) throws SocketException {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   320
        // size 0 valid for SocketChannel, invalid for Socket
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   321
        if (size <= 0)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   322
            throw new IllegalArgumentException("Invalid send size");
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   323
        setIntOption(StandardSocketOptions.SO_SNDBUF, size);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   326
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    public int getSendBufferSize() throws SocketException {
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   328
        return getIntOption(StandardSocketOptions.SO_SNDBUF);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   331
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    public void setReceiveBufferSize(int size) throws SocketException {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   333
        // size 0 valid for SocketChannel, invalid for Socket
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   334
        if (size <= 0)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   335
            throw new IllegalArgumentException("Invalid receive size");
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   336
        setIntOption(StandardSocketOptions.SO_RCVBUF, size);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   339
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    public int getReceiveBufferSize() throws SocketException {
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   341
        return getIntOption(StandardSocketOptions.SO_RCVBUF);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   344
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    public void setKeepAlive(boolean on) throws SocketException {
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   346
        setBooleanOption(StandardSocketOptions.SO_KEEPALIVE, on);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   349
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    public boolean getKeepAlive() throws SocketException {
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   351
        return getBooleanOption(StandardSocketOptions.SO_KEEPALIVE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   354
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    public void setTrafficClass(int tc) throws SocketException {
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   356
        setIntOption(StandardSocketOptions.IP_TOS, tc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   359
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    public int getTrafficClass() throws SocketException {
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   361
        return getIntOption(StandardSocketOptions.IP_TOS);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   364
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    public void setReuseAddress(boolean on) throws SocketException {
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   366
        setBooleanOption(StandardSocketOptions.SO_REUSEADDR, on);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   369
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    public boolean getReuseAddress() throws SocketException {
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   371
        return getBooleanOption(StandardSocketOptions.SO_REUSEADDR);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   374
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    public void close() throws IOException {
5784
e565c553e9fc 6938230: (so) SocketAdaptor.close() does not translate IOException resulting in Error
alanb
parents: 5506
diff changeset
   376
        sc.close();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   379
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    public void shutdownInput() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            sc.shutdownInput();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            Net.translateException(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   388
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    public void shutdownOutput() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            sc.shutdownOutput();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            Net.translateException(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   397
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        if (sc.isConnected())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            return "Socket[addr=" + getInetAddress() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                ",port=" + getPort() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                ",localport=" + getLocalPort() + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        return "Socket[unconnected]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   406
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    public boolean isConnected() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        return sc.isConnected();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   411
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    public boolean isBound() {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   413
        return sc.localAddress() != null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   416
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    public boolean isClosed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        return !sc.isOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   421
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    public boolean isInputShutdown() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        return !sc.isInputOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   426
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    public boolean isOutputShutdown() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        return !sc.isOutputOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    }
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   430
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   431
    @Override
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   432
    public <T> Socket setOption(SocketOption<T> name, T value) throws IOException {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   433
        sc.setOption(name, value);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   434
        return this;
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   435
    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   436
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   437
    @Override
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   438
    public <T> T getOption(SocketOption<T> name) throws IOException {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   439
        return sc.getOption(name);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   440
    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   441
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   442
    @Override
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   443
    public Set<SocketOption<?>> supportedOptions() {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   444
        return sc.supportedOptions();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   445
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
}