java.nio.channels
Class SocketChannel

java.lang.Object sample code for java.lang.Object definition code for java.lang.Object 
  extended by java.nio.channels.spi.AbstractInterruptibleChannel sample code for java.nio.channels.spi.AbstractInterruptibleChannel definition code for java.nio.channels.spi.AbstractInterruptibleChannel 
      extended by java.nio.channels.SelectableChannel sample code for java.nio.channels.SelectableChannel definition code for java.nio.channels.SelectableChannel 
          extended by java.nio.channels.spi.AbstractSelectableChannel sample code for java.nio.channels.spi.AbstractSelectableChannel definition code for java.nio.channels.spi.AbstractSelectableChannel 
              extended by java.nio.channels.SocketChannel
All Implemented Interfaces:
Closeable sample code for java.io.Closeable definition code for java.io.Closeable , ByteChannel sample code for java.nio.channels.ByteChannel definition code for java.nio.channels.ByteChannel , Channel sample code for java.nio.channels.Channel definition code for java.nio.channels.Channel , GatheringByteChannel sample code for java.nio.channels.GatheringByteChannel definition code for java.nio.channels.GatheringByteChannel , InterruptibleChannel sample code for java.nio.channels.InterruptibleChannel definition code for java.nio.channels.InterruptibleChannel , ReadableByteChannel sample code for java.nio.channels.ReadableByteChannel definition code for java.nio.channels.ReadableByteChannel , ScatteringByteChannel sample code for java.nio.channels.ScatteringByteChannel definition code for java.nio.channels.ScatteringByteChannel , WritableByteChannel sample code for java.nio.channels.WritableByteChannel definition code for java.nio.channels.WritableByteChannel

public abstract class SocketChannel
extends AbstractSelectableChannel sample code for java.nio.channels.spi.AbstractSelectableChannel definition code for java.nio.channels.spi.AbstractSelectableChannel
implements ByteChannel sample code for java.nio.channels.ByteChannel definition code for java.nio.channels.ByteChannel , ScatteringByteChannel sample code for java.nio.channels.ScatteringByteChannel definition code for java.nio.channels.ScatteringByteChannel , GatheringByteChannel sample code for java.nio.channels.GatheringByteChannel definition code for java.nio.channels.GatheringByteChannel

A selectable channel for stream-oriented connecting sockets.

Socket channels are not a complete abstraction of connecting network sockets. Binding, shutdown, and the manipulation of socket options must be done through an associated Socket sample code for java.net.Socket definition code for java.net.Socket object obtained by invoking the socket sample code for java.nio.channels.SocketChannel.socket() definition code for java.nio.channels.SocketChannel.socket() method. It is not possible to create a channel for an arbitrary, pre-existing socket, nor is it possible to specify the SocketImpl sample code for java.net.SocketImpl definition code for java.net.SocketImpl object to be used by a socket associated with a socket channel.

A socket channel is created by invoking one of the open sample code for java.nio.channels.SocketChannel.open() definition code for java.nio.channels.SocketChannel.open() methods of this class. A newly-created socket channel is open but not yet connected. An attempt to invoke an I/O operation upon an unconnected channel will cause a NotYetConnectedException sample code for java.nio.channels.NotYetConnectedException definition code for java.nio.channels.NotYetConnectedException to be thrown. A socket channel can be connected by invoking its connect sample code for java.nio.channels.SocketChannel.connect(java.net.SocketAddress) definition code for java.nio.channels.SocketChannel.connect(java.net.SocketAddress) method; once connected, a socket channel remains connected until it is closed. Whether or not a socket channel is connected may be determined by invoking its isConnected sample code for java.nio.channels.SocketChannel.isConnected() definition code for java.nio.channels.SocketChannel.isConnected() method.

Socket channels support non-blocking connection: A socket channel may be created and the process of establishing the link to the remote socket may be initiated via the connect sample code for java.nio.channels.SocketChannel.connect(java.net.SocketAddress) definition code for java.nio.channels.SocketChannel.connect(java.net.SocketAddress) method for later completion by the finishConnect sample code for java.nio.channels.SocketChannel.finishConnect() definition code for java.nio.channels.SocketChannel.finishConnect() method. Whether or not a connection operation is in progress may be determined by invoking the isConnectionPending sample code for java.nio.channels.SocketChannel.isConnectionPending() definition code for java.nio.channels.SocketChannel.isConnectionPending() method.

The input and output sides of a socket channel may independently be shut down without actually closing the channel. Shutting down the input side of a channel by invoking the shutdownInput sample code for java.net.Socket.shutdownInput() definition code for java.net.Socket.shutdownInput() method of an associated socket object will cause further reads on the channel to return -1, the end-of-stream indication. Shutting down the output side of the channel by invoking the shutdownOutput sample code for java.net.Socket.shutdownOutput() definition code for java.net.Socket.shutdownOutput() method of an associated socket object will cause further writes on the channel to throw a ClosedChannelException sample code for java.nio.channels.ClosedChannelException definition code for java.nio.channels.ClosedChannelException .

