Top Description Constructors Methods
java.nio.channels

public abstract Class SocketChannel

extends AbstractSelectableChannel
implements ByteChannel, ScatteringByteChannel, GatheringByteChannel, NetworkChannel
Class Inheritance
All Implemented Interfaces
java.nio.channels.NetworkChannel, java.nio.channels.Channel, java.io.Closeable, java.lang.AutoCloseable, java.nio.channels.GatheringByteChannel, java.nio.channels.WritableByteChannel, java.nio.channels.ScatteringByteChannel, java.nio.channels.ReadableByteChannel, java.nio.channels.ByteChannel
Known Direct Subclasses
sun.nio.ch.SocketChannelImpl
Imports
java.io.IOException, java.net.InetSocketAddress, .NetPermission, .ProtocolFamily, .StandardProtocolFamily, .Socket, .SocketOption, .SocketAddress, .UnixDomainSocketAddress, java.nio.ByteBuffer, java.nio.channels.spi.AbstractSelectableChannel, .SelectorProvider

A selectable channel for stream-oriented connecting sockets.

A socket channel is created by invoking one of the open methods of this class. The no-arg open method opens a socket channel for an Internet protocol socket. The open(ProtocolFamily) method is used to open a socket channel for a socket of a specified protocol family. It is not possible to create a channel for an arbitrary, pre-existing socket. 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 to be thrown. A socket channel can be connected by invoking its connect 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 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 method for later completion by the finishConnect method. Whether or not a connection operation is in progress may be determined by invoking the isConnectionPending method.

Socket channels support asynchronous shutdown, which is similar to the asynchronous close operation specified in the 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.

Socket options are configured using the setOption method. Socket channels for Internet protocol sockets support following options:

Socket options
Option Name Description
SO_SNDBUF The size of the socket send buffer
SO_RCVBUF The size of the socket receive buffer
SO_KEEPALIVE Keep connection alive
SO_REUSEADDR Re-use address
SO_LINGER Linger on close if data is present (when configured in blocking mode only)
TCP_NODELAY Disable the Nagle algorithm

Socket channels for Unix domain sockets support:

Socket options
Option Name Description
SO_SNDBUF The size of the socket send buffer
SO_RCVBUF The size of the socket receive buffer
SO_LINGER Linger on close if data is present (when configured in blocking mode only)

Additional (implementation specific) options may also be supported.

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 and 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.

Authors
Mark Reinhold, JSR-51 Expert Group
Since
1.4

Constructor Summary

AccessConstructor and Description
protected
SocketChannel(SelectorProvider
The provider that created this channel
provider
)

Initializes a new instance of this class.

Method Summary

Modifier and TypeMethod and Description
public abstract SocketChannel

Returns:

This channel
bind
(SocketAddress
The address to bind the socket, or null to bind the socket to an automatically assigned socket address
local
)

Redeclares java.nio.channels.NetworkChannel.bind.

Binds the channel's socket to a local address.

public abstract boolean

Returns:

true if a connection was established, false if this channel is in non-blocking mode and the connection operation is in progress
connect
(SocketAddress
The remote address to which this channel is to be connected
remote
)

Connects this channel's socket.

public abstract boolean

Returns:

true if, and only if, this channel's socket is now connected
finishConnect
()

Finishes the process of connecting a socket channel.

public abstract SocketAddress

Returns:

The SocketAddress that the socket is bound to, or the SocketAddress representing the loopback address or empty path if denied by the security manager, or null if the channel's socket is not bound
getLocalAddress
()

Redeclares java.nio.channels.NetworkChannel.getLocalAddress.

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 connected
getRemoteAddress
()

Returns the remote address to which this channel's socket is connected.

public abstract boolean

Returns:

true if, and only if, this channel's network socket is open and connected
isConnected
()

Tells whether or not this channel's network socket is connected.

public abstract boolean

Returns:

true if, and only if, a connection operation has been initiated on this channel but not yet completed by invoking the finishConnect method
isConnectionPending
()

Tells whether or not a connection operation is in progress on this channel.

