java.nio.channels

Class SocketChannel

public abstract class SocketChannel extends AbstractSelectableChannel implements ByteChannel, ScatteringByteChannel, GatheringByteChannel

Since: 1.4

Constructor Summary
protected SocketChannel(SelectorProvider provider)
Initializes this socket channel.
Method Summary
abstract booleanconnect(SocketAddress remote)
Connects the channel's socket to the remote address.
abstract booleanfinishConnect()
Finishes the process of connecting a socket channel.
abstract booleanisConnected()
Tells whether or not the channel's socket is connected.
abstract booleanisConnectionPending()
Tells whether or not a connection operation is in progress on this channel.
static SocketChannelopen()
Opens a socket channel.
static SocketChannelopen(SocketAddress remote)
Opens a channel and connects it to a remote address.
longread(ByteBuffer[] dsts)
Reads data from the channel.
abstract intread(ByteBuffer dst)
Reads data from the channel.
abstract longread(ByteBuffer[] dsts, int offset, int length)
Reads data from the channel.
abstract Socketsocket()
Retrieves the channel's socket.
intvalidOps()
Retrieves the valid operations for this channel.
longwrite(ByteBuffer[] dsts)
Writes data to the channel.
abstract intwrite(ByteBuffer src)
Writes data to the channel.
abstract longwrite(ByteBuffer[] srcs, int offset, int length)
Writes data to the channel.

Constructor Detail

SocketChannel

protected SocketChannel(SelectorProvider provider)
Initializes this socket channel.

Method Detail

connect

public abstract boolean connect(SocketAddress remote)
Connects the channel's socket to the remote address.

Returns: true if the channel got successfully connected, false if the channel is in non-blocking mode and connection operation is still in progress.

Throws: AlreadyConnectedException If this channel is already connected. AsynchronousCloseException If this channel is already connected. ClosedByInterruptException If another thread interrupts the current thread while the connect operation is in progress, thereby closing the channel and setting the current thread's interrupt status. ClosedChannelException If this channel is closed. ConnectionPendingException If a non-blocking connection operation is already in progress on this channel. IOException If an error occurs SecurityException If a security manager has been installed and it does not permit access to the given remote endpoint. UnresolvedAddressException If the given remote address is not fully resolved. UnsupportedAddressTypeException If the type of the given remote address is not supported.

finishConnect

public abstract boolean finishConnect()
Finishes the process of connecting a socket channel.

Throws: AsynchronousCloseException If this channel is already connected. ClosedByInterruptException If another thread interrupts the current thread while the connect operation is in progress, thereby closing the channel and setting the current thread's interrupt status. ClosedChannelException If this channel is closed. IOException If an error occurs NoConnectionPendingException If this channel is not connected and a connection operation has not been initiated.

isConnected

public abstract boolean isConnected()
Tells whether or not the channel's socket is connected.

isConnectionPending

public abstract boolean isConnectionPending()
Tells whether or not a connection operation is in progress on this channel.

open

public static SocketChannel open()
Opens a socket channel.

Returns: the new SocketChannel object

Throws: IOException If an error occurs

open

public static SocketChannel open(SocketAddress remote)
Opens a channel and connects it to a remote address.

Returns: the new SocketChannel object

Throws: AsynchronousCloseException If this channel is already connected. ClosedByInterruptException If another thread interrupts the current thread while the connect operation is in progress, thereby closing the channel and setting the current thread's interrupt status. IOException If an error occurs SecurityException If a security manager has been installed and it does not permit access to the given remote endpoint. UnresolvedAddressException If the given remote address is not fully resolved. UnsupportedAddressTypeException If the type of the given remote address is not supported.

read

public final long read(ByteBuffer[] dsts)
Reads data from the channel.

Returns: the number of bytes read, zero is valid too, -1 if end of stream is reached

Throws: IOException If an error occurs NotYetConnectedException If this channel is not yet connected.

read

public abstract int read(ByteBuffer dst)
Reads data from the channel.

Returns: the number of bytes read, zero is valid too, -1 if end of stream is reached

Throws: IOException If an error occurs NotYetConnectedException If this channel is not yet connected.

read

public abstract long read(ByteBuffer[] dsts, int offset, int length)
Reads data from the channel.

Returns: the number of bytes read, zero is valid too, -1 if end of stream is reached

Throws: IOException If an error occurs NotYetConnectedException If this channel is not yet connected.

socket

public abstract Socket socket()
Retrieves the channel's socket.

Returns: the socket

validOps

public final int validOps()
Retrieves the valid operations for this channel.

Returns: the valid operations

write

public final long write(ByteBuffer[] dsts)
Writes data to the channel.

Returns: the number of bytes written, zero is valid too

Throws: IOException If an error occurs NotYetConnectedException If this channel is not yet connected.

write

public abstract int write(ByteBuffer src)
Writes data to the channel.

Returns: the number of bytes written, zero is valid too

Throws: IOException If an error occurs NotYetConnectedException If this channel is not yet connected.

write

public abstract long write(ByteBuffer[] srcs, int offset, int length)
Writes data to the channel.

Returns: the number of bytes written, zero is valid too

Throws: IOException If an error occurs NotYetConnectedException If this channel is not yet connected.