Socket channels support asynchronous shutdown, which is similar to the asynchronous close operation specified in the Channel sample code for java.nio.channels.Channel definition code for java.nio.channels.Channel class. If the input side of a socket is shut down by one thread while another thread is blocked in a read operation on the socket's channel, then the read operation in the blocked thread will complete without reading any bytes and will return -1. If the output side of a socket is shut down by one thread while another thread is blocked in a write operation on the socket's channel, then the blocked thread will receive an AsynchronousCloseException sample code for java.nio.channels.AsynchronousCloseException definition code for java.nio.channels.AsynchronousCloseException .

Socket channels are safe for use by multiple concurrent threads. They support concurrent reading and writing, though at most one thread may be reading and at most one thread may be writing at any given time. The connect sample code for java.nio.channels.SocketChannel.connect(java.net.SocketAddress) definition code for java.nio.channels.SocketChannel.connect(java.net.SocketAddress) and finishConnect sample code for java.nio.channels.SocketChannel.finishConnect() definition code for java.nio.channels.SocketChannel.finishConnect() methods are mutually synchronized against each other, and an attempt to initiate a read or write operation while an invocation of one of these methods is in progress will block until that invocation is complete.

Since:
1.4

Constructor Summary
protected SocketChannel sample code for java.nio.channels.SocketChannel.SocketChannel(java.nio.channels.spi.SelectorProvider) definition code for java.nio.channels.SocketChannel.SocketChannel(java.nio.channels.spi.SelectorProvider) (SelectorProvider sample code for java.nio.channels.spi.SelectorProvider definition code for java.nio.channels.spi.SelectorProvider  provider)
          Initializes a new instance of this class.
 
Method Summary
abstract  boolean connect sample code for java.nio.channels.SocketChannel.connect(java.net.SocketAddress) definition code for java.nio.channels.SocketChannel.connect(java.net.SocketAddress) (SocketAddress sample code for java.net.SocketAddress definition code for java.net.SocketAddress  remote)
          Connects this channel's socket.
abstract  boolean finishConnect sample code for java.nio.channels.SocketChannel.finishConnect() definition code for java.nio.channels.SocketChannel.finishConnect() ()
          Finishes the process of connecting a socket channel.
abstract  boolean isConnected sample code for java.nio.channels.SocketChannel.isConnected() definition code for java.nio.channels.SocketChannel.isConnected() ()
          Tells whether or not this channel's network socket is connected.
abstract  boolean isConnectionPending sample code for java.nio.channels.SocketChannel.isConnectionPending() definition code for java.nio.channels.SocketChannel.isConnectionPending() ()
          Tells whether or not a connection operation is in progress on this channel.
static SocketChannel sample code for java.nio.channels.SocketChannel definition code for java.nio.channels.SocketChannel open sample code for java.nio.channels.SocketChannel.open() definition code for java.nio.channels.SocketChannel.open() ()
          Opens a socket channel.
static SocketChannel sample code for java.nio.channels.SocketChannel definition code for java.nio.channels.SocketChannel open sample code for java.nio.channels.SocketChannel.open(java.net.SocketAddress) definition code for java.nio.channels.SocketChannel.open(java.net.SocketAddress) (SocketAddress sample code for java.net.SocketAddress definition code for java.net.SocketAddress  remote)
          Opens a socket channel and connects it to a remote address.
abstract  int read sample code for java.nio.channels.SocketChannel.read(java.nio.ByteBuffer) definition code for java.nio.channels.SocketChannel.read(java.nio.ByteBuffer) (ByteBuffer sample code for java.nio.ByteBuffer definition code for java.nio.ByteBuffer  dst)
          Reads a sequence of bytes from this channel into the given buffer.
 long read sample code for java.nio.channels.SocketChannel.read(java.nio.ByteBuffer[]) definition code for java.nio.channels.SocketChannel.read(java.nio.ByteBuffer[]) (ByteBuffer sample code for java.nio.ByteBuffer definition code for java.nio.ByteBuffer [] dsts)
          Reads a sequence of bytes from this channel into the given buffers.
abstract  long read sample code for java.nio.channels.SocketChannel.read(java.nio.ByteBuffer[], int, int) definition code for java.nio.channels.SocketChannel.read(java.nio.ByteBuffer[], int, int) (ByteBuffer sample code for java.nio.ByteBuffer definition code for java.nio.ByteBuffer [] dsts, int offset, int length)
          Reads a sequence of bytes from this channel into a subsequence of the given buffers.
abstract  Socket sample code for java.net.Socket definition code for java.net.Socket socket sample code for java.nio.channels.SocketChannel.socket() definition code for java.nio.channels.SocketChannel.socket() ()
          Retrieves a socket associated with this channel.
 int validOps sample code for java.nio.channels.SocketChannel.validOps() definition code for java.nio.channels.SocketChannel.validOps() ()
          Returns an operation set identifying this channel's supported operations.
abstract  int write sample code for java.nio.channels.SocketChannel.write(java.nio.ByteBuffer) definition code for java.nio.channels.SocketChannel.write(java.nio.ByteBuffer) (ByteBuffer sample code for java.nio.ByteBuffer definition code for java.nio.ByteBuffer  src)
          Writes a sequence of bytes to this channel from the given buffer.
 long write sample code for java.nio.channels.SocketChannel.write(java.nio.ByteBuffer[]) definition code for java.nio.channels.SocketChannel.write(java.nio.ByteBuffer[]) (ByteBuffer sample code for java.nio.ByteBuffer definition code for java.nio.ByteBuffer [] srcs)
          Writes a sequence of bytes to this channel from the given buffers.