public static SocketChannel

Returns:

A new socket channel
open
()

Opens a socket channel for an Internet protocol socket.

public static SocketChannel

Returns:

A new socket channel
open
(ProtocolFamily
The protocol family
family
)

Opens a socket channel.

public static SocketChannel

Returns:

A new, and connected, socket channel
open
(SocketAddress
The remote address to which the new channel is to be connected
remote
)

Opens a socket channel and connects it to a remote address.

public abstract int
read(ByteBuffer
The buffer into which bytes are to be transferred
dst
)

Redeclares java.nio.channels.ReadableByteChannel.read.

Reads a sequence of bytes from this channel into the given buffer.

public abstract long
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 dsts.length
offset
,
int
The maximum number of buffers to be accessed; must be non-negative and no larger than dsts.length - offset
length
)

Redeclares java.nio.channels.ScatteringByteChannel.read.

Reads a sequence of bytes from this channel into a subsequence of the given buffers.

public final long
read(ByteBuffer[]
The buffers into which bytes are to be transferred
dsts
)

Implements java.nio.channels.ScatteringByteChannel.read.

Reads a sequence of bytes from this channel into the given buffers.

public abstract <T> SocketChannel
setOption(SocketOption<T>
The socket option
name
,
T
The value of the socket option. A value of null may be a valid value for some socket options.
value
)

Redeclares java.nio.channels.NetworkChannel.setOption.

Sets the value of a socket option.

public abstract SocketChannel

Returns:

The channel
shutdownInput
()

Shutdown the connection for reading without closing the channel.

public abstract SocketChannel

Returns:

The channel
shutdownOutput
()

Shutdown the connection for writing without closing the channel.

public abstract Socket

Returns:

A socket associated with this channel
socket
()

Retrieves a socket associated with this channel.

public final int

Returns:

The valid-operation set
validOps
()

Implements abstract java.nio.channels.SelectableChannel.validOps.

Returns an operation set identifying this channel's supported operations.

public abstract int
write(ByteBuffer
The buffer from which bytes are to be retrieved
src
)

Redeclares java.nio.channels.WritableByteChannel.write.

Writes a sequence of bytes to this channel from the given buffer.

public abstract long
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 srcs.length
offset
,
int
The maximum number of buffers to be accessed; must be non-negative and no larger than srcs.length - offset
length
)

Redeclares java.nio.channels.GatheringByteChannel.write.

Writes a sequence of bytes to this channel from a subsequence of the given buffers.

public final long
write(ByteBuffer[]
The buffers from which bytes are to be retrieved
srcs
)

Implements java.nio.channels.GatheringByteChannel.write.

Writes a sequence of bytes to this channel from the given buffers.

Inherited from java.nio.channels.spi.AbstractSelectableChannel:
blockingLockconfigureBlockingimplCloseChannelimplCloseSelectableChannelimplConfigureBlockingisBlockingisRegisteredkeyForproviderregister

Constructor Detail

SocketChannelback to summary
protected SocketChannel(SelectorProvider provider)

Initializes a new instance of this class.

Parameters
provider:SelectorProvider

The provider that created this channel

Method Detail

bindback to summary
public abstract SocketChannel bind(SocketAddress local) throws IOException

Redeclares java.nio.channels.NetworkChannel.bind.

Binds the channel's socket to a local address.

This method is used to establish an association between the socket and a local address. For Internet Protocol sockets, 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.

API Note

Binding a socket channel to a Unix Domain socket creates a file corresponding to the file path in the UnixDomainSocketAddress. This file persists after the channel is closed, and must be removed before another socket can bind to the same name. If a socket channel to a Unix Domain socket is implicitly bound by connecting it without calling bind first, then its socket is unnamed with no corresponding socket file in the file-system. If a socket channel to a Unix Domain socket is automatically bound by calling bind(null) this results in an unnamed socket also.

Implementation Note

Each platform enforces an implementation specific maximum length for the name of a Unix Domain socket. This limitation is enforced when a channel is bound. The maximum length is typically close to and generally not less than 100 bytes.

