|
NIO2 API | |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.classpath.icedtea.java.nio.channels.AsynchronousSocketChannel
public abstract class AsynchronousSocketChannel
An asynchronous channel for stream-oriented connecting sockets.
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.
| Constructor Summary | |
|---|---|
protected |
AsynchronousSocketChannel(AsynchronousChannelProvider provider)
Initializes a new instance of this class. |
| Method Summary | ||
|---|---|---|
abstract AsynchronousSocketChannel |
bind(java.net.SocketAddress local)
Binds the channel's socket to a local address. |
|
java.util.concurrent.Future<java.lang.Void> |
connect(java.net.SocketAddress remote)
Connects this channel. |
|
abstract
|
connect(java.net.SocketAddress remote,
A attachment,
CompletionHandler<java.lang.Void,? super A> handler)
Connects this channel. |
|
abstract java.net.SocketAddress |
getRemoteAddress()
Returns the remote address to which this channel's socket is connected. |
|
static AsynchronousSocketChannel |
open()
Opens an asynchronous socket channel. |
|
static AsynchronousSocketChannel |
open(AsynchronousChannelGroup group)
Opens an asynchronous socket channel. |
|
AsynchronousChannelProvider |
provider()
Returns the provider that created this channel. |
|
java.util.concurrent.Future<java.lang.Integer> |
read(java.nio.ByteBuffer dst)
Reads a sequence of bytes from this channel into the given buffer. |
|
abstract
|
read(java.nio.ByteBuffer[] dsts,
int offset,
int length,
long timeout,
java.util.concurrent.TimeUnit unit,
A attachment,
CompletionHandler<java.lang.Long,? super A> handler)
Reads a sequence of bytes from this channel into a subsequence of the given buffers. |
|
|
read(java.nio.ByteBuffer dst,
A attachment,
CompletionHandler<java.lang.Integer,? super A> handler)
Reads a sequence of bytes from this channel into the given buffer. |
|
abstract
|
read(java.nio.ByteBuffer dst,
long timeout,
java.util.concurrent.TimeUnit unit,
A attachment,
CompletionHandler<java.lang.Integer,? super A> handler)
Reads a sequence of bytes from this channel into the given buffer. |
|
abstract
|
setOption(SocketOption<T> name,
T value)
Sets the value of a socket option. |
|
abstract AsynchronousSocketChannel |
shutdownInput()
Shutdown the connection for reading without closing the channel. |
|
abstract AsynchronousSocketChannel |
shutdownOutput()
Shutdown the connection for writing without closing the channel. |
|
java.util.concurrent.Future<java.lang.Integer> |
write(java.nio.ByteBuffer src)
Writes a sequence of bytes to this channel from the given buffer. |
|
abstract
|
write(java.nio.ByteBuffer[] srcs,
int offset,
int length,
long timeout,
java.util.concurrent.TimeUnit unit,
A attachment,
CompletionHandler<java.lang.Long,? super A> handler)
Writes a sequence of bytes to this channel from a subsequence of the given buffers. |
|
|
write(java.nio.ByteBuffer src,
A attachment,
CompletionHandler<java.lang.Integer,? super A> handler)
Writes a sequence of bytes to this channel from the given buffer. |
|
abstract
|
write(java.nio.ByteBuffer src,
long timeout,
java.util.concurrent.TimeUnit unit,
A attachment,
CompletionHandler<java.lang.Integer,? super A> handler)
Writes a sequence of bytes to this channel from the given buffer. |
|
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Methods inherited from interface org.classpath.icedtea.java.nio.channels.AsynchronousChannel |
|---|
close |
| Methods inherited from interface java.nio.channels.Channel |
|---|
isOpen |
| Methods inherited from interface org.classpath.icedtea.java.nio.channels.NetworkChannel |
|---|
getLocalAddress, getOption, supportedOptions |
| Constructor Detail |
|---|
protected AsynchronousSocketChannel(AsynchronousChannelProvider provider)
| Method Detail |
|---|
public final AsynchronousChannelProvider provider()
public static AsynchronousSocketChannel open(AsynchronousChannelGroup group)
throws java.io.IOException
The new channel is created by invoking the openAsynchronousSocketChannel method on the AsynchronousChannelProvider that created the group. If the group parameter
is null then the resulting channel is created by the system-wide
default provider, and bound to the default group.
group - The group to which the newly constructed channel should be bound,
or null for the default group
ShutdownChannelGroupException - If the channel group is shutdown
java.io.IOException - If an I/O error occurs
public static AsynchronousSocketChannel open()
throws java.io.IOException
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);
java.io.IOException - If an I/O error occurs
public abstract AsynchronousSocketChannel bind(java.net.SocketAddress local)
throws java.io.IOException
NetworkChannel 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 local parameter has the
value null then the socket will be bound to an address that is
assigned automatically.
bind in interface NetworkChannellocal - The address to bind the socket, or null to bind the socket
to an automatically assigned socket address
ConnectionPendingException - If a connection operation is already in progress on this channel
AlreadyBoundException - If the socket is already bound
UnsupportedAddressTypeException - If the type of the given address is not supported
ClosedChannelException - If the channel is closed
java.io.IOException - If some other I/O error occursNetworkChannel.getLocalAddress()
public abstract <T> AsynchronousSocketChannel setOption(SocketOption<T> name,
T value)
throws java.io.IOException
NetworkChannel
setOption in interface NetworkChannelname - The socket optionvalue - The value of the socket option. A value of null may be
a valid value for some socket options.
java.lang.IllegalArgumentException - If the value is not a valid value for this socket option
ClosedChannelException - If this channel is closed
java.io.IOException - If an I/O error occursjava.net.StandardSocketOption
public abstract AsynchronousSocketChannel shutdownInput()
throws java.io.IOException
Once shutdown for reading then further reads on the channel will
return -1, the end-of-stream indication. If the input side of the
connection is already shutdown then invoking this method has no effect.
The effect on an outstanding read operation is system dependent and
therefore not specified. The effect, if any, when there is data in the
socket receive buffer that has not been read, or data arrives subsequently,
is also system dependent.
NotYetConnectedException - If this channel is not yet connected
ClosedChannelException - If this channel is closed
java.io.IOException - If some other I/O error occurs
public abstract AsynchronousSocketChannel shutdownOutput()
throws java.io.IOException
Once shutdown for writing then further attempts to write to the
channel will throw ClosedChannelException. If the output side of
the connection is already shutdown then invoking this method has no
effect. The effect on an outstanding write operation is system dependent
and therefore not specified.
NotYetConnectedException - If this channel is not yet connected
ClosedChannelException - If this channel is closed
java.io.IOException - If some other I/O error occurs
public abstract java.net.SocketAddress getRemoteAddress()
throws java.io.IOException
Where the channel is bound and connected to an Internet Protocol
socket address then the return value from this method is of type InetSocketAddress.
null if the channel's socket is not
connected
ClosedChannelException - If the channel is closed
java.io.IOException - If an I/O error occurs
public abstract <A> java.util.concurrent.Future<java.lang.Void> connect(java.net.SocketAddress remote,
A attachment,
CompletionHandler<java.lang.Void,? super A> handler)
This method initiates an operation to connect this channel, returning
a Future representing the pending result of the operation. If
the connection is successfully established then the Future's
get method will return null. If the
connection cannot be established then the channel is closed. In that case,
invoking the get method throws ExecutionException with an IOException as
the cause.
This method performs exactly the same security checks as the Socket class. That is, if a security manager has been
installed then this method verifies that its checkConnect method permits
connecting to the address and port number of the given remote endpoint.
remote - The remote address to which this channel is to be connectedattachment - The object to attach to the I/O operation; can be nullhandler - The handler for consuming the result; can be null
Future object representing the pending result
UnresolvedAddressException - If the given remote address is not fully resolved
UnsupportedAddressTypeException - If the type of the given remote address is not supported
AlreadyConnectedException - If this channel is already connected
ConnectionPendingException - If a connection operation is already in progress on this channel
ShutdownChannelGroupException - If a handler is specified, and the channel group is shutdown
java.lang.SecurityException - If a security manager has been installed
and it does not permit access to the given remote endpointgetRemoteAddress()public final java.util.concurrent.Future<java.lang.Void> connect(java.net.SocketAddress remote)
This method is equivalent to invoking connect(SocketAddress,Object,CompletionHandler) with the attachment
and handler parameters set to null.
remote - The remote address to which this channel is to be connected
Future object representing the pending result
UnresolvedAddressException - If the given remote address is not fully resolved
UnsupportedAddressTypeException - If the type of the given remote address is not supported
AlreadyConnectedException - If this channel is already connected
ConnectionPendingException - If a connection operation is already in progress on this channel
java.lang.SecurityException - If a security manager has been installed
and it does not permit access to the given remote endpoint
public abstract <A> java.util.concurrent.Future<java.lang.Integer> read(java.nio.ByteBuffer dst,
long timeout,
java.util.concurrent.TimeUnit unit,
A attachment,
CompletionHandler<java.lang.Integer,? super A> handler)
This method initiates the reading of a sequence of bytes from this
channel into the given buffer, returning a Future representing
the pending result of the operation. The Future's get method returns the number of bytes read or -1
if all bytes have been read and channel has reached end-of-stream.
If a timeout is specified and the timeout elapses before the operation
completes then the operation completes with the exception InterruptedByTimeoutException. Where a timeout occurs, and the
implementation cannot guarantee that bytes have not been read, or will not
be read from the channel into the given buffer, then further attempts to
read from the channel will cause an unspecific runtime exception to be
thrown.
Otherwise this method works in the same manner as the AsynchronousByteChannel.read(ByteBuffer,Object,CompletionHandler)
method.
dst - The buffer into which bytes are to be transferredtimeout - The timeout, or 0L for no timeoutunit - The time unit of the timeout argumentattachment - The object to attach to the I/O operation; can be nullhandler - The handler for consuming the result; can be null
Future object representing the pending result
java.lang.IllegalArgumentException - If the timeout parameter is negative or the buffer is
read-only
ReadPendingException - If a read operation is already in progress on this channel
NotYetConnectedException - If this channel is not yet connected
ShutdownChannelGroupException - If a handler is specified, and the channel group is shutdown
public final <A> java.util.concurrent.Future<java.lang.Integer> read(java.nio.ByteBuffer dst,
A attachment,
CompletionHandler<java.lang.Integer,? super A> handler)
AsynchronousByteChannel This method initiates an operation to read a sequence of bytes from
this channel into the given buffer. The method returns a Future
representing the pending result of the operation. The result of the
operation, obtained by invoking the Future 's get method, is the number of bytes read or -1 if
all bytes have been read and the channel has reached end-of-stream.
This method initiates a read operation to read up to r bytes
from the channel, where r is the number of bytes remaining in the
buffer, that is, dst.remaining() at the time that the read is
attempted. Where r is 0, the read operation completes immediately
with a result of 0 without initiating an I/O operation.
Suppose that a byte sequence of length n is read, where 0 < n <= r. This byte sequence will be transferred into the buffer so that the first byte in the sequence is at index p and the last byte is at index p + n - 1, where p is the buffer's position at the moment the read is performed. Upon completion the buffer's position will be equal to p + n; its limit will not have changed.
Buffers are not safe for use by multiple concurrent threads so care should be taken to not to access the buffer until the operaton 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 ReadPendingException will be thrown.
The handler parameter is used to specify a CompletionHandler. When the read operation completes the handler's
completed method is executed.
read in interface AsynchronousByteChanneldst - The buffer into which bytes are to be transferredattachment - The object to attach to the I/O operation; can be nullhandler - The completion handler object; can be null
java.lang.IllegalArgumentException - If the buffer is read-only
ReadPendingException - If the channel does not allow more than one read to be outstanding
and a previous read has not completed
NotYetConnectedException - If this channel is not yet connected
ShutdownChannelGroupException - If a handler is specified, and the channel group is shutdownpublic final java.util.concurrent.Future<java.lang.Integer> read(java.nio.ByteBuffer dst)
AsynchronousByteChannelAn invocation of this method of the form c.read(dst) behaves in exactly the same manner as the invocation
c.read(dst, null, null);
read in interface AsynchronousByteChanneldst - The buffer into which bytes are to be transferred
java.lang.IllegalArgumentException - If the buffer is read-only
ReadPendingException - If the channel does not allow more than one read to be outstanding
and a previous read has not completed
NotYetConnectedException - If this channel is not yet connected
public abstract <A> java.util.concurrent.Future<java.lang.Long> read(java.nio.ByteBuffer[] dsts,
int offset,
int length,
long timeout,
java.util.concurrent.TimeUnit unit,
A attachment,
CompletionHandler<java.lang.Long,? super A> handler)
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()
at the moment that the read is attempted.
Suppose that a byte sequence of length n is read, where 0 < n <= r. Up to the first dsts[offset].remaining() bytes of this sequence are transferred into buffer dsts[offset], up to the next dsts[offset+1].remaining() bytes are transferred into buffer dsts[offset+1], and so forth, until the entire byte sequence is transferred into the given buffers. As many bytes as possible are transferred into each buffer, hence the final position of each updated buffer, except the last updated buffer, is guaranteed to be equal to that buffer's limit. The underlying operating system may impose a limit on the number of buffers that may be used in an I/O operation. Where the number of buffers (with bytes remaining), exceeds this limit, then the I/O operation is performed with the maximum number of buffers allowed by the operating system.
The return value from this method is a Future representing
the pending result of the operation. The Future's get method returns the number of bytes read or -1L
if all bytes have been read and the channel has reached end-of-stream.
If a timeout is specified and the timeout elapses before the operation
completes then it completes with the exception InterruptedByTimeoutException. Where a timeout occurs, and the
implementation cannot guarantee that bytes have not been read, or will not
be read from the channel into the given buffers, then further attempts to
read from the channel will cause an unspecific runtime exception to be
thrown.
dsts - The buffers into which bytes are to be transferredoffset - 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
dsts.lengthlength - The maximum number of buffers to be accessed; must be non-negative
and no larger than dsts.length - offsettimeout - The timeout, or 0L for no timeoutunit - The time unit of the timeout argumentattachment - The object to attach to the I/O operation; can be nullhandler - The handler for consuming the result; can be null
Future object representing the pending result
java.lang.IndexOutOfBoundsException - If the pre-conditions for the offset and length
parameter aren't met
java.lang.IllegalArgumentException - If the timeout parameter is negative, or a buffer is
read-only
ReadPendingException - If a read operation is already in progress on this channel
NotYetConnectedException - If this channel is not yet connected
ShutdownChannelGroupException - If a handler is specified, and the channel group is shutdown
public abstract <A> java.util.concurrent.Future<java.lang.Integer> write(java.nio.ByteBuffer src,
long timeout,
java.util.concurrent.TimeUnit unit,
A attachment,
CompletionHandler<java.lang.Integer,? super A> handler)
This method initiates the writing of a sequence of bytes to this channel
from the given buffer, returning a Future representing the
pending result of the operation. The Future's get method will return the number of bytes written.
If a timeout is specified and the timeout elapses before the operation
completes then it completes with the exception InterruptedByTimeoutException. Where a timeout occurs, and the
implementation cannot guarantee that bytes have not been written, or will
not be written to the channel from the given buffer, then further attempts
to write to the channel will cause an unspecific runtime exception to be
thrown.
Otherwise this method works in the same manner as the AsynchronousByteChannel.write(ByteBuffer,Object,CompletionHandler)
method.
src - The buffer from which bytes are to be retrievedtimeout - The timeout, or 0L for no timeoutunit - The time unit of the timeout argumentattachment - The object to attach to the I/O operation; can be nullhandler - The handler for consuming the result; can be null
Future object representing the pending result
java.lang.IllegalArgumentException - If the timeout parameter is negative
WritePendingException - If a write operation is already in progress on this channel
NotYetConnectedException - If this channel is not yet connected
ShutdownChannelGroupException - If a handler is specified, and the channel group is shutdown
public final <A> java.util.concurrent.Future<java.lang.Integer> write(java.nio.ByteBuffer src,
A attachment,
CompletionHandler<java.lang.Integer,? super A> handler)
AsynchronousByteChannel This method initiates an operation to write a sequence of bytes to
this channel from the given buffer. This method returns a Future representing the pending result of the operation. The result
of the operation, obtained by invoking the Future's get method, is the number of bytes written, possibly zero.
This method initiates a write operation to write up to r bytes
to the channel, where r is the number of bytes remaining in the
buffer, that is, src.remaining() at the moment the write is
attempted. Where r is 0, the write operation completes immediately
with a result of 0 without initiating an I/O operation.
Suppose that a byte sequence of length n is written, where 0 < n <= r. This byte sequence will be transferred from the buffer starting at index p, where p is the buffer's position at the moment the write is performed; the index of the last byte written will be p + n - 1. Upon completion the buffer's position will be equal to p + n; its limit will not have changed.
Buffers are not safe for use by multiple concurrent threads so care should be taken to not to access the buffer until the operaton 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 WritePendingException will be thrown.
The handler parameter is used to specify a CompletionHandler. When the write operation completes the handler's
completed method is executed.
write in interface AsynchronousByteChannelsrc - The buffer from which bytes are to be retrievedattachment - The object to attach to the I/O operation; can be nullhandler - The completion handler object; can be null
WritePendingException - If the channel does not allow more than one write to be outstanding
and a previous write has not completed
NotYetConnectedException - If this channel is not yet connected
ShutdownChannelGroupException - If a handler is specified, and the channel group is shutdownpublic final java.util.concurrent.Future<java.lang.Integer> write(java.nio.ByteBuffer src)
AsynchronousByteChannelAn invocation of this method of the form c.write(src) behaves in exactly the same manner as the invocation
c.write(src, null, null);
write in interface AsynchronousByteChannelsrc - The buffer from which bytes are to be retrieved
WritePendingException - If the channel does not allow more than one write to be outstanding
and a previous write has not completed
NotYetConnectedException - If this channel is not yet connected
public abstract <A> java.util.concurrent.Future<java.lang.Long> write(java.nio.ByteBuffer[] srcs,
int offset,
int length,
long timeout,
java.util.concurrent.TimeUnit unit,
A attachment,
CompletionHandler<java.lang.Long,? super A> handler)
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()
at the moment that the write is attempted.
Suppose that a byte sequence of length n is written, where 0 < n <= r. Up to the first srcs[offset].remaining() bytes of this sequence are written from buffer srcs[offset], up to the next srcs[offset+1].remaining() bytes are written from buffer srcs[offset+1], and so forth, until the entire byte sequence is written. As many bytes as possible are written from each buffer, hence the final position of each updated buffer, except the last updated buffer, is guaranteed to be equal to that buffer's limit. The underlying operating system may impose a limit on the number of buffers that may be used in an I/O operation. Where the number of buffers (with bytes remaining), exceeds this limit, then the I/O operation is performed with the maximum number of buffers allowed by the operating system.
The return value from this method is a Future representing
the pending result of the operation. The Future's get method will return the number of bytes written.
If a timeout is specified and the timeout elapses before the operation
completes then it completes with the exception InterruptedByTimeoutException. Where a timeout occurs, and the
implementation cannot guarantee that bytes have not been written, or will
not be written to the channel from the given buffers, then further attempts
to write to the channel will cause an unspecific runtime exception to be
thrown.
srcs - The buffers from which bytes are to be retrievedoffset - 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 srcs.lengthlength - The maximum number of buffers to be accessed; must be non-negative
and no larger than srcs.length - offsettimeout - The timeout, or 0L for no timeoutunit - The time unit of the timeout argumentattachment - The object to attach to the I/O operation; can be nullhandler - The handler for consuming the result; can be null
Future object representing the pending result
java.lang.IndexOutOfBoundsException - If the pre-conditions for the offset and length
parameter aren't met
java.lang.IllegalArgumentException - If the timeout parameter is negative
WritePendingException - If a write operation is already in progress on this channel
NotYetConnectedException - If this channel is not yet connected
ShutdownChannelGroupException - If a handler is specified, and the channel group is shutdown
|
NIO2 API | |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
Copyright 2010 Sun Microsystems, Inc. All rights reserved. Use is subject to the terms of the GNU General Public License.