abstract  long write sample code for java.nio.channels.SocketChannel.write(java.nio.ByteBuffer[], int, int) definition code for java.nio.channels.SocketChannel.write(java.nio.ByteBuffer[], int, int) (ByteBuffer sample code for java.nio.ByteBuffer definition code for java.nio.ByteBuffer [] srcs, int offset, int length)
          Writes a sequence of bytes to this channel from a subsequence of the given buffers.
 
Methods inherited from class java.nio.channels.spi.AbstractSelectableChannel sample code for java.nio.channels.spi.AbstractSelectableChannel definition code for java.nio.channels.spi.AbstractSelectableChannel
blockingLock sample code for java.nio.channels.spi.AbstractSelectableChannel.blockingLock() definition code for java.nio.channels.spi.AbstractSelectableChannel.blockingLock() , configureBlocking sample code for java.nio.channels.spi.AbstractSelectableChannel.configureBlocking(boolean) definition code for java.nio.channels.spi.AbstractSelectableChannel.configureBlocking(boolean) , implCloseChannel sample code for java.nio.channels.spi.AbstractSelectableChannel.implCloseChannel() definition code for java.nio.channels.spi.AbstractSelectableChannel.implCloseChannel() , implCloseSelectableChannel sample code for java.nio.channels.spi.AbstractSelectableChannel.implCloseSelectableChannel() definition code for java.nio.channels.spi.AbstractSelectableChannel.implCloseSelectableChannel() , implConfigureBlocking sample code for java.nio.channels.spi.AbstractSelectableChannel.implConfigureBlocking(boolean) definition code for java.nio.channels.spi.AbstractSelectableChannel.implConfigureBlocking(boolean) , isBlocking sample code for java.nio.channels.spi.AbstractSelectableChannel.isBlocking() definition code for java.nio.channels.spi.AbstractSelectableChannel.isBlocking() , isRegistered sample code for java.nio.channels.spi.AbstractSelectableChannel.isRegistered() definition code for java.nio.channels.spi.AbstractSelectableChannel.isRegistered() , keyFor sample code for java.nio.channels.spi.AbstractSelectableChannel.keyFor(java.nio.channels.Selector) definition code for java.nio.channels.spi.AbstractSelectableChannel.keyFor(java.nio.channels.Selector) , provider sample code for java.nio.channels.spi.AbstractSelectableChannel.provider() definition code for java.nio.channels.spi.AbstractSelectableChannel.provider() , register sample code for java.nio.channels.spi.AbstractSelectableChannel.register(java.nio.channels.Selector, int, java.lang.Object) definition code for java.nio.channels.spi.AbstractSelectableChannel.register(java.nio.channels.Selector, int, java.lang.Object)
 
Methods inherited from class java.nio.channels.SelectableChannel sample code for java.nio.channels.SelectableChannel definition code for java.nio.channels.SelectableChannel
register sample code for java.nio.channels.SelectableChannel.register(java.nio.channels.Selector, int) definition code for java.nio.channels.SelectableChannel.register(java.nio.channels.Selector, int)
 
Methods inherited from class java.nio.channels.spi.AbstractInterruptibleChannel sample code for java.nio.channels.spi.AbstractInterruptibleChannel definition code for java.nio.channels.spi.AbstractInterruptibleChannel
begin sample code for java.nio.channels.spi.AbstractInterruptibleChannel.begin() definition code for java.nio.channels.spi.AbstractInterruptibleChannel.begin() , close sample code for java.nio.channels.spi.AbstractInterruptibleChannel.close() definition code for java.nio.channels.spi.AbstractInterruptibleChannel.close() , end sample code for java.nio.channels.spi.AbstractInterruptibleChannel.end(boolean) definition code for java.nio.channels.spi.AbstractInterruptibleChannel.end(boolean) , isOpen sample code for java.nio.channels.spi.AbstractInterruptibleChannel.isOpen() definition code for java.nio.channels.spi.AbstractInterruptibleChannel.isOpen()
 
Methods inherited from class java.lang.Object sample code for java.lang.Object definition code for java.lang.Object
clone sample code for java.lang.Object.clone() definition code for java.lang.Object.clone() , equals sample code for java.lang.Object.equals(java.lang.Object) definition code for java.lang.Object.equals(java.lang.Object) , finalize sample code for java.lang.Object.finalize() definition code for java.lang.Object.finalize() , getClass sample code for java.lang.Object.getClass() definition code for java.lang.Object.getClass() , hashCode sample code for java.lang.Object.hashCode() definition code for java.lang.Object.hashCode() , notify sample code for java.lang.Object.notify() definition code for java.lang.Object.notify() , notifyAll sample code for java.lang.Object.notifyAll() definition code for java.lang.Object.notifyAll() , toString sample code for java.lang.Object.toString() definition code for java.lang.Object.toString() , wait sample code for java.lang.Object.wait() definition code for java.lang.Object.wait() , wait sample code for java.lang.Object.wait(long) definition code for java.lang.Object.wait(long) , wait sample code for java.lang.Object.wait(long, int) definition code for java.lang.Object.wait(long, int)
 
