Asynchronous socket channels are created in one of two ways. A newly-created
AsynchronousSocketChannel is created by invoking one of the open methods defined by this class. A newly-created channel is open but
not yet connected. A connected AsynchronousSocketChannel is created
when a connection is made to the socket of an AsynchronousServerSocketChannel.
It is not possible to create an asynchronous socket channel for an arbitrary,
pre-existing socket.
A newly-created channel is connected by invoking its connect
method; once connected, a channel remains connected until it is closed. Whether
or not a socket channel is connected may be determined by invoking its getRemoteAddress method. An attempt to invoke an I/O
operation upon an unconnected channel will cause a NotYetConnectedException
to be thrown.
Channels of this type are safe for use by multiple concurrent threads.
They support concurrent reading and writing, though at most one read operation
and one write operation can be outstanding at any time.
If a thread initiates a read operation before a previous read operation has
completed then a ReadPendingException will be thrown. Similarly, an
attempt to initiate a write operation before a previous write has completed
will throw a WritePendingException.
Socket options are configured using the setOption method. Asynchronous socket channels support the following options:
Additional (implementation specific) options may also be supported.
Option Name Description SO_SNDBUFThe size of the socket send buffer SO_RCVBUFThe size of the socket receive buffer SO_KEEPALIVEKeep connection alive SO_REUSEADDRRe-use address TCP_NODELAYDisable the Nagle algorithm
The read
and write
methods defined by this class allow a timeout to be specified when initiating
a read or write operation. If the timeout elapses before an operation completes
then the operation completes with the exception InterruptedByTimeoutException. A timeout may leave the channel, or the
underlying connection, in an inconsistent state. Where the implementation
cannot guarantee that bytes have not been read from the channel then it puts
the channel into an implementation specific error state. A subsequent
attempt to initiate a read operation causes an unspecified runtime
exception to be thrown. Similarly if a write operation times out and
the implementation cannot guarantee bytes have not been written to the
channel then further attempts to write to the channel cause an
unspecified runtime exception to be thrown. When a timeout elapses then the
state of the ByteBuffer, or the sequence of buffers, for the I/O
operation is not defined. Buffers should be discarded or at least care must
be taken to ensure that the buffers are not accessed while the channel remains
open. All methods that accept timeout parameters treat values less than or
equal to zero to mean that the I/O operation does not timeout.
| Modifier and Type | Field and Description |
|---|---|
| private final AsynchronousChannelProvider |
| Access | Constructor and Description |
|---|---|
| protected | AsynchronousSocketChannel(AsynchronousChannelProvider
The provider that created this channel provider)Initializes a new instance of this class. |
| Modifier and Type | Method and Description |
|---|---|
| public abstract AsynchronousSocketChannel | bind(SocketAddress local)
Redeclares java. Binds the channel's socket to a local address. |
| public abstract < The type of the attachment A> void | connect(SocketAddress
The remote address to which this channel is to be connected remote, A The object to attach to the I/O operation; can be attachment, CompletionHandler<Void, ? super A> nullThe handler for consuming the result handler)Connects this channel. |
| public abstract Future | Returns: AFuture object representing the pending resultThe remote address to which this channel is to be connected remote)Connects this channel. |
| public abstract SocketAddress | Returns: TheSocketAddress that the socket is bound to; null
if the channel's socket is not boundRedeclares java. Returns the socket address that this channel's socket is bound to. |
| public abstract SocketAddress | Returns: The remote address;null if the channel's socket is not
connectedReturns the remote address to which this channel's socket is connected. |
| public static AsynchronousSocketChannel | Returns: A new asynchronous socket channelThe group to which the newly constructed channel should be bound,
or group)null for the default groupOpens an asynchronous socket channel. |
| public static AsynchronousSocketChannel | |
| public final AsynchronousChannelProvider | Returns: The provider that created this channelReturns the provider that created this channel. |
| public abstract < The type of the attachment A> void | read(ByteBuffer
The buffer into which bytes are to be transferred dst, long The maximum time for the I/O operation to complete timeout, TimeUnit The time unit of the unit, A timeout argumentThe object to attach to the I/O operation; can be attachment, CompletionHandler<Integer, ? super A> nullThe handler for consuming the result handler)Reads a sequence of bytes from this channel into the given buffer. |
| public final <A> void | read(ByteBuffer dst, A attachment, CompletionHandler<Integer, ? super A> handler)
Implements java. Reads a sequence of bytes from this channel into the given buffer. |
| public abstract Future | read(ByteBuffer dst)
Redeclares java. Reads a sequence of bytes from this channel into the given buffer. |
| public abstract < The type of the attachment A> void | read(ByteBuffer[]
The buffers into which bytes are to be transferred dsts, int The offset within the buffer array of the first buffer into which
bytes are to be transferred; must be non-negative and no larger than
offset, int dsts.lengthThe maximum number of buffers to be accessed; must be non-negative
and no larger than length, long dsts.length - offsetThe maximum time for the I/O operation to complete timeout, TimeUnit The time unit of the unit, A timeout argumentThe object to attach to the I/O operation; can be attachment, CompletionHandler<Long, ? super A> nullThe handler for consuming the result handler)Reads a sequence of bytes from this channel into a subsequence of the given buffers. |
| public abstract <T> AsynchronousSocketChannel | setOption(SocketOption<T> name, T value)
Redeclares java. Sets the value of a socket option. |
| public abstract AsynchronousSocketChannel | |
| public abstract AsynchronousSocketChannel | Returns: The channelShutdown the connection for writing without closing the channel. |
| public abstract < The type of the attachment A> void | write(ByteBuffer
The buffer from which bytes are to be retrieved src, long The maximum time for the I/O operation to complete timeout, TimeUnit The time unit of the unit, A timeout argumentThe object to attach to the I/O operation; can be attachment, CompletionHandler<Integer, ? super A> nullThe handler for consuming the result handler)Writes a sequence of bytes to this channel from the given buffer. |
| public final <A> void | write(ByteBuffer src, A attachment, CompletionHandler<Integer, ? super A> handler)
Implements java. Writes a sequence of bytes to this channel from the given buffer. |
| public abstract Future | write(ByteBuffer src)
Redeclares java. Writes a sequence of bytes to this channel from the given buffer. |
| public abstract < The type of the attachment A> void | write(ByteBuffer[]
The buffers from which bytes are to be retrieved srcs, int The offset within the buffer array of the first buffer from which
bytes are to be retrieved; must be non-negative and no larger
than offset, int srcs.lengthThe maximum number of buffers to be accessed; must be non-negative
and no larger than length, long srcs.length - offsetThe maximum time for the I/O operation to complete timeout, TimeUnit The time unit of the unit, A timeout argumentThe object to attach to the I/O operation; can be attachment, CompletionHandler<Long, ? super A> nullThe handler for consuming the result handler)Writes a sequence of bytes to this channel from a subsequence of the given buffers. |
| provider | back to summary |
|---|---|
| private final AsynchronousChannelProvider provider | |
| AsynchronousSocketChannel | back to summary |
|---|---|
| protected AsynchronousSocketChannel(AsynchronousChannelProvider provider) Initializes a new instance of this class.
| |
| bind | back to summary |
|---|---|
| public abstract AsynchronousSocketChannel bind(SocketAddress local) throws IOException Redeclares java. Doc from java. Binds the channel's socket to a local address. This method is used to establish an association between the socket and
a local address. Once an association is established then the socket remains
bound until the channel is closed. If the
| |
| connect | back to summary |
|---|---|
| public abstract <A> void connect(SocketAddress remote, A attachment, CompletionHandler<Void, ? super A> handler) Connects this channel. This method initiates an operation to connect this channel. The
| |
| connect | back to summary |
|---|---|
| public abstract Future Connects this channel. This method initiates an operation to connect this channel. This
method behaves in exactly the same manner as the
| |
| getLocalAddress | back to summary |
|---|---|
| public abstract SocketAddress getLocalAddress() throws IOException Redeclares java. Doc from java. Returns the socket address that this channel's socket is bound to. Where the channel is
| |
| getRemoteAddress | back to summary |
|---|---|
| public abstract SocketAddress getRemoteAddress() throws IOException Returns the remote address to which this channel's socket is connected. Where the channel is bound and connected to an Internet Protocol
socket address then the return value from this method is of type
| |
| open | back to summary |
|---|---|
| public static AsynchronousSocketChannel open(AsynchronousChannelGroup group) throws IOException Opens an asynchronous socket channel. The new channel is created by invoking the
| |
| open | back to summary |
|---|---|
| public static AsynchronousSocketChannel open() throws IOException Opens an asynchronous socket channel. This method returns an asynchronous socket channel that is bound to the default group.This method is equivalent to evaluating the expression: open((AsynchronousChannelGroup)null);
| |
| provider | back to summary |
|---|---|
| public final AsynchronousChannelProvider provider() Returns the provider that created this channel.
| |
| read | back to summary |
|---|---|
| public abstract <A> void read(ByteBuffer dst, long timeout, TimeUnit unit, A attachment, CompletionHandler<Integer, ? super A> handler) Reads a sequence of bytes from this channel into the given buffer. This method initiates an asynchronous read operation to read a
sequence of bytes from this channel into the given buffer. The If a timeout is specified and the timeout elapses before the operation
completes then the operation completes with the exception Otherwise this method works in the same manner as the
| |
| read | back to summary |
|---|---|
| public final <A> void read(ByteBuffer dst, A attachment, CompletionHandler<Integer, ? super A> handler) Implements java. Doc from java. Reads a sequence of bytes from this channel into the given buffer. This method initiates an asynchronous read operation to read a
sequence of bytes from this channel into the given buffer. The The read operation may read up to r bytes from the channel,
where r is the number of bytes remaining in the buffer, that is,
Suppose that a byte sequence of length n is read, where
Buffers are not safe for use by multiple concurrent threads so care should be taken to not access the buffer until the operation has completed. This method may be invoked at any time. Some channel types may not
allow more than one read to be outstanding at any given time. If a thread
initiates a read operation before a previous read operation has
completed then a
| |
| read | back to summary |
|---|---|
| public abstract Future Redeclares java. Doc from java. Reads a sequence of bytes from this channel into the given buffer. This method initiates an asynchronous read operation to read a
sequence of bytes from this channel into the given buffer. The method
behaves in exactly the same manner as the
| |
| read | back to summary |
|---|---|
| public abstract <A> void read(ByteBuffer[] dsts, int offset, int length, long timeout, TimeUnit unit, A attachment, CompletionHandler<Long, ? super A> handler) Reads a sequence of bytes from this channel into a subsequence of the
given buffers. This operation, sometimes called a scattering read,
is often useful when implementing network protocols that group data into
segments consisting of one or more fixed-length headers followed by a
variable-length body. The This method initiates a read of up to r bytes from this channel, where r is the total number of bytes remaining in the specified subsequence of the given buffer array, that is, dsts[offset].remaining() + dsts[offset+1].remaining() + ... + dsts[offset+length-1].remaining() Suppose that a byte sequence of length n is read, where
If a timeout is specified and the timeout elapses before the operation
completes then it completes with the exception
| |
| setOption | back to summary |
|---|---|
| public abstract <T> AsynchronousSocketChannel setOption(SocketOption<T> name, T value) throws IOException Redeclares java. Doc from java. Sets the value of a socket option.
| |
| shutdownInput | back to summary |
|---|---|
| public abstract AsynchronousSocketChannel shutdownInput() throws IOException Shutdown the connection for reading without closing the channel. Once shutdown for reading then further reads on the channel will
return
| |
| shutdownOutput | back to summary |
|---|---|
| public abstract AsynchronousSocketChannel shutdownOutput() throws IOException Shutdown the connection for writing without closing the channel. Once shutdown for writing then further attempts to write to the
channel will throw
| |
| write | back to summary |
|---|---|
| public abstract <A> void write(ByteBuffer src, long timeout, TimeUnit unit, A attachment, CompletionHandler<Integer, ? super A> handler) Writes a sequence of bytes to this channel from the given buffer. This method initiates an asynchronous write operation to write a
sequence of bytes to this channel from the given buffer. The If a timeout is specified and the timeout elapses before the operation
completes then it completes with the exception Otherwise this method works in the same manner as the
| |
| write | back to summary |
|---|---|
| public final <A> void write(ByteBuffer src, A attachment, CompletionHandler<Integer, ? super A> handler) Implements java. Doc from java. Writes a sequence of bytes to this channel from the given buffer. This method initiates an asynchronous write operation to write a
sequence of bytes to this channel from the given buffer. The The write operation may write up to r bytes to the channel,
where r is the number of bytes remaining in the buffer, that is,
Suppose that a byte sequence of length n is written, where
Buffers are not safe for use by multiple concurrent threads so care should be taken to not access the buffer until the operation has completed. This method may be invoked at any time. Some channel types may not
allow more than one write to be outstanding at any given time. If a thread
initiates a write operation before a previous write operation has
completed then a
| |
| write | back to summary |
|---|---|
| public abstract Future Redeclares java. Doc from java. Writes a sequence of bytes to this channel from the given buffer. This method initiates an asynchronous write operation to write a
sequence of bytes to this channel from the given buffer. The method
behaves in exactly the same manner as the
| |
| write | back to summary |
|---|---|
| public abstract <A> void write(ByteBuffer[] srcs, int offset, int length, long timeout, TimeUnit unit, A attachment, CompletionHandler<Long, ? super A> handler) Writes a sequence of bytes to this channel from a subsequence of the given
buffers. This operation, sometimes called a gathering write, is
often useful when implementing network protocols that group data into
segments consisting of one or more fixed-length headers followed by a
variable-length body. The This method initiates a write of up to r bytes to this channel, where r is the total number of bytes remaining in the specified subsequence of the given buffer array, that is, srcs[offset].remaining() + srcs[offset+1].remaining() + ... + srcs[offset+length-1].remaining() Suppose that a byte sequence of length n is written, where
If a timeout is specified and the timeout elapses before the operation
completes then it completes with the exception
| |