jdk/src/share/classes/sun/nio/ch/SocketAdaptor.java
author alanb
Mon, 09 May 2011 18:45:33 +0100
changeset 9679 d98ae8bc45fc
parent 7668 d4a77089c587
child 11106 802bb70d8fa2
permissions -rw-r--r--
7042979: Rename StandardSocketOption and StandardWatchEventKind Reviewed-by: forax, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 6117
diff changeset
     2
 * Copyright (c) 2000, 2010, 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.lang.ref.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.nio.channels.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.PrivilegedExceptionAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
// Make a socket channel look like a socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
// The only aspects of java.net.Socket-hood that we don't attempt to emulate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
// here are the interrupted-I/O exceptions (which our Solaris implementations
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
// attempt to support) and the sending of urgent data.  Otherwise an adapted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
// socket should look enough like a real java.net.Socket to fool most of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
// developers most of the time, right down to the exception message strings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
// The methods in this class are defined in exactly the same order as in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
// java.net.Socket so as to simplify tracking future changes to that class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
public class SocketAdaptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    extends Socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    // The channel being adapted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private final SocketChannelImpl sc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    // Timeout "option" value for reads
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private volatile int timeout = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    // ## super will create a useless impl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private SocketAdaptor(SocketChannelImpl sc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        this.sc = sc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public static Socket create(SocketChannelImpl sc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        return new SocketAdaptor(sc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public SocketChannel getChannel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        return sc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    // Override this method just to protect against changes in the superclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    public void connect(SocketAddress remote) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        connect(remote, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public void connect(SocketAddress remote, int timeout) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        if (remote == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            throw new IllegalArgumentException("connect: The address can't be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        if (timeout < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            throw new IllegalArgumentException("connect: timeout can't be negative");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        synchronized (sc.blockingLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            if (!sc.isBlocking())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                throw new IllegalBlockingModeException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                if (timeout == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    sc.connect(remote);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                // Implement timeout with a selector
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                SelectionKey sk = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                Selector sel = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                sc.configureBlocking(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                    if (sc.connect(remote))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                    sel = Util.getTemporarySelector(sc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                    sk = sc.register(sel, SelectionKey.OP_CONNECT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                    long to = timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                    for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                        if (!sc.isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                            throw new ClosedChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                        long st = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                        int ns = sel.select(to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                        if (ns > 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                            sk.isConnectable() && sc.finishConnect())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                        sel.selectedKeys().remove(sk);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                        to -= System.currentTimeMillis() - st;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                        if (to <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                                sc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                            } catch (IOException x) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                            throw new SocketTimeoutException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                    if (sk != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                        sk.cancel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                    if (sc.isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                        sc.configureBlocking(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    if (sel != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                        Util.releaseTemporarySelector(sel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                Net.translateException(x, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public void bind(SocketAddress local) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            sc.bind(local);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            Net.translateException(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public InetAddress getInetAddress() {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   148
        SocketAddress remote = sc.remoteAddress();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   149
        if (remote == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            return null;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   151
        } else {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   152
            return ((InetSocketAddress)remote).getAddress();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   153
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    public InetAddress getLocalAddress() {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   157
        if (sc.isOpen()) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   158
            SocketAddress local = sc.localAddress();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   159
            if (local != null)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   160
                return ((InetSocketAddress)local).getAddress();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   161
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   162
        return new InetSocketAddress(0).getAddress();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    public int getPort() {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   166
        SocketAddress remote = sc.remoteAddress();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   167
        if (remote == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            return 0;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   169
        } else {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   170
            return ((InetSocketAddress)remote).getPort();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   171
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    public int getLocalPort() {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   175
        SocketAddress local = sc.localAddress();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   176
        if (local == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            return -1;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   178
        } else {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   179
            return ((InetSocketAddress)local).getPort();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   180
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    private class SocketInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        extends ChannelInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        private SocketInputStream() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            super(sc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        protected int read(ByteBuffer bb)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            synchronized (sc.blockingLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                if (!sc.isBlocking())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                    throw new IllegalBlockingModeException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                if (timeout == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                    return sc.read(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                // Implement timeout with a selector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                SelectionKey sk = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                Selector sel = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                sc.configureBlocking(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                    int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                    if ((n = sc.read(bb)) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                    sel = Util.getTemporarySelector(sc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                    sk = sc.register(sel, SelectionKey.OP_READ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                    long to = timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                    for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                        if (!sc.isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                            throw new ClosedChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                        long st = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                        int ns = sel.select(to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                        if (ns > 0 && sk.isReadable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                            if ((n = sc.read(bb)) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                                return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                        sel.selectedKeys().remove(sk);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                        to -= System.currentTimeMillis() - st;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                        if (to <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                            throw new SocketTimeoutException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                    if (sk != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                        sk.cancel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                    if (sc.isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                        sc.configureBlocking(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                    if (sel != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                        Util.releaseTemporarySelector(sel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    private InputStream socketInputStream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    public InputStream getInputStream() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        if (!sc.isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        if (!sc.isConnected())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            throw new SocketException("Socket is not connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        if (!sc.isInputOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            throw new SocketException("Socket input is shutdown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        if (socketInputStream == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            try {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   248
                socketInputStream = AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   249
                    new PrivilegedExceptionAction<InputStream>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   250
                        public InputStream run() throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                            return new SocketInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            } catch (java.security.PrivilegedActionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                throw (IOException)e.getException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        return socketInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    public OutputStream getOutputStream() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        if (!sc.isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        if (!sc.isConnected())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            throw new SocketException("Socket is not connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        if (!sc.isOutputOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            throw new SocketException("Socket output is shutdown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        OutputStream os = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        try {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   270
            os = AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   271
                new PrivilegedExceptionAction<OutputStream>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   272
                    public OutputStream run() throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                        return Channels.newOutputStream(sc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        } catch (java.security.PrivilegedActionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            throw (IOException)e.getException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        return os;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   282
    private void setBooleanOption(SocketOption<Boolean> name, boolean value)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   283
        throws SocketException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   284
    {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   285
        try {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   286
            sc.setOption(name, value);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   287
        } catch (IOException x) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   288
            Net.translateToSocketException(x);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   289
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   290
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   291
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   292
    private void setIntOption(SocketOption<Integer> name, int value)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   293
        throws SocketException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   294
    {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   295
        try {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   296
            sc.setOption(name, value);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   297
        } catch (IOException x) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   298
            Net.translateToSocketException(x);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   299
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   300
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   301
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   302
    private boolean getBooleanOption(SocketOption<Boolean> name) throws SocketException {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   303
        try {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   304
            return sc.getOption(name).booleanValue();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   305
        } catch (IOException x) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   306
            Net.translateToSocketException(x);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   307
            return false;       // keep compiler happy
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   308
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   309
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   310
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   311
    private int getIntOption(SocketOption<Integer> name) throws SocketException {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   312
        try {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   313
            return sc.getOption(name).intValue();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   314
        } catch (IOException x) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   315
            Net.translateToSocketException(x);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   316
            return -1;          // keep compiler happy
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   317
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    public void setTcpNoDelay(boolean on) throws SocketException {
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   321
        setBooleanOption(StandardSocketOptions.TCP_NODELAY, on);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    public boolean getTcpNoDelay() throws SocketException {
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   325
        return getBooleanOption(StandardSocketOptions.TCP_NODELAY);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    public void setSoLinger(boolean on, int linger) throws SocketException {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   329
        if (!on)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   330
            linger = -1;
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   331
        setIntOption(StandardSocketOptions.SO_LINGER, linger);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    public int getSoLinger() throws SocketException {
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   335
        return getIntOption(StandardSocketOptions.SO_LINGER);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    public void sendUrgentData(int data) throws IOException {
6117
471ae95609d5 6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents: 5784
diff changeset
   339
        synchronized (sc.blockingLock()) {
471ae95609d5 6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents: 5784
diff changeset
   340
            if (!sc.isBlocking())
471ae95609d5 6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents: 5784
diff changeset
   341
                throw new IllegalBlockingModeException();
471ae95609d5 6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents: 5784
diff changeset
   342
            int n = sc.sendOutOfBandData((byte)data);
471ae95609d5 6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents: 5784
diff changeset
   343
            assert n == 1;
471ae95609d5 6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents: 5784
diff changeset
   344
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    public void setOOBInline(boolean on) throws SocketException {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   348
        setBooleanOption(ExtendedSocketOption.SO_OOBINLINE, on);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    public boolean getOOBInline() throws SocketException {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   352
        return getBooleanOption(ExtendedSocketOption.SO_OOBINLINE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    public void setSoTimeout(int timeout) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        if (timeout < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            throw new IllegalArgumentException("timeout can't be negative");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        this.timeout = timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    public int getSoTimeout() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        return timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    public void setSendBufferSize(int size) throws SocketException {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   366
        // size 0 valid for SocketChannel, invalid for Socket
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   367
        if (size <= 0)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   368
            throw new IllegalArgumentException("Invalid send size");
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   369
        setIntOption(StandardSocketOptions.SO_SNDBUF, size);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    public int getSendBufferSize() throws SocketException {
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   373
        return getIntOption(StandardSocketOptions.SO_SNDBUF);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    public void setReceiveBufferSize(int size) throws SocketException {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   377
        // size 0 valid for SocketChannel, invalid for Socket
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   378
        if (size <= 0)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   379
            throw new IllegalArgumentException("Invalid receive size");
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   380
        setIntOption(StandardSocketOptions.SO_RCVBUF, size);
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 getReceiveBufferSize() throws SocketException {
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   384
        return getIntOption(StandardSocketOptions.SO_RCVBUF);
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 setKeepAlive(boolean on) throws SocketException {
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   388
        setBooleanOption(StandardSocketOptions.SO_KEEPALIVE, 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 getKeepAlive() throws SocketException {
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   392
        return getBooleanOption(StandardSocketOptions.SO_KEEPALIVE);
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 setTrafficClass(int tc) throws SocketException {
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   396
        setIntOption(StandardSocketOptions.IP_TOS, tc);
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 int getTrafficClass() throws SocketException {
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   400
        return getIntOption(StandardSocketOptions.IP_TOS);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    public void setReuseAddress(boolean on) throws SocketException {
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   404
        setBooleanOption(StandardSocketOptions.SO_REUSEADDR, on);
2
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 boolean getReuseAddress() throws SocketException {
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   408
        return getBooleanOption(StandardSocketOptions.SO_REUSEADDR);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    public void close() throws IOException {
5784
e565c553e9fc 6938230: (so) SocketAdaptor.close() does not translate IOException resulting in Error
alanb
parents: 5506
diff changeset
   412
        sc.close();
2
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 void shutdownInput() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            sc.shutdownInput();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            Net.translateException(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        }
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 void shutdownOutput() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            sc.shutdownOutput();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            Net.translateException(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        }
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 String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        if (sc.isConnected())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            return "Socket[addr=" + getInetAddress() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                ",port=" + getPort() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                ",localport=" + getLocalPort() + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        return "Socket[unconnected]";
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 isConnected() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        return sc.isConnected();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    public boolean isBound() {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 715
diff changeset
   444
        return sc.localAddress() != null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    public boolean isClosed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        return !sc.isOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    public boolean isInputShutdown() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        return !sc.isInputOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    public boolean isOutputShutdown() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        return !sc.isOutputOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
}