jdk/src/share/classes/java/nio/channels/SocketChannel.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 8539 eeb9fc5a68c1
child 9679 d98ae8bc45fc
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8539
diff changeset
     2
 * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2057
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2057
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2057
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2057
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2057
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package 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;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    30
import java.net.SocketOption;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.net.SocketAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.nio.ByteBuffer;
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
    33
import java.nio.channels.spi.AbstractSelectableChannel;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
    34
import java.nio.channels.spi.SelectorProvider;
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
 * A selectable channel for stream-oriented connecting sockets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <p> A socket channel is created by invoking one of the {@link #open open}
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    40
 * methods of this class.  It is not possible to create a channel for an arbitrary,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    41
 * pre-existing socket. A newly-created socket channel is open but not yet
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * connected.  An attempt to invoke an I/O operation upon an unconnected
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * channel will cause a {@link NotYetConnectedException} to be thrown.  A
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * socket channel can be connected by invoking its {@link #connect connect}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * method; once connected, a socket channel remains connected until it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * closed.  Whether or not a socket channel is connected may be determined by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * invoking its {@link #isConnected isConnected} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <p> Socket channels support <i>non-blocking connection:</i>&nbsp;A socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * channel may be created and the process of establishing the link to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * remote socket may be initiated via the {@link #connect connect} method for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * later completion by the {@link #finishConnect finishConnect} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * Whether or not a connection operation is in progress may be determined by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * invoking the {@link #isConnectionPending isConnectionPending} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <p> Socket channels support <i>asynchronous shutdown,</i> which is similar
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * to the asynchronous close operation specified in the {@link Channel} class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * If the input side of a socket is shut down by one thread while another
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * thread is blocked in a read operation on the socket's channel, then the read
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * operation in the blocked thread will complete without reading any bytes and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * will return <tt>-1</tt>.  If the output side of a socket is shut down by one
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * thread while another thread is blocked in a write operation on the socket's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * channel, then the blocked thread will receive an {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * AsynchronousCloseException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    66
 * <p> Socket options are configured using the {@link #setOption(SocketOption,Object)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    67
 * setOption} method. Socket channels support the following options:
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    68
 * <blockquote>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    69
 * <table border>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    70
 *   <tr>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    71
 *     <th>Option Name</th>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    72
 *     <th>Description</th>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    73
 *   </tr>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    74
 *   <tr>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    75
 *     <td> {@link java.net.StandardSocketOption#SO_SNDBUF SO_SNDBUF} </td>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    76
 *     <td> The size of the socket send buffer </td>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    77
 *   </tr>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    78
 *   <tr>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    79
 *     <td> {@link java.net.StandardSocketOption#SO_RCVBUF SO_RCVBUF} </td>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    80
 *     <td> The size of the socket receive buffer </td>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    81
 *   </tr>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    82
 *   <tr>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    83
 *     <td> {@link java.net.StandardSocketOption#SO_KEEPALIVE SO_KEEPALIVE} </td>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    84
 *     <td> Keep connection alive </td>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    85
 *   </tr>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    86
 *   <tr>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    87
 *     <td> {@link java.net.StandardSocketOption#SO_REUSEADDR SO_REUSEADDR} </td>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    88
 *     <td> Re-use address </td>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    89
 *   </tr>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    90
 *   <tr>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    91
 *     <td> {@link java.net.StandardSocketOption#SO_LINGER SO_LINGER} </td>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    92
 *     <td> Linger on close if data is present (when configured in blocking mode
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    93
 *          only) </td>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    94
 *   </tr>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    95
 *   <tr>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    96
 *     <td> {@link java.net.StandardSocketOption#TCP_NODELAY TCP_NODELAY} </td>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    97
 *     <td> Disable the Nagle algorithm </td>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    98
 *   </tr>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    99
 * </table>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   100
 * </blockquote>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   101
 * Additional (implementation specific) options may also be supported.
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   102
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * <p> Socket channels are safe for use by multiple concurrent threads.  They
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * support concurrent reading and writing, though at most one thread may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * reading and at most one thread may be writing at any given time.  The {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * #connect connect} and {@link #finishConnect finishConnect} methods are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * mutually synchronized against each other, and an attempt to initiate a read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * or write operation while an invocation of one of these methods is in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * progress will block until that invocation is complete.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * @author Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * @author JSR-51 Expert Group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
public abstract class SocketChannel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    extends AbstractSelectableChannel
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   118
    implements ByteChannel, ScatteringByteChannel, GatheringByteChannel, NetworkChannel
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * Initializes a new instance of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    protected SocketChannel(SelectorProvider provider) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        super(provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * Opens a socket channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * <p> The new channel is created by invoking the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * java.nio.channels.spi.SelectorProvider#openSocketChannel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * openSocketChannel} method of the system-wide default {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * java.nio.channels.spi.SelectorProvider} object.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @return  A new socket channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *          If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    public static SocketChannel open() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        return SelectorProvider.provider().openSocketChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * Opens a socket channel and connects it to a remote address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * <p> This convenience method works as if by invoking the {@link #open()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * method, invoking the {@link #connect(SocketAddress) connect} method upon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * the resulting socket channel, passing it <tt>remote</tt>, and then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * returning that channel.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @param  remote
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *         The remote address to which the new channel is to be connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @throws  AsynchronousCloseException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *          If another thread closes this channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *          while the connect operation is in progress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @throws  ClosedByInterruptException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *          If another thread interrupts the current thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *          while the connect operation is in progress, thereby
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *          closing the channel and setting the current thread's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *          interrupt status
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @throws  UnresolvedAddressException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *          If the given remote address is not fully resolved
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @throws  UnsupportedAddressTypeException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *          If the type of the given remote address is not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *          If a security manager has been installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *          and it does not permit access to the given remote endpoint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     *          If some other I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    public static SocketChannel open(SocketAddress remote)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        SocketChannel sc = open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            sc.connect(remote);
8539
eeb9fc5a68c1 7020888: (file) Miscellaneous and trivial clean-ups (typos and opportunities to use suppressed exceptions)
alanb
parents: 5506
diff changeset
   185
        } catch (Throwable x) {
eeb9fc5a68c1 7020888: (file) Miscellaneous and trivial clean-ups (typos and opportunities to use suppressed exceptions)
alanb
parents: 5506
diff changeset
   186
            try {
eeb9fc5a68c1 7020888: (file) Miscellaneous and trivial clean-ups (typos and opportunities to use suppressed exceptions)
alanb
parents: 5506
diff changeset
   187
                sc.close();
eeb9fc5a68c1 7020888: (file) Miscellaneous and trivial clean-ups (typos and opportunities to use suppressed exceptions)
alanb
parents: 5506
diff changeset
   188
            } catch (Throwable suppressed) {
eeb9fc5a68c1 7020888: (file) Miscellaneous and trivial clean-ups (typos and opportunities to use suppressed exceptions)
alanb
parents: 5506
diff changeset
   189
                x.addSuppressed(suppressed);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            }
8539
eeb9fc5a68c1 7020888: (file) Miscellaneous and trivial clean-ups (typos and opportunities to use suppressed exceptions)
alanb
parents: 5506
diff changeset
   191
            throw x;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        assert sc.isConnected();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        return sc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * Returns an operation set identifying this channel's supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * <p> Socket channels support connecting, reading, and writing, so this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * method returns <tt>(</tt>{@link SelectionKey#OP_CONNECT}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * <tt>|</tt>&nbsp;{@link SelectionKey#OP_READ} <tt>|</tt>&nbsp;{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * SelectionKey#OP_WRITE}<tt>)</tt>.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * @return  The valid-operation set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    public final int validOps() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        return (SelectionKey.OP_READ
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                | SelectionKey.OP_WRITE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                | SelectionKey.OP_CONNECT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    // -- Socket-specific operations --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    /**
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   218
     * @throws  ConnectionPendingException
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   219
     *          If a non-blocking connect operation is already in progress on
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   220
     *          this channel
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   221
     * @throws  AlreadyBoundException               {@inheritDoc}
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   222
     * @throws  UnsupportedAddressTypeException     {@inheritDoc}
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   223
     * @throws  ClosedChannelException              {@inheritDoc}
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   224
     * @throws  IOException                         {@inheritDoc}
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   225
     *
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   226
     * @since 1.7
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   227
     */
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   228
    @Override
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   229
    public abstract SocketChannel bind(SocketAddress local)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   230
        throws IOException;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   231
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   232
    /**
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   233
     * @throws  UnsupportedOperationException           {@inheritDoc}
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   234
     * @throws  IllegalArgumentException                {@inheritDoc}
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   235
     * @throws  ClosedChannelException                  {@inheritDoc}
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   236
     * @throws  IOException                             {@inheritDoc}
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   237
     *
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   238
     * @since 1.7
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   239
     */
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   240
    @Override
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   241
    public abstract <T> SocketChannel setOption(SocketOption<T> name, T value)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   242
        throws IOException;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   243
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   244
    /**
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   245
     * Shutdown the connection for reading without closing the channel.
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   246
     *
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   247
     * <p> Once shutdown for reading then further reads on the channel will
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   248
     * return {@code -1}, the end-of-stream indication. If the input side of the
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   249
     * connection is already shutdown then invoking this method has no effect.
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   250
     *
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   251
     * @return  The channel
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   252
     *
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   253
     * @throws  NotYetConnectedException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   254
     *          If this channel is not yet connected
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   255
     * @throws  ClosedChannelException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   256
     *          If this channel is closed
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   257
     * @throws  IOException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   258
     *          If some other I/O error occurs
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   259
     *
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   260
     * @since 1.7
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   261
     */
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   262
    public abstract SocketChannel shutdownInput() throws IOException;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   263
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   264
    /**
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   265
     * Shutdown the connection for writing without closing the channel.
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   266
     *
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   267
     * <p> Once shutdown for writing then further attempts to write to the
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   268
     * channel will throw {@link ClosedChannelException}. If the output side of
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   269
     * the connection is already shutdown then invoking this method has no
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   270
     * effect.
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   271
     *
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   272
     * @return  The channel
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   273
     *
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   274
     * @throws  NotYetConnectedException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   275
     *          If this channel is not yet connected
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   276
     * @throws  ClosedChannelException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   277
     *          If this channel is closed
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   278
     * @throws  IOException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   279
     *          If some other I/O error occurs
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   280
     *
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   281
     * @since 1.7
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   282
     */
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   283
    public abstract SocketChannel shutdownOutput() throws IOException;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   284
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   285
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * Retrieves a socket associated with this channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * <p> The returned object will not declare any public methods that are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * declared in the {@link java.net.Socket} class.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * @return  A socket associated with this channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    public abstract Socket socket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    /**
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   296
     * Tells whether or not this channel's network socket is connected.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * @return  <tt>true</tt> if, and only if, this channel's network socket
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   299
     *          is {@link #isOpen open} and connected
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    public abstract boolean isConnected();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * Tells whether or not a connection operation is in progress on this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * channel.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * @return  <tt>true</tt> if, and only if, a connection operation has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     *          initiated on this channel but not yet completed by invoking the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *          {@link #finishConnect finishConnect} method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    public abstract boolean isConnectionPending();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * Connects this channel's socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * <p> If this channel is in non-blocking mode then an invocation of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * method initiates a non-blocking connection operation.  If the connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * is established immediately, as can happen with a local connection, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * this method returns <tt>true</tt>.  Otherwise this method returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * <tt>false</tt> and the connection operation must later be completed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * invoking the {@link #finishConnect finishConnect} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * <p> If this channel is in blocking mode then an invocation of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * method will block until the connection is established or an I/O error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * <p> This method performs exactly the same security checks as the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * java.net.Socket} class.  That is, if a security manager has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * installed then this method verifies that its {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * java.lang.SecurityManager#checkConnect checkConnect} method permits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * connecting to the address and port number of the given remote endpoint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * <p> This method may be invoked at any time.  If a read or write
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * operation upon this channel is invoked while an invocation of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * method is in progress then that operation will first block until this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * invocation is complete.  If a connection attempt is initiated but fails,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * that is, if an invocation of this method throws a checked exception,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * then the channel will be closed.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * @param  remote
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     *         The remote address to which this channel is to be connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * @return  <tt>true</tt> if a connection was established,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     *          <tt>false</tt> if this channel is in non-blocking mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     *          and the connection operation is in progress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * @throws  AlreadyConnectedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     *          If this channel is already connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * @throws  ConnectionPendingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     *          If a non-blocking connection operation is already in progress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     *          on this channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * @throws  ClosedChannelException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     *          If this channel is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * @throws  AsynchronousCloseException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     *          If another thread closes this channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     *          while the connect operation is in progress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * @throws  ClosedByInterruptException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     *          If another thread interrupts the current thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     *          while the connect operation is in progress, thereby
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     *          closing the channel and setting the current thread's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *          interrupt status
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * @throws  UnresolvedAddressException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     *          If the given remote address is not fully resolved
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * @throws  UnsupportedAddressTypeException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     *          If the type of the given remote address is not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     *          If a security manager has been installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     *          and it does not permit access to the given remote endpoint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     *          If some other I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    public abstract boolean connect(SocketAddress remote) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * Finishes the process of connecting a socket channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * <p> A non-blocking connection operation is initiated by placing a socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * channel in non-blocking mode and then invoking its {@link #connect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * connect} method.  Once the connection is established, or the attempt has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * failed, the socket channel will become connectable and this method may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * be invoked to complete the connection sequence.  If the connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * operation failed then invoking this method will cause an appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * {@link java.io.IOException} to be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * <p> If this channel is already connected then this method will not block
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * and will immediately return <tt>true</tt>.  If this channel is in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * non-blocking mode then this method will return <tt>false</tt> if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * connection process is not yet complete.  If this channel is in blocking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * mode then this method will block until the connection either completes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * or fails, and will always either return <tt>true</tt> or throw a checked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * exception describing the failure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * <p> This method may be invoked at any time.  If a read or write
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * operation upon this channel is invoked while an invocation of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * method is in progress then that operation will first block until this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * invocation is complete.  If a connection attempt fails, that is, if an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * invocation of this method throws a checked exception, then the channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * will be closed.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * @return  <tt>true</tt> if, and only if, this channel's socket is now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     *          connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * @throws  NoConnectionPendingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     *          If this channel is not connected and a connection operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     *          has not been initiated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * @throws  ClosedChannelException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     *          If this channel is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * @throws  AsynchronousCloseException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     *          If another thread closes this channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     *          while the connect operation is in progress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * @throws  ClosedByInterruptException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     *          If another thread interrupts the current thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     *          while the connect operation is in progress, thereby
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     *          closing the channel and setting the current thread's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     *          interrupt status
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     *          If some other I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    public abstract boolean finishConnect() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   433
    /**
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   434
     * Returns the remote address to which this channel's socket is connected.
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   435
     *
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   436
     * <p> Where the channel is bound and connected to an Internet Protocol
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   437
     * socket address then the return value from this method is of type {@link
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   438
     * java.net.InetSocketAddress}.
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   439
     *
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   440
     * @return  The remote address; {@code null} if the channel's socket is not
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   441
     *          connected
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   442
     *
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   443
     * @throws  ClosedChannelException
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   444
     *          If the channel is closed
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   445
     * @throws  IOException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   446
     *          If an I/O error occurs
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   447
     *
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   448
     * @since 1.7
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   449
     */
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   450
    public abstract SocketAddress getRemoteAddress() throws IOException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    // -- ByteChannel operations --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * @throws  NotYetConnectedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     *          If this channel is not yet connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    public abstract int read(ByteBuffer dst) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * @throws  NotYetConnectedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     *          If this channel is not yet connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    public abstract long read(ByteBuffer[] dsts, int offset, int length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * @throws  NotYetConnectedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     *          If this channel is not yet connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    public final long read(ByteBuffer[] dsts) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        return read(dsts, 0, dsts.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * @throws  NotYetConnectedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     *          If this channel is not yet connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    public abstract int write(ByteBuffer src) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * @throws  NotYetConnectedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     *          If this channel is not yet connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    public abstract long write(ByteBuffer[] srcs, int offset, int length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * @throws  NotYetConnectedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     *          If this channel is not yet connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    public final long write(ByteBuffer[] srcs) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        return write(srcs, 0, srcs.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
}