Methods inherited from interface java.nio.channels.Channel sample code for java.nio.channels.Channel definition code for java.nio.channels.Channel
close sample code for java.nio.channels.Channel.close() definition code for java.nio.channels.Channel.close() , isOpen sample code for java.nio.channels.Channel.isOpen() definition code for java.nio.channels.Channel.isOpen()
 
Methods inherited from interface java.nio.channels.Channel sample code for java.nio.channels.Channel definition code for java.nio.channels.Channel
close sample code for java.nio.channels.Channel.close() definition code for java.nio.channels.Channel.close() , isOpen sample code for java.nio.channels.Channel.isOpen() definition code for java.nio.channels.Channel.isOpen()
 
Methods inherited from interface java.nio.channels.Channel sample code for java.nio.channels.Channel definition code for java.nio.channels.Channel
close sample code for java.nio.channels.Channel.close() definition code for java.nio.channels.Channel.close() , isOpen sample code for java.nio.channels.Channel.isOpen() definition code for java.nio.channels.Channel.isOpen()
 
Methods inherited from interface java.nio.channels.Channel sample code for java.nio.channels.Channel definition code for java.nio.channels.Channel
close sample code for java.nio.channels.Channel.close() definition code for java.nio.channels.Channel.close() , isOpen sample code for java.nio.channels.Channel.isOpen() definition code for java.nio.channels.Channel.isOpen()
 

Constructor Detail

SocketChannel sample code for java.nio.channels.SocketChannel(java.nio.channels.spi.SelectorProvider) definition code for java.nio.channels.SocketChannel(java.nio.channels.spi.SelectorProvider)

protected SocketChannel(SelectorProvider sample code for java.nio.channels.spi.SelectorProvider definition code for java.nio.channels.spi.SelectorProvider  provider)
Initializes a new instance of this class.

Method Detail

open sample code for java.nio.channels.SocketChannel.open() definition code for java.nio.channels.SocketChannel.open()

public static SocketChannel sample code for java.nio.channels.SocketChannel definition code for java.nio.channels.SocketChannel  open()
                          throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Opens a socket channel.

The new channel is created by invoking the openSocketChannel sample code for java.nio.channels.spi.SelectorProvider.openSocketChannel() definition code for java.nio.channels.spi.SelectorProvider.openSocketChannel() method of the system-wide default SelectorProvider sample code for java.nio.channels.spi.SelectorProvider definition code for java.nio.channels.spi.SelectorProvider object.

Returns:
A new socket channel
Throws:
IOException sample code for java.io.IOException definition code for java.io.IOException - If an I/O error occurs

open sample code for java.nio.channels.SocketChannel.open(java.net.SocketAddress) definition code for java.nio.channels.SocketChannel.open(java.net.SocketAddress)

public static SocketChannel sample code for java.nio.channels.SocketChannel definition code for java.nio.channels.SocketChannel  open(SocketAddress sample code for java.net.SocketAddress definition code for java.net.SocketAddress  remote)
                          throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Opens a socket channel and connects it to a remote address.

This convenience method works as if by invoking the open() sample code for java.nio.channels.SocketChannel.open() definition code for java.nio.channels.SocketChannel.open() method, invoking the connect sample code for java.nio.channels.SocketChannel.connect(java.net.SocketAddress) definition code for java.nio.channels.SocketChannel.connect(java.net.SocketAddress) method upon the resulting socket channel, passing it remote, and then returning that channel.

Parameters:
remote - The remote address to which the new channel is to be connected
Throws:
AsynchronousCloseException sample code for java.nio.channels.AsynchronousCloseException definition code for java.nio.channels.AsynchronousCloseException - If another thread closes this channel while the connect operation is in progress
ClosedByInterruptException sample code for java.nio.channels.ClosedByInterruptException definition code for java.nio.channels.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
UnresolvedAddressException sample code for java.nio.channels.UnresolvedAddressException definition code for java.nio.channels.UnresolvedAddressException - If the given remote address is not fully resolved
UnsupportedAddressTypeException sample code for java.nio.channels.UnsupportedAddressTypeException definition code for java.nio.channels.UnsupportedAddressTypeException - If the type of the given remote address is not supported
SecurityException sample code for java.lang.SecurityException definition code for java.lang.SecurityException - If a security manager has been installed and it does not permit access to the given remote endpoint
IOException sample code for java.io.IOException definition code for java.io.IOException - If some other I/O error occurs

validOps sample code for java.nio.channels.SocketChannel.validOps() definition code for java.nio.channels.SocketChannel.validOps()

public final int validOps()
Returns an operation set identifying this channel's supported operations.

Socket channels support connecting, reading, and writing, so this method returns (SelectionKey.OP_CONNECT sample code for java.nio.channels.SelectionKey.OP_CONNECT definition code for java.nio.channels.SelectionKey.OP_CONNECT | SelectionKey.OP_READ sample code for java.nio.channels.SelectionKey.OP_READ definition code for java.nio.channels.SelectionKey.OP_READ | SelectionKey.OP_WRITE sample code for java.nio.channels.SelectionKey.OP_WRITE definition code for java.nio.channels.SelectionKey.OP_WRITE ).

