Methods Summary |
---|
public abstract boolean | connect(java.net.SocketAddress address)Connects this channel's socket with a remote address.
If this channel is blocking, this method will suspend until connecting is
finished or an I/O exception occurrs. If the channel is non-blocking,
this method will return {@code true} if the connection is finished at
once or return {@code false} when the connection must be finished later
by calling {@code finishConnect()}.
This method can be called at any moment and can block other read and
write operations while connecting. It executes the same security checks
as the connect method of the {@code Socket} class.
|
public abstract boolean | finishConnect()Completes the connection process initiated by a call of {@code
connect(SocketAddress)}.
This method returns {@code true} if the connection is finished already
and returns {@code false} if the channel is non-blocking and the
connection is not finished yet.
If this channel is in blocking mode, this method will suspend and return
{@code true} when the connection is finished. It closes this channel and
throws an exception if the connection fails.
This method can be called at any moment and it can block other {@code
read} and {@code write} operations while connecting.
|
public abstract boolean | isConnected()Indicates whether this channel's socket is connected.
|
public abstract boolean | isConnectionPending()Indicates whether this channel's socket is still trying to connect.
|
public static java.nio.channels.SocketChannel | open()Creates an open and unconnected socket channel.
This channel is created by calling {@code openSocketChannel()} of the
default {@link SelectorProvider} instance.
return SelectorProvider.provider().openSocketChannel();
|
public static java.nio.channels.SocketChannel | open(java.net.SocketAddress address)Creates a socket channel and connects it to a socket address.
This method performs a call to {@code open()} followed by a call to
{@code connect(SocketAdress)}.
SocketChannel socketChannel = open();
if (null != socketChannel) {
socketChannel.connect(address);
}
return socketChannel;
|
public abstract int | read(java.nio.ByteBuffer target)Reads bytes from this socket channel into the given buffer.
The maximum number of bytes that will be read is the remaining number of
bytes in the buffer when the method is invoked. The bytes will be copied
into the buffer starting at the buffer's current position.
The call may block if other threads are also attempting to read from this
channel.
Upon completion, the buffer's position is set to the end of the bytes
that have been read. The buffer's limit is not changed.
|
public abstract long | read(java.nio.ByteBuffer[] targets, int offset, int length)Reads bytes from this socket channel and stores them in a subset of the
specified array of buffers. The subset is defined by {@code offset} and
{@code length}, indicating the first buffer and the number of buffers to
use. This method attempts to read as many bytes as can be stored in the
buffer subset from this channel and returns the number of bytes actually
read.
If a read operation is in progress, subsequent threads will block until
the read is completed and will then contend for the ability to read.
|
public final synchronized long | read(java.nio.ByteBuffer[] targets)Reads bytes from this socket channel and stores them in the specified
array of buffers. This method attempts to read as many bytes as can be
stored in the buffer array from this channel and returns the number of
bytes actually read.
If a read operation is in progress, subsequent threads will block until
the read is completed and will then contend for the ability to read.
Calling this method is equivalent to calling {@code read(targets, 0,
targets.length);}
return read(targets, 0, targets.length);
|
public abstract java.net.Socket | socket()Returns the socket assigned to this channel, which does not declare any public
methods that are not declared in {@code Socket}.
|
public final int | validOps()Gets the valid operations of this channel. Socket channels support
connect, read and write operation, so this method returns
{@code SelectionKey.OP_CONNECT | SelectionKey.OP_READ | SelectionKey.OP_WRITE}.
return (SelectionKey.OP_CONNECT | SelectionKey.OP_READ | SelectionKey.OP_WRITE);
|
public abstract int | write(java.nio.ByteBuffer source)Writes bytes from the given byte buffer to this socket channel. The
maximum number of bytes that are written is the remaining number of bytes
in the buffer when this method is invoked. The bytes are taken from the
buffer starting at the buffer's position.
The call may block if other threads are also attempting to write to the
same channel.
Upon completion, the buffer's position is updated to the end of the bytes
that have been written. The buffer's limit is not changed.
|
public abstract long | write(java.nio.ByteBuffer[] sources, int offset, int length)Writes bytes from a subset of the specified array of buffers into this
socket channel. The subset is defined by {@code offset} and {@code
length}, indicating the first buffer and the number of buffers to use.
If a write operation is in progress, subsequent threads will block until
the write is completed and then contend for the ability to write.
|
public final synchronized long | write(java.nio.ByteBuffer[] sources)Writes bytes from all the given byte buffers to this socket channel.
Calling this method is equivalent to calling {@code write(sources, 0,
sources.length);}
return write(sources, 0, sources.length);
|