Parameters
local:SocketAddress

The address to bind the socket, or null to bind the socket to an automatically assigned socket address

Returns:SocketChannel

This channel

Annotations
@Override
Exceptions
IOException:
If some other I/O error occurs
ConnectionPendingException:
If a non-blocking connect 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
SecurityException:
If a security manager has been installed and its checkListen method denies the operation for an Internet protocol socket address, or for a Unix domain socket address if it denies NetPermission("accessUnixDomainSocket").
Since
1.7
connectback to summary
public abstract boolean connect(SocketAddress remote) throws 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 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.

For channels to Internet protocol sockets, this method performs exactly the same security checks as the java.net.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.

For channels to Unix Domain sockets, this method checks NetPermission("accessUnixDomainSocket") with the security manager's checkPermission method.

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:SocketAddress

The remote address to which this channel is to be connected

Returns:boolean

true if a connection was established, false if this channel is in non-blocking mode and the connection operation is in progress

Exceptions
IOException:
If some other I/O error occurs
AlreadyConnectedException:
If this channel is already connected
ConnectionPendingException:
If a non-blocking connection operation is already in progress on this channel
ClosedChannelException:
If this channel is closed
AsynchronousCloseException:
If another thread closes this channel while the connect operation is in progress
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:
If the given remote address is an InetSocketAddress that is not fully resolved
UnsupportedAddressTypeException:
If the type of the given remote address is not supported
SecurityException:
If a security manager has been installed and it does not permit access to the given remote endpoint
finishConnectback to summary
public abstract boolean finishConnect() throws 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 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 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:boolean

true if, and only if, this channel's socket is now connected

Exceptions
IOException:
If some other I/O error occurs
NoConnectionPendingException:
If this channel is not connected and a connection operation has not been initiated
ClosedChannelException:
If this channel is closed
AsynchronousCloseException:
If another thread closes this channel while the connect operation is in progress
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
getLocalAddressback to summary
public abstract SocketAddress getLocalAddress() throws IOException

Redeclares java.nio.channels.NetworkChannel.getLocalAddress.

Doc from java.nio.channels.NetworkChannel.getLocalAddress.

Returns the socket address that this channel's socket is bound to.

Where the channel is bound to an Internet Protocol socket address then the return value from this method is of type java.net.InetSocketAddress. If there is a security manager set, its checkConnect method is called with the local address and -1 as its arguments to see if the operation is allowed. If the operation is not allowed, a SocketAddress representing the loopback address and the local port of the channel's socket is returned.

Where the channel is bound to a Unix Domain socket address, the socket address is a UnixDomainSocketAddress. If there is a security manager set, its checkPermission method is called with NetPermission("accessUnixDomainSocket"). If the operation is not allowed an unnamed UnixDomainSocketAddress is returned.

Returns:SocketAddress

The SocketAddress that the socket is bound to, or the SocketAddress representing the loopback address or empty path if denied by the security manager, or null if the channel's socket is not bound

Annotations
@Override
Exceptions
IOException:
If an I/O error occurs
ClosedChannelException:
If the channel is closed
getRemoteAddressback to summary
public abstract SocketAddress getRemoteAddress() throws IOException

Returns the remote address to which this channel's socket is connected.

Where the channel's socket is bound and connected to an Internet Protocol socket address then the return value is of type java.net.InetSocketAddress.

Where the channel's socket is bound and connected to a Unix Domain socket address, the returned address is a UnixDomainSocketAddress.

Returns:SocketAddress

The remote address; null if the channel's socket is not connected

Exceptions
IOException:
If an I/O error occurs
ClosedChannelException:
If the channel is closed
Since
1.7
isConnectedback to summary
public abstract boolean isConnected()

Tells whether or not this channel's network socket is connected.

Returns:boolean

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

isConnectionPendingback to summary
public abstract boolean isConnectionPending()

Tells whether or not a connection operation is in progress on this channel.

Returns:boolean

true if, and only if, a connection operation has been initiated on this channel but not yet completed by invoking the finishConnect method

openback to summary
public static SocketChannel open() throws IOException