Specified by:
validOps sample code for java.nio.channels.SelectableChannel.validOps() definition code for java.nio.channels.SelectableChannel.validOps() in class SelectableChannel sample code for java.nio.channels.SelectableChannel definition code for java.nio.channels.SelectableChannel
Returns:
The valid-operation set

socket sample code for java.nio.channels.SocketChannel.socket() definition code for java.nio.channels.SocketChannel.socket()

public abstract Socket sample code for java.net.Socket definition code for java.net.Socket  socket()
Retrieves a socket associated with this channel.

The returned object will not declare any public methods that are not declared in the Socket sample code for java.net.Socket definition code for java.net.Socket class.

Returns:
A socket associated with this channel

isConnected sample code for java.nio.channels.SocketChannel.isConnected() definition code for java.nio.channels.SocketChannel.isConnected()

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

Returns:
true if, and only if, this channel's network socket is connected

isConnectionPending sample code for java.nio.channels.SocketChannel.isConnectionPending() definition code for java.nio.channels.SocketChannel.isConnectionPending()

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

Returns:
true if, and only if, a connection operation has been initiated on this channel but not yet completed by invoking the finishConnect sample code for java.nio.channels.SocketChannel.finishConnect() definition code for java.nio.channels.SocketChannel.finishConnect() method

connect sample code for java.nio.channels.SocketChannel.connect(java.net.SocketAddress) definition code for java.nio.channels.SocketChannel.connect(java.net.SocketAddress)

public abstract boolean connect(SocketAddress sample code for java.net.SocketAddress definition code for java.net.SocketAddress  remote)
                         throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Connects this channel's socket.

If this channel is in non-blocking mode then an invocation of this method initiates a non-blocking connection operation. If the connection is established immediately, as can happen with a local connection, then this method returns true. Otherwise this method returns false and the connection operation must later be completed by invoking the finishConnect sample code for java.nio.channels.SocketChannel.finishConnect() definition code for java.nio.channels.SocketChannel.finishConnect() method.

If this channel is in blocking mode then an invocation of this method will block until the connection is established or an I/O error occurs.

This method performs exactly the same security checks as the Socket sample code for java.net.Socket definition code for java.net.Socket class. That is, if a security manager has been installed then this method verifies that its checkConnect sample code for java.lang.SecurityManager.checkConnect(java.lang.String, int) definition code for java.lang.SecurityManager.checkConnect(java.lang.String, int) method permits connecting to the address and port number of the given remote endpoint.

This method may be invoked at any time. If a read or write operation upon this channel is invoked while an invocation of this method is in progress then that operation will first block until this invocation is complete. If a connection attempt is initiated but fails, that is, if an invocation of this method throws a checked exception, then the channel will be closed.

Parameters:
remote - The remote address to which this channel is to be connected
Returns:
true if a connection was established, false if this channel is in non-blocking mode and the connection operation is in progress
Throws:
AlreadyConnectedException sample code for java.nio.channels.AlreadyConnectedException definition code for java.nio.channels.AlreadyConnectedException - If this channel is already connected
ConnectionPendingException sample code for java.nio.channels.ConnectionPendingException definition code for java.nio.channels.ConnectionPendingException - If a non-blocking connection operation is already in progress on this channel
ClosedChannelException sample code for java.nio.channels.ClosedChannelException definition code for java.nio.channels.ClosedChannelException - If this channel is closed
AsynchronousCloseException sample code for java.nio.channels.AsynchronousCloseException definition code for java.nio.channels.AsynchronousCloseException - If another thread closes this channel while the connect operation is in progress
ClosedByInterruptException sample code for java.nio.channels.ClosedByInterruptException definition code for java.nio.channels.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
UnresolvedAddressException sample code for java.nio.channels.UnresolvedAddressException definition code for java.nio.channels.UnresolvedAddressException - If the given remote address is not fully resolved
UnsupportedAddressTypeException sample code for java.nio.channels.UnsupportedAddressTypeException definition code for java.nio.channels.UnsupportedAddressTypeException - If the type of the given remote address is not supported
SecurityException sample code for java.lang.SecurityException definition code for java.lang.SecurityException - If a security manager has been installed and it does not permit access to the given remote endpoint
IOException sample code for java.io.IOException definition code for java.io.IOException - If some other I/O error occurs

finishConnect sample code for java.nio.channels.SocketChannel.finishConnect() definition code for java.nio.channels.SocketChannel.finishConnect()

public abstract boolean finishConnect()
                               throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Finishes the process of connecting a socket channel.

A non-blocking connection operation is initiated by placing a socket channel in non-blocking mode and then invoking its connect sample code for java.nio.channels.SocketChannel.connect(java.net.SocketAddress) definition code for java.nio.channels.SocketChannel.connect(java.net.SocketAddress) method. Once the connection is established, or the attempt has failed, the socket channel will become connectable and this method may be invoked to complete the connection sequence. If the connection operation failed then invoking this method will cause an appropriate IOException sample code for java.io.IOException definition code for java.io.IOException to be thrown.

