jdk/src/share/classes/sun/nio/ch/SocketChannelImpl.java
author alanb
Sun, 15 Feb 2009 12:25:54 +0000
changeset 2057 3acf8e5e2ca0
parent 1247 b4c26443dee5
child 2446 07047237e4d4
permissions -rw-r--r--
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99 4313887: New I/O: Improved filesystem interface 4607272: New I/O: Support asynchronous I/O Reviewed-by: sherman, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
     2
 * Copyright 2000-2009 Sun Microsystems, Inc.  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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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.FileDescriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.nio.channels.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.nio.channels.spi.*;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    34
import java.util.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * An implementation of SocketChannels
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
class SocketChannelImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    extends SocketChannel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    implements SelChImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    // Used to make native read and write calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static NativeDispatcher nd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    // Our file descriptor object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private final FileDescriptor fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    // fd value needed for dev/poll. This value will remain valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    // even after the value in the file descriptor object has been set to -1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private final int fdVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    // IDs of native threads doing reads and writes, for signalling
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private volatile long readerThread = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private volatile long writerThread = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    // Lock held by current reading or connecting thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private final Object readLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    // Lock held by current writing or connecting thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private final Object writeLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    // Lock held by any thread that modifies the state fields declared below
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    // DO NOT invoke a blocking I/O operation while holding this lock!
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private final Object stateLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    // -- The following fields are protected by stateLock
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    // State, increases monotonically
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private static final int ST_UNINITIALIZED = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private static final int ST_UNCONNECTED = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private static final int ST_PENDING = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private static final int ST_CONNECTED = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private static final int ST_KILLPENDING = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private static final int ST_KILLED = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private int state = ST_UNINITIALIZED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    // Binding
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    82
    private SocketAddress localAddress;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    83
    private SocketAddress remoteAddress;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    // Input/Output open
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private boolean isInputOpen = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    private boolean isOutputOpen = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private boolean readyToConnect = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    // Socket adaptor, created on demand
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    91
    private Socket socket;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    // -- End of fields protected by stateLock
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    // Constructor for normal connecting sockets
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    SocketChannelImpl(SelectorProvider sp) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        super(sp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        this.fd = Net.socket(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        this.fdVal = IOUtil.fdVal(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        this.state = ST_UNCONNECTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    // Constructor for sockets obtained from server sockets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    SocketChannelImpl(SelectorProvider sp,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                      FileDescriptor fd, InetSocketAddress remote)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        super(sp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        this.fd = fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        this.fdVal = IOUtil.fdVal(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        this.state = ST_CONNECTED;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   115
        this.localAddress = Net.localAddress(fd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        this.remoteAddress = remote;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    public Socket socket() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            if (socket == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                socket = SocketAdaptor.create(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            return socket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   127
    @Override
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   128
    public SocketAddress getLocalAddress() throws IOException {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   129
        synchronized (stateLock) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   130
            if (!isOpen())
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   131
                throw new ClosedChannelException();
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   132
            return localAddress;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   133
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   134
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   135
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   136
    @Override
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   137
    public SocketAddress getRemoteAddress() throws IOException {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   138
        synchronized (stateLock) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   139
            if (!isOpen())
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   140
                throw new ClosedChannelException();
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   141
            return remoteAddress;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   142
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   143
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   144
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   145
    @Override
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   146
    public <T> SocketChannel setOption(SocketOption<T> name, T value)
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   147
        throws IOException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   148
    {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   149
        if (name == null)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   150
            throw new NullPointerException();
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   151
        if (!supportedOptions().contains(name))
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   152
            throw new UnsupportedOperationException("'" + name + "' not supported");
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   153
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   154
        synchronized (stateLock) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   155
            if (!isOpen())
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   156
                throw new ClosedChannelException();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   157
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   158
            // special handling for IP_TOS: no-op when IPv6
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   159
            if (name == StandardSocketOption.IP_TOS) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   160
                if (!Net.isIPv6Available())
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   161
                    Net.setSocketOption(fd, StandardProtocolFamily.INET, name, value);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   162
                return this;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   163
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   164
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   165
            // no options that require special handling
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   166
            Net.setSocketOption(fd, Net.UNSPEC, name, value);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   167
            return this;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   168
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   169
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   170
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   171
    @Override
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   172
    @SuppressWarnings("unchecked")
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   173
    public <T> T getOption(SocketOption<T> name)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   174
        throws IOException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   175
    {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   176
        if (name == null)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   177
            throw new NullPointerException();
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   178
        if (!supportedOptions().contains(name))
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   179
            throw new UnsupportedOperationException("'" + name + "' not supported");
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   180
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   181
        synchronized (stateLock) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   182
            if (!isOpen())
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   183
                throw new ClosedChannelException();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   184
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   185
            // special handling for IP_TOS: always return 0 when IPv6
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   186
            if (name == StandardSocketOption.IP_TOS) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   187
                return (Net.isIPv6Available()) ? (T) Integer.valueOf(0) :
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   188
                    (T) Net.getSocketOption(fd, StandardProtocolFamily.INET, name);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   189
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   190
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   191
            // no options that require special handling
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   192
            return (T) Net.getSocketOption(fd, Net.UNSPEC, name);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   193
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   194
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   195
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   196
    private static class DefaultOptionsHolder {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   197
        static final Set<SocketOption<?>> defaultOptions = defaultOptions();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   198
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   199
        private static Set<SocketOption<?>> defaultOptions() {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   200
            HashSet<SocketOption<?>> set = new HashSet<SocketOption<?>>(8);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   201
            set.add(StandardSocketOption.SO_SNDBUF);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   202
            set.add(StandardSocketOption.SO_RCVBUF);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   203
            set.add(StandardSocketOption.SO_KEEPALIVE);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   204
            set.add(StandardSocketOption.SO_REUSEADDR);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   205
            set.add(StandardSocketOption.SO_LINGER);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   206
            set.add(StandardSocketOption.TCP_NODELAY);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   207
            // additional options required by socket adaptor
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   208
            set.add(StandardSocketOption.IP_TOS);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   209
            set.add(ExtendedSocketOption.SO_OOBINLINE);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   210
            return Collections.unmodifiableSet(set);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   211
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   212
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   213
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   214
    @Override
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   215
    public final Set<SocketOption<?>> supportedOptions() {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   216
        return DefaultOptionsHolder.defaultOptions;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   217
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   218
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    private boolean ensureReadOpen() throws ClosedChannelException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                throw new ClosedChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            if (!isConnected())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                throw new NotYetConnectedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            if (!isInputOpen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    private void ensureWriteOpen() throws ClosedChannelException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                throw new ClosedChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            if (!isOutputOpen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                throw new ClosedChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            if (!isConnected())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                throw new NotYetConnectedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    private void readerCleanup() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            readerThread = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            if (state == ST_KILLPENDING)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                kill();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    private void writerCleanup() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            writerThread = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            if (state == ST_KILLPENDING)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                kill();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    public int read(ByteBuffer buf) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        if (buf == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        synchronized (readLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            if (!ensureReadOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                // Set up the interruption machinery; see
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                // AbstractInterruptibleChannel for details
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                begin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                    if (!isOpen()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                    // Either the current thread is already interrupted, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                    // begin() closed the channel, or another thread closed the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                    // channel since we checked it a few bytecodes ago.  In
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                    // either case the value returned here is irrelevant since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                    // the invocation of end() in the finally block will throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                    // an appropriate exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                    // Save this thread so that it can be signalled on those
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                    // platforms that require it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                    readerThread = NativeThread.current();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                // Between the previous test of isOpen() and the return of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                // IOUtil.read invocation below, this channel might be closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                // or this thread might be interrupted.  We rely upon the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                // implicit synchronization point in the kernel read() call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                // make sure that the right thing happens.  In either case the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                // implCloseSelectableChannel method is ultimately invoked in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                // some other thread, so there are three possibilities:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                //   - implCloseSelectableChannel() invokes nd.preClose()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                //     before this thread invokes read(), in which case the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                //     read returns immediately with either EOF or an error,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                //     the latter of which will cause an IOException to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                //     thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                //   - implCloseSelectableChannel() invokes nd.preClose() after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                //     this thread is blocked in read().  On some operating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                //     systems (e.g., Solaris and Windows) this causes the read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                //     to return immediately with either EOF or an error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                //     indication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                //   - implCloseSelectableChannel() invokes nd.preClose() after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                //     this thread is blocked in read() but the operating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                //     system (e.g., Linux) doesn't support preemptive close,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                //     so implCloseSelectableChannel() proceeds to signal this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                //     thread, thereby causing the read to return immediately
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                //     with IOStatus.INTERRUPTED.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                // In all three cases the invocation of end() in the finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                // clause will notice that the channel has been closed and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                // throw an appropriate exception (AsynchronousCloseException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                // or ClosedByInterruptException) if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                // *There is A fourth possibility. implCloseSelectableChannel()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                // invokes nd.preClose(), signals reader/writer thred and quickly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                // moves on to nd.close() in kill(), which does a real close.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                // Then a third thread accepts a new connection, opens file or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                // whatever that causes the released "fd" to be recycled. All
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                // above happens just between our last isOpen() check and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                // next kernel read reached, with the recycled "fd". The solution
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                // is to postpone the real kill() if there is a reader or/and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                // writer thread(s) over there "waiting", leave the cleanup/kill
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                // to the reader or writer thread. (the preClose() still happens
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                // so the connection gets cut off as usual).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                // For socket channels there is the additional wrinkle that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                // asynchronous shutdown works much like asynchronous close,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                // except that the channel is shutdown rather than completely
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                // closed.  This is analogous to the first two cases above,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                // except that the shutdown operation plays the role of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                // nd.preClose().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                    n = IOUtil.read(fd, buf, -1, nd, readLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    if ((n == IOStatus.INTERRUPTED) && isOpen()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                        // The system call was interrupted but the channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                        // is still open, so retry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                    return IOStatus.normalize(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                readerCleanup();        // Clear reader thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                // The end method, which is defined in our superclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                // AbstractInterruptibleChannel, resets the interruption
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                // machinery.  If its argument is true then it returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                // normally; otherwise it checks the interrupt and open state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                // of this channel and throws an appropriate exception if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                // necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                // So, if we actually managed to do any I/O in the above try
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                // block then we pass true to the end method.  We also pass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                // true if the channel was in non-blocking mode when the I/O
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                // operation was initiated but no data could be transferred;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                // this prevents spurious exceptions from being thrown in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                // rare event that a channel is closed or a thread is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                // interrupted at the exact moment that a non-blocking I/O
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                // request is made.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                end(n > 0 || (n == IOStatus.UNAVAILABLE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                // Extra case for socket channels: Asynchronous shutdown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                    if ((n <= 0) && (!isInputOpen))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                        return IOStatus.EOF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                assert IOStatus.check(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    private long read0(ByteBuffer[] bufs) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        if (bufs == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        synchronized (readLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            if (!ensureReadOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            long n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                begin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                    if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                    readerThread = NativeThread.current();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                    n = IOUtil.read(fd, bufs, nd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                    if ((n == IOStatus.INTERRUPTED) && isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                    return IOStatus.normalize(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                readerCleanup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                end(n > 0 || (n == IOStatus.UNAVAILABLE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                    if ((n <= 0) && (!isInputOpen))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                        return IOStatus.EOF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                assert IOStatus.check(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    public long read(ByteBuffer[] dsts, int offset, int length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        if ((offset < 0) || (length < 0) || (offset > dsts.length - length))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        // ## Fix IOUtil.write so that we can avoid this array copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        return read0(Util.subsequence(dsts, offset, length));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    public int write(ByteBuffer buf) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        if (buf == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        synchronized (writeLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            ensureWriteOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                begin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                    if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                    writerThread = NativeThread.current();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                    n = IOUtil.write(fd, buf, -1, nd, writeLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                    if ((n == IOStatus.INTERRUPTED) && isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                    return IOStatus.normalize(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                writerCleanup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                end(n > 0 || (n == IOStatus.UNAVAILABLE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                    if ((n <= 0) && (!isOutputOpen))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                        throw new AsynchronousCloseException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                assert IOStatus.check(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    public long write0(ByteBuffer[] bufs) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        if (bufs == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        synchronized (writeLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            ensureWriteOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            long n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                begin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                    if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                    writerThread = NativeThread.current();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                    n = IOUtil.write(fd, bufs, nd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                    if ((n == IOStatus.INTERRUPTED) && isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                    return IOStatus.normalize(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                writerCleanup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                end((n > 0) || (n == IOStatus.UNAVAILABLE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                    if ((n <= 0) && (!isOutputOpen))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                        throw new AsynchronousCloseException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                assert IOStatus.check(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    public long write(ByteBuffer[] srcs, int offset, int length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        if ((offset < 0) || (length < 0) || (offset > srcs.length - length))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        // ## Fix IOUtil.write so that we can avoid this array copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        return write0(Util.subsequence(srcs, offset, length));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    protected void implConfigureBlocking(boolean block) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        IOUtil.configureBlocking(fd, block);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    public SocketAddress localAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            return localAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    public SocketAddress remoteAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            return remoteAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   516
    @Override
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   517
    public SocketChannel bind(SocketAddress local) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        synchronized (readLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            synchronized (writeLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                synchronized (stateLock) {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   521
                    if (!isOpen())
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   522
                        throw new ClosedChannelException();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   523
                    if (state == ST_PENDING)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   524
                        throw new ConnectionPendingException();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                    if (localAddress != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                        throw new AlreadyBoundException();
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   527
                    InetSocketAddress isa = (local == null) ?
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   528
                        new InetSocketAddress(0) : Net.checkAddress(local);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                    Net.bind(fd, isa.getAddress(), isa.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                    localAddress = Net.localAddress(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        }
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   534
        return this;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    public boolean isConnected() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            return (state == ST_CONNECTED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    public boolean isConnectionPending() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            return (state == ST_PENDING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    void ensureOpenAndUnconnected() throws IOException { // package-private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                throw new ClosedChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            if (state == ST_CONNECTED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                throw new AlreadyConnectedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            if (state == ST_PENDING)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                throw new ConnectionPendingException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    public boolean connect(SocketAddress sa) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        int localPort = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        synchronized (readLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            synchronized (writeLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                ensureOpenAndUnconnected();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                InetSocketAddress isa = Net.checkAddress(sa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                if (sm != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                    sm.checkConnect(isa.getAddress().getHostAddress(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                                    isa.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                synchronized (blockingLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                    int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                            begin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                            synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                                if (!isOpen()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                                readerThread = NativeThread.current();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                                InetAddress ia = isa.getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                                if (ia.isAnyLocalAddress())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                                    ia = InetAddress.getLocalHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                                n = Net.connect(fd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                                                ia,
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   588
                                                isa.getPort());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                                if (  (n == IOStatus.INTERRUPTED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                                      && isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                            }
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   594
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   595
                            synchronized (stateLock) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   596
                                if (isOpen() && (localAddress == null) ||
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   597
                                    ((InetSocketAddress)localAddress)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   598
                                        .getAddress().isAnyLocalAddress())
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   599
                                {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   600
                                    // Socket was not bound before connecting or
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   601
                                    // Socket was bound with an "anyLocalAddress"
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   602
                                    localAddress = Net.localAddress(fd);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   603
                                }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   604
                            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   605
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                            readerCleanup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                            end((n > 0) || (n == IOStatus.UNAVAILABLE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                            assert IOStatus.check(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                    } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                        // If an exception was thrown, close the channel after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                        // invoking end() so as to avoid bogus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                        // AsynchronousCloseExceptions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                        close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                        throw x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                    synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                        remoteAddress = isa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                        if (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                            // Connection succeeded; disallow further
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                            // invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                            state = ST_CONNECTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                        // If nonblocking and no exception then connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                        // pending; disallow another invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                        if (!isBlocking())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                            state = ST_PENDING;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                            assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    public boolean finishConnect() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        synchronized (readLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            synchronized (writeLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                    if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                        throw new ClosedChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                    if (state == ST_CONNECTED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                    if (state != ST_PENDING)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                        throw new NoConnectionPendingException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                        begin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                        synchronized (blockingLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                            synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                                if (!isOpen()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                                readerThread = NativeThread.current();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                            if (!isBlocking()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                                for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                                    n = checkConnect(fd, false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                                                     readyToConnect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                                    if (  (n == IOStatus.INTERRUPTED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                                          && isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                                for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                                    n = checkConnect(fd, true,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                                                     readyToConnect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                                    if (n == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                                        // Loop in case of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                                        // spurious notifications
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                                    if (  (n == IOStatus.INTERRUPTED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                                          && isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                    } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                            readerThread = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                            if (state == ST_KILLPENDING) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                                kill();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                                // poll()/getsockopt() does not report
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                                // error (throws exception, with n = 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                                // on Linux platform after dup2 and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                                // signal-wakeup. Force n to 0 so the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                                // end() can throw appropriate exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                                n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                        end((n > 0) || (n == IOStatus.UNAVAILABLE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                        assert IOStatus.check(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                    // If an exception was thrown, close the channel after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                    // invoking end() so as to avoid bogus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                    // AsynchronousCloseExceptions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                    close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                    throw x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                if (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                    synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                        state = ST_CONNECTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   721
    @Override
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   722
    public SocketChannel shutdownInput() throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                throw new ClosedChannelException();
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   726
            if (!isConnected())
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   727
                throw new NotYetConnectedException();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   728
            if (isInputOpen) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   729
                Net.shutdown(fd, Net.SHUT_RD);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   730
                if (readerThread != 0)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   731
                    NativeThread.signal(readerThread);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   732
                isInputOpen = false;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   733
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   734
            return this;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   738
    @Override
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   739
    public SocketChannel shutdownOutput() throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                throw new ClosedChannelException();
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   743
            if (!isConnected())
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   744
                throw new NotYetConnectedException();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   745
            if (isOutputOpen) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   746
                Net.shutdown(fd, Net.SHUT_WR);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   747
                if (writerThread != 0)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   748
                    NativeThread.signal(writerThread);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   749
                isOutputOpen = false;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   750
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   751
            return this;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
    public boolean isInputOpen() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            return isInputOpen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
    public boolean isOutputOpen() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            return isOutputOpen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    // AbstractInterruptibleChannel synchronizes invocations of this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
    // using AbstractInterruptibleChannel.closeLock, and also ensures that this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
    // method is only ever invoked once.  Before we get to this method, isOpen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
    // (which is volatile) will have been set to false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    protected void implCloseSelectableChannel() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            isInputOpen = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            isOutputOpen = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
            // Close the underlying file descriptor and dup it to a known fd
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            // that's already closed.  This prevents other operations on this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            // channel from using the old fd, which might be recycled in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            // meantime and allocated to an entirely different channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            nd.preClose(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            // Signal native threads, if needed.  If a target thread is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            // currently blocked in an I/O operation then no harm is done since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            // the signal handler doesn't actually do anything.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            if (readerThread != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                NativeThread.signal(readerThread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            if (writerThread != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                NativeThread.signal(writerThread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            // If this channel is not registered then it's safe to close the fd
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            // immediately since we know at this point that no thread is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            // blocked in an I/O operation upon the channel and, since the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            // channel is marked closed, no thread will start another such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            // operation.  If this channel is registered then we don't close
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            // the fd since it might be in use by a selector.  In that case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            // closing this channel caused its keys to be cancelled, so the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            // last selector to deregister a key for this channel will invoke
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            // kill() to close the fd.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            if (!isRegistered())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                kill();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
    public void kill() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            if (state == ST_KILLED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
            if (state == ST_UNINITIALIZED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                state = ST_KILLED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
            assert !isOpen() && !isRegistered();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
            // Postpone the kill if there is a waiting reader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
            // or writer thread. See the comments in read() for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
            // more detailed explanation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
            if (readerThread == 0 && writerThread == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
                nd.close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
                state = ST_KILLED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
                state = ST_KILLPENDING;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     * Translates native poll revent ops into a ready operation ops
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
    public boolean translateReadyOps(int ops, int initialOps,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                                     SelectionKeyImpl sk) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        int intOps = sk.nioInterestOps(); // Do this just once, it synchronizes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        int oldOps = sk.nioReadyOps();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        int newOps = initialOps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        if ((ops & PollArrayWrapper.POLLNVAL) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            // This should only happen if this channel is pre-closed while a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
            // selection operation is in progress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
            // ## Throw an error if this channel has not been pre-closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        if ((ops & (PollArrayWrapper.POLLERR
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
                    | PollArrayWrapper.POLLHUP)) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
            newOps = intOps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
            sk.nioReadyOps(newOps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
            // No need to poll again in checkConnect,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
            // the error will be detected there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
            readyToConnect = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
            return (newOps & ~oldOps) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        if (((ops & PollArrayWrapper.POLLIN) != 0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
            ((intOps & SelectionKey.OP_READ) != 0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
            (state == ST_CONNECTED))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
            newOps |= SelectionKey.OP_READ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        if (((ops & PollArrayWrapper.POLLCONN) != 0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
            ((intOps & SelectionKey.OP_CONNECT) != 0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
            ((state == ST_UNCONNECTED) || (state == ST_PENDING))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
            newOps |= SelectionKey.OP_CONNECT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
            readyToConnect = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        if (((ops & PollArrayWrapper.POLLOUT) != 0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
            ((intOps & SelectionKey.OP_WRITE) != 0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
            (state == ST_CONNECTED))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
            newOps |= SelectionKey.OP_WRITE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        sk.nioReadyOps(newOps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        return (newOps & ~oldOps) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
    public boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl sk) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        return translateReadyOps(ops, sk.nioReadyOps(), sk);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
    public boolean translateAndSetReadyOps(int ops, SelectionKeyImpl sk) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        return translateReadyOps(ops, 0, sk);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     * Translates an interest operation set into a native poll event set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
    public void translateAndSetInterestOps(int ops, SelectionKeyImpl sk) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
        int newOps = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
        if ((ops & SelectionKey.OP_READ) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
            newOps |= PollArrayWrapper.POLLIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        if ((ops & SelectionKey.OP_WRITE) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
            newOps |= PollArrayWrapper.POLLOUT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        if ((ops & SelectionKey.OP_CONNECT) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
            newOps |= PollArrayWrapper.POLLCONN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
        sk.selector.putEventOps(sk, newOps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
    public FileDescriptor getFD() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        return fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
    public int getFDVal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        return fdVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        StringBuffer sb = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        sb.append(this.getClass().getSuperclass().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        sb.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
            sb.append("closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
                switch (state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
                case ST_UNCONNECTED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
                    sb.append("unconnected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
                case ST_PENDING:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
                    sb.append("connection-pending");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
                case ST_CONNECTED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
                    sb.append("connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
                    if (!isInputOpen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
                        sb.append(" ishut");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
                    if (!isOutputOpen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
                        sb.append(" oshut");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
                if (localAddress() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
                    sb.append(" local=");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
                    sb.append(localAddress().toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
                if (remoteAddress() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
                    sb.append(" remote=");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
                    sb.append(remoteAddress().toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        sb.append(']');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
    // -- Native methods --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
    private static native int checkConnect(FileDescriptor fd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
                                           boolean block, boolean ready)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        Util.load();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        nd = new SocketDispatcher();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
}