Opens a socket channel for an Internet protocol socket.

The new channel is created by invoking the openSocketChannel method of the system-wide default java.nio.channels.spi.SelectorProvider object.

Returns:SocketChannel

A new socket channel

Exceptions
IOException:
If an I/O error occurs
See Also
java.net.preferIPv4Stack system property
openback to summary
public static SocketChannel open(ProtocolFamily family) throws IOException

Opens a socket channel. The family parameter specifies the protocol family of the channel's socket.

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

Parameters
family:ProtocolFamily

The protocol family

Returns:SocketChannel

A new socket channel

Exceptions
IOException:
If an I/O error occurs
UnsupportedOperationException:
If the specified protocol family is not supported. For example, suppose the parameter is specified as StandardProtocolFamily.INET6 but IPv6 is not enabled on the platform.
Since
15
See Also
java.net.preferIPv4Stack system property
openback to summary
public static SocketChannel open(SocketAddress remote) throws IOException

Opens a socket channel and connects it to a remote address.

If the remote address is an InetSocketAddress then this method works as if by invoking the open() method, invoking the connect method upon the resulting socket channel, passing it remote, and then returning that channel.

If the remote address is a UnixDomainSocketAddress then this works by invoking the open(ProtocolFamily) method with StandardProtocolFamily#UNIX as parameter, invoking the connect method upon the resulting socket channel, passing it remote, then returning that channel.

Parameters
remote:SocketAddress

The remote address to which the new channel is to be connected

Returns:SocketChannel

A new, and connected, socket channel

Exceptions
IOException:
If some other I/O error occurs
AsynchronousCloseException:
If another thread closes this channel while the connect operation is in progress
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:
If the given remote address is an InetSocketAddress that is not fully resolved
UnsupportedAddressTypeException:
If the type of the given remote address is not supported
SecurityException:
If a security manager has been installed and it does not permit access to the given remote endpoint
See Also
java.net.preferIPv4Stack system property
readback to summary
public abstract int read(ByteBuffer dst) throws IOException

Redeclares java.nio.channels.ReadableByteChannel.read.

Doc from java.nio.channels.ReadableByteChannel.read.

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.

Parameters
dst:ByteBuffer

The buffer into which bytes are to be transferred

Returns:int

The number of bytes read, possibly zero, or -1 if the channel has reached end-of-stream

Exceptions
IOException:
If some other I/O error occurs
NotYetConnectedException:
If this channel is not yet connected
ClosedChannelException:
If this channel is closed
AsynchronousCloseException:
If another thread closes this channel while the read operation is in progress
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
readback to summary
public abstract long read(ByteBuffer[] dsts, int offset, int length) throws IOException

Redeclares java.nio.channels.ScatteringByteChannel.read.

Doc from java.nio.channels.ScatteringByteChannel.read.

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 in the specified subsequence of the given buffer array, that is,

dsts[offset].remaining() + dsts[offset+1].remaining() + ... + dsts[offset+length-1].remaining()
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.

Parameters
dsts:ByteBuffer[]

The buffers into which bytes are to be transferred

offset: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 dsts.length

length:int

The maximum number of buffers to be accessed; must be non-negative and no larger than dsts.length - offset

Returns:long

The number of bytes read, possibly zero, or -1 if the channel has reached end-of-stream

Exceptions
IOException:
If some other I/O error occurs
NotYetConnectedException:
If this channel is not yet connected
ClosedChannelException:
If this channel is closed
AsynchronousCloseException:
If another thread closes this channel while the read operation is in progress
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
readback to summary
public final long read(ByteBuffer[] dsts) throws IOException

Implements java.nio.channels.ScatteringByteChannel.read.

Doc from java.nio.channels.ScatteringByteChannel.read.

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, dsts.length);
c.read(dsts, 0, dsts.length);
Parameters
dsts:ByteBuffer[]

The buffers into which bytes are to be transferred

Returns:long

The number of bytes read, possibly zero, or -1 if the channel has reached end-of-stream