If this channel is already connected then this method will not block and will immediately return true. If this channel is in non-blocking mode then this method will return false if the connection process is not yet complete. If this channel is in blocking mode then this method will block until the connection either completes or fails, and will always either return true or throw a checked exception describing the failure.

This method may be invoked at any time. If a read or write operation upon this channel is invoked while an invocation of this method is in progress then that operation will first block until this invocation is complete. If a connection attempt fails, that is, if an invocation of this method throws a checked exception, then the channel will be closed.

Returns:
true if, and only if, this channel's socket is now connected
Throws:
NoConnectionPendingException sample code for java.nio.channels.NoConnectionPendingException definition code for java.nio.channels.NoConnectionPendingException - If this channel is not connected and a connection operation has not been initiated
ClosedChannelException sample code for java.nio.channels.ClosedChannelException definition code for java.nio.channels.ClosedChannelException - If this channel is closed
AsynchronousCloseException sample code for java.nio.channels.AsynchronousCloseException definition code for java.nio.channels.AsynchronousCloseException - If another thread closes this channel while the connect operation is in progress
ClosedByInterruptException sample code for java.nio.channels.ClosedByInterruptException definition code for java.nio.channels.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 sample code for java.io.IOException definition code for java.io.IOException - If some other I/O error occurs

read sample code for java.nio.channels.SocketChannel.read(java.nio.ByteBuffer) definition code for java.nio.channels.SocketChannel.read(java.nio.ByteBuffer)

public abstract int read(ByteBuffer sample code for java.nio.ByteBuffer definition code for java.nio.ByteBuffer  dst)
                  throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Description copied from interface: ReadableByteChannel sample code for java.nio.channels.ReadableByteChannel.read(java.nio.ByteBuffer) definition code for java.nio.channels.ReadableByteChannel.read(java.nio.ByteBuffer)
Reads a sequence of bytes from this channel into the given buffer.

An attempt is made 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 moment this method is invoked.

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 this method is invoked. Upon return the buffer's position will be equal to p + n; its limit will not have changed.

A read operation might not fill the buffer, and in fact it might not read any bytes at all. Whether or not it does so depends upon the nature and state of the channel. A socket channel in non-blocking mode, for example, cannot read any more bytes than are immediately available from the socket's input buffer; similarly, a file channel cannot read any more bytes than remain in the file. It is guaranteed, however, that if a channel is in blocking mode and there is at least one byte remaining in the buffer then this method will block until at least one byte is read.

This method may be invoked at any time. If another thread has already initiated a read operation upon this channel, however, then an invocation of this method will block until the first operation is complete.

Specified by:
read sample code for java.nio.channels.ReadableByteChannel.read(java.nio.ByteBuffer) definition code for java.nio.channels.ReadableByteChannel.read(java.nio.ByteBuffer) in interface ReadableByteChannel sample code for java.nio.channels.ReadableByteChannel definition code for java.nio.channels.ReadableByteChannel
Parameters:
dst - The buffer into which bytes are to be transferred
Returns:
The number of bytes read, possibly zero, or -1 if the channel has reached end-of-stream
Throws:
NotYetConnectedException sample code for java.nio.channels.NotYetConnectedException definition code for java.nio.channels.NotYetConnectedException - If this channel is not yet connected
ClosedChannelException sample code for java.nio.channels.ClosedChannelException definition code for java.nio.channels.ClosedChannelException - If this channel is closed
AsynchronousCloseException sample code for java.nio.channels.AsynchronousCloseException definition code for java.nio.channels.AsynchronousCloseException - If another thread closes this channel while the read operation is in progress
ClosedByInterruptException sample code for java.nio.channels.ClosedByInterruptException definition code for java.nio.channels.ClosedByInterruptException - If another thread interrupts the current thread while the read operation is in progress, thereby closing the channel and setting the current thread's interrupt status
IOException sample code for java.io.IOException definition code for java.io.IOException - If some other I/O error occurs

read sample code for java.nio.channels.SocketChannel.read(java.nio.ByteBuffer[], int, int) definition code for java.nio.channels.SocketChannel.read(java.nio.ByteBuffer[], int, int)

public abstract long read(ByteBuffer sample code for java.nio.ByteBuffer definition code for java.nio.ByteBuffer [] dsts,
                          int offset,
                          int length)
                   throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Description copied from interface: ScatteringByteChannel sample code for java.nio.channels.ScatteringByteChannel.read(java.nio.ByteBuffer[], int, int) definition code for java.nio.channels.ScatteringByteChannel.read(java.nio.ByteBuffer[], int, int)
Reads a sequence of bytes from this channel into a subsequence of the given buffers.

An invocation of this method attempts to read up to r bytes from this channel, where r is the total number of bytes remaining 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 this method is invoked.

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.

This method may be invoked at any time. If another thread has already initiated a read operation upon this channel, however, then an invocation of this method will block until the first operation is complete.

