jdk/src/share/classes/java/nio/channels/SocketChannel.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 1152 29d6145d1097
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2000-2001 Sun Microsystems, Inc.  All Rights Reserved.
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 java.nio.channels;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.net.Socket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.SocketAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.nio.channels.spi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * A selectable channel for stream-oriented connecting sockets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <p> Socket channels are not a complete abstraction of connecting network
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * sockets.  Binding, shutdown, and the manipulation of socket options must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * done through an associated {@link java.net.Socket} object obtained by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * invoking the {@link #socket() socket} method.  It is not possible to create
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * a channel for an arbitrary, pre-existing socket, nor is it possible to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * specify the {@link java.net.SocketImpl} object to be used by a socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * associated with a socket channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <p> A socket channel is created by invoking one of the {@link #open open}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * methods of this class.  A newly-created socket channel is open but not yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * connected.  An attempt to invoke an I/O operation upon an unconnected
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * channel will cause a {@link NotYetConnectedException} to be thrown.  A
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * socket channel can be connected by invoking its {@link #connect connect}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * method; once connected, a socket channel remains connected until it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * closed.  Whether or not a socket channel is connected may be determined by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * invoking its {@link #isConnected isConnected} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <p> Socket channels support <i>non-blocking connection:</i>&nbsp;A socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * channel may be created and the process of establishing the link to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * remote socket may be initiated via the {@link #connect connect} method for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * later completion by the {@link #finishConnect finishConnect} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * Whether or not a connection operation is in progress may be determined by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * invoking the {@link #isConnectionPending isConnectionPending} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <p> The input and output sides of a socket channel may independently be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * <i>shut down</i> without actually closing the channel.  Shutting down the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * input side of a channel by invoking the {@link java.net.Socket#shutdownInput
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * shutdownInput} method of an associated socket object will cause further
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * reads on the channel to return <tt>-1</tt>, the end-of-stream indication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * Shutting down the output side of the channel by invoking the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * java.net.Socket#shutdownOutput shutdownOutput} method of an associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * socket object will cause further writes on the channel to throw a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * ClosedChannelException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * <p> Socket channels support <i>asynchronous shutdown,</i> which is similar
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * to the asynchronous close operation specified in the {@link Channel} class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * If the input side of a socket is shut down by one thread while another
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * thread is blocked in a read operation on the socket's channel, then the read
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * operation in the blocked thread will complete without reading any bytes and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * will return <tt>-1</tt>.  If the output side of a socket is shut down by one
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * thread while another thread is blocked in a write operation on the socket's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * channel, then the blocked thread will receive an {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * AsynchronousCloseException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * <p> Socket channels are safe for use by multiple concurrent threads.  They
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * support concurrent reading and writing, though at most one thread may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * reading and at most one thread may be writing at any given time.  The {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * #connect connect} and {@link #finishConnect finishConnect} methods are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * mutually synchronized against each other, and an attempt to initiate a read
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * or write operation while an invocation of one of these methods is in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * progress will block until that invocation is complete.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * @author Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * @author JSR-51 Expert Group
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
public abstract class SocketChannel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    extends AbstractSelectableChannel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    implements ByteChannel, ScatteringByteChannel, GatheringByteChannel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * Initializes a new instance of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    protected SocketChannel(SelectorProvider provider) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        super(provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * Opens a socket channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * <p> The new channel is created by invoking the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * java.nio.channels.spi.SelectorProvider#openSocketChannel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * openSocketChannel} method of the system-wide default {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * java.nio.channels.spi.SelectorProvider} object.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @return  A new socket channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *          If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    public static SocketChannel open() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        return SelectorProvider.provider().openSocketChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * Opens a socket channel and connects it to a remote address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * <p> This convenience method works as if by invoking the {@link #open()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * method, invoking the {@link #connect(SocketAddress) connect} method upon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * the resulting socket channel, passing it <tt>remote</tt>, and then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * returning that channel.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @param  remote
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *         The remote address to which the new channel is to be connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @throws  AsynchronousCloseException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *          If another thread closes this channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *          while the connect operation is in progress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @throws  ClosedByInterruptException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *          If another thread interrupts the current thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *          while the connect operation is in progress, thereby
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *          closing the channel and setting the current thread's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *          interrupt status
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @throws  UnresolvedAddressException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *          If the given remote address is not fully resolved
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @throws  UnsupportedAddressTypeException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *          If the type of the given remote address is not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *          If a security manager has been installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *          and it does not permit access to the given remote endpoint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *          If some other I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public static SocketChannel open(SocketAddress remote)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        SocketChannel sc = open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            sc.connect(remote);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            if (!sc.isConnected()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                try { sc.close(); } catch (IOException x) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        assert sc.isConnected();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        return sc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * Returns an operation set identifying this channel's supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * <p> Socket channels support connecting, reading, and writing, so this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * method returns <tt>(</tt>{@link SelectionKey#OP_CONNECT}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * <tt>|</tt>&nbsp;{@link SelectionKey#OP_READ} <tt>|</tt>&nbsp;{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * SelectionKey#OP_WRITE}<tt>)</tt>.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @return  The valid-operation set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    public final int validOps() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        return (SelectionKey.OP_READ
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                | SelectionKey.OP_WRITE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                | SelectionKey.OP_CONNECT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    // -- Socket-specific operations --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * Retrieves a socket associated with this channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * <p> The returned object will not declare any public methods that are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * declared in the {@link java.net.Socket} class.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @return  A socket associated with this channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    public abstract Socket socket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * Tells whether or not this channel's network socket is connected.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @return  <tt>true</tt> if, and only if, this channel's network socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *          is connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    public abstract boolean isConnected();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * Tells whether or not a connection operation is in progress on this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * channel.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @return  <tt>true</tt> if, and only if, a connection operation has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     *          initiated on this channel but not yet completed by invoking the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *          {@link #finishConnect finishConnect} method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    public abstract boolean isConnectionPending();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * Connects this channel's socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * <p> If this channel is in non-blocking mode then an invocation of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * method initiates a non-blocking connection operation.  If the connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * is established immediately, as can happen with a local connection, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * this method returns <tt>true</tt>.  Otherwise this method returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * <tt>false</tt> and the connection operation must later be completed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * invoking the {@link #finishConnect finishConnect} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * <p> If this channel is in blocking mode then an invocation of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * method will block until the connection is established or an I/O error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * <p> This method performs exactly the same security checks as the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * java.net.Socket} class.  That is, if a security manager has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * installed then this method verifies that its {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * java.lang.SecurityManager#checkConnect checkConnect} method permits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * connecting to the address and port number of the given remote endpoint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * <p> This method may be invoked at any time.  If a read or write
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * operation upon this channel is invoked while an invocation of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * method is in progress then that operation will first block until this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * invocation is complete.  If a connection attempt is initiated but fails,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * that is, if an invocation of this method throws a checked exception,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * then the channel will be closed.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @param  remote
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     *         The remote address to which this channel is to be connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * @return  <tt>true</tt> if a connection was established,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *          <tt>false</tt> if this channel is in non-blocking mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     *          and the connection operation is in progress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * @throws  AlreadyConnectedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     *          If this channel is already connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * @throws  ConnectionPendingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     *          If a non-blocking connection operation is already in progress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *          on this channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @throws  ClosedChannelException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     *          If this channel is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * @throws  AsynchronousCloseException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *          If another thread closes this channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *          while the connect operation is in progress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * @throws  ClosedByInterruptException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *          If another thread interrupts the current thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *          while the connect operation is in progress, thereby
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *          closing the channel and setting the current thread's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *          interrupt status
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * @throws  UnresolvedAddressException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *          If the given remote address is not fully resolved
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @throws  UnsupportedAddressTypeException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     *          If the type of the given remote address is not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *          If a security manager has been installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *          and it does not permit access to the given remote endpoint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     *          If some other I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    public abstract boolean connect(SocketAddress remote) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * Finishes the process of connecting a socket channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * <p> A non-blocking connection operation is initiated by placing a socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * channel in non-blocking mode and then invoking its {@link #connect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * connect} method.  Once the connection is established, or the attempt has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * failed, the socket channel will become connectable and this method may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * be invoked to complete the connection sequence.  If the connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * operation failed then invoking this method will cause an appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * {@link java.io.IOException} to be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * <p> If this channel is already connected then this method will not block
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * and will immediately return <tt>true</tt>.  If this channel is in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * non-blocking mode then this method will return <tt>false</tt> if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * connection process is not yet complete.  If this channel is in blocking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * mode then this method will block until the connection either completes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * or fails, and will always either return <tt>true</tt> or throw a checked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * exception describing the failure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * <p> This method may be invoked at any time.  If a read or write
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * operation upon this channel is invoked while an invocation of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * method is in progress then that operation will first block until this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * invocation is complete.  If a connection attempt fails, that is, if an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * invocation of this method throws a checked exception, then the channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * will be closed.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * @return  <tt>true</tt> if, and only if, this channel's socket is now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *          connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * @throws  NoConnectionPendingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     *          If this channel is not connected and a connection operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     *          has not been initiated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * @throws  ClosedChannelException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     *          If this channel is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * @throws  AsynchronousCloseException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     *          If another thread closes this channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     *          while the connect operation is in progress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * @throws  ClosedByInterruptException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *          If another thread interrupts the current thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     *          while the connect operation is in progress, thereby
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *          closing the channel and setting the current thread's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     *          interrupt status
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     *          If some other I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    public abstract boolean finishConnect() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    // -- ByteChannel operations --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * @throws  NotYetConnectedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     *          If this channel is not yet connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    public abstract int read(ByteBuffer dst) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * @throws  NotYetConnectedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     *          If this channel is not yet connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    public abstract long read(ByteBuffer[] dsts, int offset, int length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * @throws  NotYetConnectedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     *          If this channel is not yet connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    public final long read(ByteBuffer[] dsts) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        return read(dsts, 0, dsts.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * @throws  NotYetConnectedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     *          If this channel is not yet connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    public abstract int write(ByteBuffer src) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * @throws  NotYetConnectedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     *          If this channel is not yet connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    public abstract long write(ByteBuffer[] srcs, int offset, int length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * @throws  NotYetConnectedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     *          If this channel is not yet connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    public final long write(ByteBuffer[] srcs) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        return write(srcs, 0, srcs.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
}