Exceptions
IOException:
If some other I/O error occurs
NotYetConnectedException:
If this channel is not yet connected
ClosedChannelException:
If this channel is closed
AsynchronousCloseException:
If another thread closes this channel while the read operation is in progress
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
setOptionback to summary
public abstract <T> SocketChannel setOption(SocketOption<T> name, T value) throws IOException

Redeclares java.nio.channels.NetworkChannel.setOption.

Doc from java.nio.channels.NetworkChannel.setOption.

Sets the value of a socket option.

Parameters
<T>
The type of the socket option value
name:SocketOption<T>

The socket option

value:T

The value of the socket option. A value of null may be a valid value for some socket options.

Returns:SocketChannel

This channel

Annotations
@Override
Exceptions
IOException:
If an I/O error occurs
UnsupportedOperationException:
If the socket option is not supported by this channel
IllegalArgumentException:
If the value is not a valid value for this socket option
ClosedChannelException:
If this channel is closed
Since
1.7
shutdownInputback to summary
public abstract SocketChannel shutdownInput() throws IOException

Shutdown the connection for reading without closing the channel.

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.

Returns:SocketChannel

The channel

Exceptions
IOException:
If some other I/O error occurs
NotYetConnectedException:
If this channel is not yet connected
ClosedChannelException:
If this channel is closed
Since
1.7
shutdownOutputback to summary
public abstract SocketChannel 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 ClosedChannelException. If the output side of the connection is already shutdown then invoking this method has no effect.

Returns:SocketChannel

The channel

Exceptions
IOException:
If some other I/O error occurs
NotYetConnectedException:
If this channel is not yet connected
ClosedChannelException:
If this channel is closed
Since
1.7
socketback to summary
public abstract Socket socket()

Retrieves a socket associated with this channel.

Returns:Socket

A socket associated with this channel

Exceptions
UnsupportedOperationException:
If the channel's socket is not an Internet protocol socket
validOpsback to summary
public final int validOps()

Implements abstract java.nio.channels.SelectableChannel.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 | SelectionKey#OP_READ | SelectionKey#OP_WRITE).

Returns:int

The valid-operation set

writeback to summary
public abstract int write(ByteBuffer src) throws IOException

Redeclares java.nio.channels.WritableByteChannel.write.

Doc from java.nio.channels.WritableByteChannel.write.

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, src.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.

Parameters
src:ByteBuffer

The buffer from which bytes are to be retrieved

Returns:int

The number of bytes written, possibly zero

Exceptions
IOException:
If some other I/O error occurs
NotYetConnectedException:
If this channel is not yet connected
ClosedChannelException:
If this channel is closed
AsynchronousCloseException:
If another thread closes this channel while the write operation is in progress
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
writeback to summary
public abstract long write(ByteBuffer[] srcs, int offset, int length) throws IOException

Redeclares java.nio.channels.GatheringByteChannel.write.

Doc from java.nio.channels.GatheringByteChannel.write.

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()
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.

Parameters
srcs:ByteBuffer[]

The buffers from which bytes are to be retrieved

offset: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 srcs.length

length:int

The maximum number of buffers to be accessed; must be non-negative and no larger than srcs.length - offset

Returns:long

The number of bytes written, possibly zero

Exceptions
IOException:
If some other I/O error occurs
NotYetConnectedException:
If this channel is not yet connected
ClosedChannelException:
If this channel is closed
AsynchronousCloseException:
If another thread closes this channel while the write operation is in progress
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
writeback to summary
public final long write(ByteBuffer[] srcs) throws IOException

Implements java.nio.channels.GatheringByteChannel.write.

Doc from java.nio.channels.GatheringByteChannel.write.

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);
c.write(srcs, 0, srcs.length);
Parameters
srcs:ByteBuffer[]

The buffers from which bytes are to be retrieved

Returns:long

The number of bytes written, possibly zero

Exceptions
IOException:
If some other I/O error occurs
NotYetConnectedException:
If this channel is not yet connected
ClosedChannelException:
If this channel is closed
AsynchronousCloseException:
If another thread closes this channel while the write operation is in progress
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