Specified by:
read sample code for java.nio.channels.ScatteringByteChannel.read(java.nio.ByteBuffer[], int, int) definition code for java.nio.channels.ScatteringByteChannel.read(java.nio.ByteBuffer[], int, int) in interface ScatteringByteChannel sample code for java.nio.channels.ScatteringByteChannel definition code for java.nio.channels.ScatteringByteChannel
Parameters:
dsts - The buffers into which bytes are to be transferred
offset - 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.length
length - The maximum number of buffers to be accessed; must be non-negative and no larger than dsts.length - offset
Returns:
The number of bytes read, possibly zero, or -1 if the channel has reached end-of-stream
Throws:
NotYetConnectedException sample code for java.nio.channels.NotYetConnectedException definition code for java.nio.channels.NotYetConnectedException - If this channel is not yet connected
ClosedChannelException sample code for java.nio.channels.ClosedChannelException definition code for java.nio.channels.ClosedChannelException - If this channel is closed
AsynchronousCloseException sample code for java.nio.channels.AsynchronousCloseException definition code for java.nio.channels.AsynchronousCloseException - If another thread closes this channel while the read operation is in progress
ClosedByInterruptException sample code for java.nio.channels.ClosedByInterruptException definition code for java.nio.channels.ClosedByInterruptException - If another thread interrupts the current thread while the read operation is in progress, thereby closing the channel and setting the current thread's interrupt status
IOException sample code for java.io.IOException definition code for java.io.IOException - If some other I/O error occurs

read sample code for java.nio.channels.SocketChannel.read(java.nio.ByteBuffer[]) definition code for java.nio.channels.SocketChannel.read(java.nio.ByteBuffer[])

public final long read(ByteBuffer sample code for java.nio.ByteBuffer definition code for java.nio.ByteBuffer [] dsts)
                throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Description copied from interface: ScatteringByteChannel sample code for java.nio.channels.ScatteringByteChannel.read(java.nio.ByteBuffer[]) definition code for java.nio.channels.ScatteringByteChannel.read(java.nio.ByteBuffer[])
Reads a sequence of bytes from this channel into the given buffers.

An invocation of this method of the form c.read(dsts) behaves in exactly the same manner as the invocation

 c.read(dsts, 0, srcs.length);

Specified by:
read sample code for java.nio.channels.ScatteringByteChannel.read(java.nio.ByteBuffer[]) definition code for java.nio.channels.ScatteringByteChannel.read(java.nio.ByteBuffer[]) in interface ScatteringByteChannel sample code for java.nio.channels.ScatteringByteChannel definition code for java.nio.channels.ScatteringByteChannel
Parameters:
dsts - The buffers into which bytes are to be transferred
Returns:
The number of bytes read, possibly zero, or -1 if the channel has reached end-of-stream
Throws:
NotYetConnectedException sample code for java.nio.channels.NotYetConnectedException definition code for java.nio.channels.NotYetConnectedException - If this channel is not yet connected
ClosedChannelException sample code for java.nio.channels.ClosedChannelException definition code for java.nio.channels.ClosedChannelException - If this channel is closed
AsynchronousCloseException sample code for java.nio.channels.AsynchronousCloseException definition code for java.nio.channels.AsynchronousCloseException - If another thread closes this channel while the read operation is in progress
ClosedByInterruptException sample code for java.nio.channels.ClosedByInterruptException definition code for java.nio.channels.ClosedByInterruptException - If another thread interrupts the current thread while the read operation is in progress, thereby closing the channel and setting the current thread's interrupt status
IOException sample code for java.io.IOException definition code for java.io.IOException - If some other I/O error occurs

write sample code for java.nio.channels.SocketChannel.write(java.nio.ByteBuffer) definition code for java.nio.channels.SocketChannel.write(java.nio.ByteBuffer)

public abstract int write(ByteBuffer sample code for java.nio.ByteBuffer definition code for java.nio.ByteBuffer  src)
                   throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Description copied from interface: WritableByteChannel sample code for java.nio.channels.WritableByteChannel.write(java.nio.ByteBuffer) definition code for java.nio.channels.WritableByteChannel.write(java.nio.ByteBuffer)
Writes a sequence of bytes to this channel from the given buffer.

An attempt is made to write up to r bytes to the channel, where r is the number of bytes remaining in the buffer, that is, dst.remaining(), at the moment this method is invoked.

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 this method is invoked; the index of the last byte written will be p + n - 1. Upon return the buffer's position will be equal to p + n; its limit will not have changed.

Unless otherwise specified, a write operation will return only after writing all of the r requested bytes. Some types of channels, depending upon their state, may write only some of the bytes or possibly none at all. A socket channel in non-blocking mode, for example, cannot write any more bytes than are free in the socket's output buffer.

This method may be invoked at any time. If another thread has already initiated a write operation upon this channel, however, then an invocation of this method will block until the first operation is complete.

Specified by:
write sample code for java.nio.channels.WritableByteChannel.write(java.nio.ByteBuffer) definition code for java.nio.channels.WritableByteChannel.write(java.nio.ByteBuffer) in interface WritableByteChannel sample code for java.nio.channels.WritableByteChannel definition code for java.nio.channels.WritableByteChannel
Parameters:
src - The buffer from which bytes are to be retrieved
Returns:
The number of bytes written, possibly zero
Throws:
NotYetConnectedException sample code for java.nio.channels.NotYetConnectedException definition code for java.nio.channels.NotYetConnectedException - If this channel is not yet connected
ClosedChannelException sample code for java.nio.channels.ClosedChannelException definition code for java.nio.channels.ClosedChannelException - If this channel is closed
AsynchronousCloseException sample code for java.nio.channels.AsynchronousCloseException definition code for java.nio.channels.AsynchronousCloseException - If another thread closes this channel while the write operation is in progress
ClosedByInterruptException sample code for java.nio.channels.ClosedByInterruptException definition code for java.nio.channels.ClosedByInterruptException - If another thread interrupts the current thread while the write operation is in progress, thereby closing the channel and setting the current thread's interrupt status
IOException sample code for java.io.IOException definition code for java.io.IOException - If some other I/O error occurs

write sample code for java.nio.channels.SocketChannel.write(java.nio.ByteBuffer[], int, int) definition code for java.nio.channels.SocketChannel.write(java.nio.ByteBuffer[], int, int)

public abstract long write(ByteBuffer sample code for java.nio.ByteBuffer definition code for java.nio.ByteBuffer [] srcs,
                           int offset,
                           int length)
                    throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Description copied from interface: GatheringByteChannel sample code for java.nio.channels.GatheringByteChannel.write(java.nio.ByteBuffer[], int, int) definition code for java.nio.channels.GatheringByteChannel.write(java.nio.ByteBuffer[], int, int)
Writes a sequence of bytes to this channel from a subsequence of the given buffers.

An attempt is made to write 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 this method is invoked.

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.

Unless otherwise specified, a write operation will return only after writing all of the r requested bytes. Some types of channels, depending upon their state, may write only some of the bytes or possibly none at all. A socket channel in non-blocking mode, for example, cannot write any more bytes than are free in the socket's output buffer.

This method may be invoked at any time. If another thread has already initiated a write operation upon this channel, however, then an invocation of this method will block until the first operation is complete.

Specified by:
write sample code for java.nio.channels.GatheringByteChannel.write(java.nio.ByteBuffer[], int, int) definition code for java.nio.channels.GatheringByteChannel.write(java.nio.ByteBuffer[], int, int) in interface GatheringByteChannel sample code for java.nio.channels.GatheringByteChannel definition code for java.nio.channels.GatheringByteChannel
Parameters:
srcs - The buffers from which bytes are to be retrieved
offset - 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.length
length - The maximum number of buffers to be accessed; must be non-negative and no larger than srcs.length - offset
Returns:
The number of bytes written, possibly zero
Throws:
NotYetConnectedException sample code for java.nio.channels.NotYetConnectedException definition code for java.nio.channels.NotYetConnectedException - If this channel is not yet connected
ClosedChannelException sample code for java.nio.channels.ClosedChannelException definition code for java.nio.channels.ClosedChannelException - If this channel is closed
AsynchronousCloseException sample code for java.nio.channels.AsynchronousCloseException definition code for java.nio.channels.AsynchronousCloseException - If another thread closes this channel while the write operation is in progress
ClosedByInterruptException sample code for java.nio.channels.ClosedByInterruptException definition code for java.nio.channels.ClosedByInterruptException - If another thread interrupts the current thread while the write operation is in progress, thereby closing the channel and setting the current thread's interrupt status
IOException sample code for java.io.IOException definition code for java.io.IOException - If some other I/O error occurs

write sample code for java.nio.channels.SocketChannel.write(java.nio.ByteBuffer[]) definition code for java.nio.channels.SocketChannel.write(java.nio.ByteBuffer[])

public final long write(ByteBuffer sample code for java.nio.ByteBuffer definition code for java.nio.ByteBuffer [] srcs)
                 throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Description copied from interface: GatheringByteChannel sample code for java.nio.channels.GatheringByteChannel.write(java.nio.ByteBuffer[]) definition code for java.nio.channels.GatheringByteChannel.write(java.nio.ByteBuffer[])
Writes a sequence of bytes to this channel from the given buffers.

An invocation of this method of the form c.write(srcs) behaves in exactly the same manner as the invocation

 c.write(srcs, 0, srcs.length);

Specified by:
write sample code for java.nio.channels.GatheringByteChannel.write(java.nio.ByteBuffer[]) definition code for java.nio.channels.GatheringByteChannel.write(java.nio.ByteBuffer[]) in interface GatheringByteChannel sample code for java.nio.channels.GatheringByteChannel definition code for java.nio.channels.GatheringByteChannel
Parameters:
srcs - The buffers from which bytes are to be retrieved
Returns:
The number of bytes written, possibly zero
Throws:
NotYetConnectedException sample code for java.nio.channels.NotYetConnectedException definition code for java.nio.channels.NotYetConnectedException - If this channel is not yet connected
ClosedChannelException sample code for java.nio.channels.ClosedChannelException definition code for java.nio.channels.ClosedChannelException - If this channel is closed
AsynchronousCloseException sample code for java.nio.channels.AsynchronousCloseException definition code for java.nio.channels.AsynchronousCloseException - If another thread closes this channel while the write operation is in progress
ClosedByInterruptException sample code for java.nio.channels.ClosedByInterruptException definition code for java.nio.channels.ClosedByInterruptException - If another thread interrupts the current thread while the write operation is in progress, thereby closing the channel and setting the current thread's interrupt status
IOException sample code for java.io.IOException definition code for java.io.IOException - If some other I/O error occurs