Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   /*
    * JBoss, Home of Professional Open Source.
    * Copyright 2012 Red Hat, Inc., and individual contributors
    * as indicated by the @author tags.
    *
    * Licensed under the Apache License, Version 2.0 (the "License");
    * you may not use this file except in compliance with the License.
    * You may obtain a copy of the License at
    *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package org.apache.tomcat.util.net;
  
  import  java.net.SocketOption;
  import  java.nio.channels.AlreadyBoundException;
  import  java.nio.channels.AsynchronousByteChannel;
  import  java.nio.channels.AsynchronousChannelGroup;
  import  java.nio.channels.AsynchronousServerSocketChannel;
  import  java.nio.channels.AsynchronousSocketChannel;
  import  java.nio.channels.CompletionHandler;
  import  java.nio.channels.InterruptedByTimeoutException;
  import  java.nio.channels.NetworkChannel;
  import  java.nio.channels.ReadPendingException;
  import  java.nio.channels.ShutdownChannelGroupException;
  import  java.nio.channels.WritePendingException;
  import  java.nio.channels.spi.AsynchronousChannelProvider;
  import java.util.Set;
NioChannel

An asynchronous channel for stream-oriented connecting sockets.

NIO Asynchronous socket channels are created in one of two ways. A newly-created NioChannel is created by invoking one of the open methods defined by this class. A newly-created channel is open but not yet connected. A connected NioChannel is created when a connection is made to the socket of an AsynchronousServerSocketChannel. It is not possible to create an asynchronous socket channel for an arbitrary, pre-existing socket.

A newly-created channel is connected by invoking its connect method; once connected, a channel remains connected until it is closed. Whether or not a socket channel is connected may be determined by invoking its getRemoteAddress method. An attempt to invoke an I/O operation upon an unconnected channel will cause a NotYetConnectedException to be thrown.

Channels of this type are safe for use by multiple concurrent threads. They support concurrent reading and writing, though at most one read operation and one write operation can be outstanding at any time. If a thread initiates a read operation before a previous read operation has completed then a ReadPendingException will be thrown. Similarly, an attempt to initiate a write operation before a previous write has completed will throw a WritePendingException.

Socket options are configured using the setOption method. NIO Asynchronous socket channels support the following options:

Option NameDescription
SO_SNDBUFThe size of the socket send buffer
SO_RCVBUFThe size of the socket receive buffer
SO_KEEPALIVEKeep connection alive
SO_REUSEADDRRe-use address
TCP_NODELAYDisable the Nagle algorithm
Additional (implementation specific) options may also be supported.

Timeouts

The read and write methods defined by this class allow a timeout to be specified when initiating a read or write operation. If the timeout elapses before an operation completes then the operation completes with the exception java.nio.channels.InterruptedByTimeoutException. A timeout may leave the channel, or the underlying connection, in an inconsistent state. Where the implementation cannot guarantee that bytes have not been read from the channel then it puts the channel into an implementation specific error state. A subsequent attempt to initiate a read operation causes an unspecified runtime exception to be thrown. Similarly if a write operation times out and the implementation cannot guarantee bytes have not been written to the channel then further attempts to write to the channel cause an unspecified runtime exception to be thrown. When a timeout elapses then the state of the java.nio.ByteBuffer, or the sequence of buffers, for the I/O operation is not defined. Buffers should be discarded or at least care must be taken to ensure that the buffers are not accessed while the channel remains open. All methods that accept timeout parameters treat values less than or equal to zero to mean that the I/O operation does not timeout.

Created on Dec 19, 2011 at 11:40:18 AM

Author(s):
Nabil Benothman
 
 public class NioChannel implements AsynchronousByteChannel, NetworkChannel {

 
 	public static final int OP_STATUS_NORMAL = 0;

Channel closed status code
 
 	public static final int OP_STATUS_CLOSED = -1;
Read timeout status code
 
 	public static final int OP_STATUS_READ_TIMEOUT = -2;
Write timeout status code
 
 	public static final int OP_STATUS_WRITE_TIMEOUT = -3;

 
 	public static final int OP_STATUS_READ_KILLED = -4;

 
 	public static final int OP_STATUS_WRITE_KILLED = -5;

Read/write operation error code
 
 	public static final int OP_STATUS_ERROR = -17;

 
 	private static final AtomicLong counter = new AtomicLong(0);
 	protected AsynchronousSocketChannel channel;
 	private long id;
 	private ByteBuffer buffer;
 
 	private Object lock = new Object();
 	public Object getLock() {
 	    return ;
 	}
 	private Object writeLock = new Object();
 	public Object getWriteLock() {
         return ;
     }

Create a new instance of NioChannel

Parameters:
channel The java.nio.channels.AsynchronousSocketChannel attached to this channel
Throws:
NullPointerException if the channel parameter is null
 
 	protected NioChannel(AsynchronousSocketChannel channel) {
 		if (channel == null) {
 			throw new NullPointerException("null channel parameter");
 		}
 		this. = channel;
 		this. = ByteBuffer.allocateDirect(1);
 	}

Opens an asynchronous socket channel.

The new channel is created by invoking the openAsynchronousSocketChannel method on the AsynchronousChannelProvider that created the group. If the group parameter is null then the resulting channel is created by the system-wide default provider, and bound to the default group.

Parameters:
group The group to which the newly constructed channel should be bound, or null for the default group
Returns:
A new asynchronous socket channel
Throws:
ShutdownChannelGroupException If the channel group is shutdown
IOException If an I/O error occurs
 
 	public static NioChannel open(AsynchronousChannelGroup groupthrows IOException {
 		AsynchronousSocketChannel channel = AsynchronousSocketChannel.open(group);
 		return new NioChannel(channel);
 	}

Opens a NioChannel.

This method returns an NioChannel that is bound to the default group.This method is equivalent to evaluating the expression:

 open((AsynchronousChannelGroup) null);
 

Returns:
A new NioChannel
Throws:
IOException If an I/O error occurs
 
 	public static NioChannel open() throws IOException {
 		return open(null);
 	}

Binds the channel's socket to a local address.

This method is used to establish an association between the socket and a local address. Once an association is established then the socket remains bound until the channel is closed. If the local parameter has the value null then the socket will be bound to an address that is assigned automatically.

Parameters:
local The address to bind the socket, or null to bind the socket to an automatically assigned socket address
Returns:
This channel
Throws:
AlreadyBoundException If the socket is already bound
UnsupportedAddressTypeException If the type of the given address is not supported
ClosedChannelException If the channel is closed
IOException If some other I/O error occurs
SecurityException If a security manager is installed and it denies an unspecified permission. An implementation of this interface should specify any required permissions.
See also:
getLocalAddress
 
 	public NioChannel bind(SocketAddress localthrows IOException {
 		this..bind(local);
 		return this;
 	}

Binds the channel's socket to a local address.

This method is used to establish an association between the socket and a local address. Once an association is established then the socket remains bound until the channel is closed.

Parameters:
hostname the Host name
port The port number
Returns:
This channel
Throws:
AlreadyBoundException If the socket is already bound
UnsupportedAddressTypeException If the type of the given address is not supported
ClosedChannelException If the channel is closed
IOException If some other I/O error occurs
IllegalArgumentException if the port parameter is outside the range of valid port values, or if the hostname parameter is null.
SecurityException If a security manager is installed and it denies an unspecified permission. An implementation of this interface should specify any required permissions.
See also:
bind(SocketAddress)
 
 	public NioChannel bind(String hostnameint portthrows IOException {
 		return bind(new InetSocketAddress(hostnameport));
 	}

Connects this channel.

This method initiates an operation to connect this channel. The handler parameter is a completion handler that is invoked when the connection is successfully established or connection cannot be established. If the connection cannot be established then the channel is closed.

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.

Parameters:
remote The remote address to which this channel is to be connected
attachment The object to attach to the I/O operation; can be null
handler The handler for consuming the result
Throws:
UnresolvedAddressException If the given remote address is not fully resolved
UnsupportedAddressTypeException If the type of the given remote address is not supported
AlreadyConnectedException If this channel is already connected
ConnectionPendingException If a connection operation is already in progress on this channel
ShutdownChannelGroupException If the channel group has terminated
SecurityException If a security manager has been installed and it does not permit access to the given remote endpoint
See also:
getRemoteAddress
 
 	public <A> void connect(SocketAddress remote, A attachment,
 			CompletionHandler<Void, ? super A> handler) {
 		this..connect(remoteattachmenthandler);
 	}

Connects this channel.

This method initiates an operation to connect this channel. This method behaves in exactly the same manner as the connect(SocketAddress, Object, CompletionHandler) method except that instead of specifying a completion handler, this method returns a Future representing the pending result. The Future's get method returns null on successful completion.

Parameters:
remote The remote address to which this channel is to be connected
Returns:
A Future object representing the pending result
Throws:
UnresolvedAddressException If the given remote address is not fully resolved
UnsupportedAddressTypeException If the type of the given remote address is not supported
AlreadyConnectedException If this channel is already connected
ConnectionPendingException If a connection operation is already in progress on this channel
SecurityException If a security manager has been installed and it does not permit access to the given remote endpoint
 
 	public Future<Voidconnect(SocketAddress remote) {
 		return this..connect(remote);
 	}

Reset the internal buffer
 
 	protected void reset() {
 		this..clear();
 	}

Retrieve data, if any, from the internal buffer, put it in the dst buffer and clear the internal buffer

Parameters:
dst the destination buffer
Returns:
the number of bytes retrieved
 
 	protected int reset(final ByteBuffer dst) {
 		this..flip();
 		int x = this..remaining();
 		dst.put(this.);
 		this..clear();
 
 		return x;
 	}

Returns:
TRUE if the channel is secure (i.e., use SSL), else FALSE
 
 	public boolean isSecure() {
 		return false;
 	}

Returns:
the internal buffer
 
 	protected ByteBuffer getBuffer() {
 		return this.;
 	}

Returns:
the channel id
 
 	public long getId() {
 		return this.;
 	}

Getter for name

Returns:
the name
 
 	public String getName() {
 		return getClass().getName() + "[" + getId() + "]";
 	}

Returns the provider that created this channel.

Returns:
the channel provider
 
 	public final AsynchronousChannelProvider provider() {
 		return this..provider();
 	}

Tells whether or not this channel is open.

Returns:
true if, and only if, this channel is open
 
 	public boolean isOpen() {
 		return this..isOpen();
 	}

Tells whether or not this channel is closed.

Returns:
true if, and only if, this channel is closed
 
 	public boolean isClosed() {
 		return !isOpen();
 	}

Close this channel.

Any outstanding asynchronous operations upon this channel will complete with the exception AsynchronousCloseException. After a channel is closed, further attempts to initiate asynchronous I/O operations complete immediately with cause ClosedChannelException.

This method otherwise behaves exactly as specified by the java.nio.channels.Channel interface.

Throws:
IOException If an I/O error occurs
 
 	public synchronized void close() throws IOException {
 		if (isClosed()) {
 			return;
 		}
 		// Closing the channel
 		this..close();
 	}

Try to close this channel. If the channel is already closed or the force parameter is false, nothing will happen. This method has an impact only if the channel is open and the force parameter is true

Parameters:
force a boolean value indicating if we need to force closing the channel
Throws:
IOException
 
 	public void close(boolean forcethrows IOException {
 		if (isOpen() && force) {
 			this.close();
 		}
 	}
 
 	/*
 	 * (non-Javadoc)
 	 * 
 	 * @see java.nio.channels.AsynchronousByteChannel#read(java.nio.ByteBuffer)
 	 * 
 	 * @deprecated (use readBytes(...) instead)
 	 */
 	public Future<Integerread(ByteBuffer dst) {
 		this.reset(dst);
 		return this..read(dst);
 	}

Read a sequence of bytes in blocking mode. This method works exactly in the same way as readBytes(dst, Integer.MAX_VALUE, TimeUnit.MILLISECONDS).

Parameters:
dst the buffer containing the read bytes
Returns:
the number of bytes read
Throws:
Exception
ExecutionException
InterruptedException
 
 	public int readBytes(ByteBuffer dstthrows Exception {
 	}

Read a sequence of bytes in blocking mode

Parameters:
dst the buffer containing the read bytes
timeout the read timeout
unit the timeout unit
Returns:
The possible returned values are :
  • The number of bytes read if the operation was succeed
  • NioChannel.OP_STATUS_CLOSED if the channel is closed
  • NioChannel.OP_STATUS_READ_TIMEOUT if the operation was timed out
Throws:
Exception
ExecutionException
InterruptedException
 
 	public int readBytes(final ByteBuffer dstfinal long timeoutfinal TimeUnit unit)
 			throws Exception {
 		try {
 			int x = this.reset(dst);
 			return (x + this..read(dst).get(timeoutunit));
 		} catch (Exception exp) {
 			if (exp instanceof ClosedChannelException) {
 			} else if (exp instanceof TimeoutException) {
 			} else {
 				throw exp;
 			}
 		}
 	}
 
 	/*
 	 * (non-Javadoc)
 	 * 
 	 * @see java.nio.channels.AsynchronousByteChannel#read(java.nio.ByteBuffer,
 	 * java.lang.Object, java.nio.channels.CompletionHandler)
 	 */
 	public <A> void read(ByteBuffer dst, A attachment, CompletionHandler<Integer, ? super A> handler) {
 		this.read(dst, 0L, .attachmenthandler);
 	}

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

This method initiates an asynchronous read operation to read a sequence of bytes from this channel into the given buffer. The handler parameter is a completion handler that is invoked when the read operation completes (or fails). The result passed to the completion handler is the number of bytes read or -1 if no bytes could be read because the channel has reached end-of-stream.

If a timeout is specified and the timeout elapses before the operation completes then the operation completes with the exception InterruptedByTimeoutException. Where a timeout occurs, and the implementation cannot guarantee that bytes have not been read, or will not be read from the channel into the given buffer, then further attempts to read from the channel will cause an unspecific runtime exception to be thrown.

Otherwise this method works in the same manner as the NioChannel.read(ByteBuffer,Object,CompletionHandler) method.

Parameters:
dst The buffer into which bytes are to be transferred
timeout The maximum time for the I/O operation to complete
unit The time unit of the timeout argument
attachment The object to attach to the I/O operation; can be null
handler The handler for consuming the result
Throws:
IllegalArgumentException If the buffer is read-only
ReadPendingException If a read operation is already in progress on this channel
NotYetConnectedException If this channel is not yet connected
ShutdownChannelGroupException If the channel group has terminated
 
 	public <A> void read(ByteBuffer dstlong timeoutTimeUnit unit, A attachment,
 			final CompletionHandler<Integer, ? super A> handler) {
 		final int x = this.reset(dst);
 		this..read(dsttimeoutunitattachmentnew CompletionHandler<Integer, A>() {
 
 			public void completed(Integer result, A attach) {
 				handler.completed(result + xattach);
 			}
 
 			public void failed(Throwable exc, A attach) {
 				handler.failed(excattach);
 			}
 		});
 	}

Reads a sequence of bytes from this channel into a subsequence of the given buffers. This operation, sometimes called a scattering read, is often useful when implementing network protocols that group data into segments consisting of one or more fixed-length headers followed by a variable-length body. The handler parameter is a completion handler that is invoked when the read operation completes (or fails). The result passed to the completion handler is the number of bytes read or -1 if no bytes could be read because the channel has reached end-of-stream.

This method initiates a read of up to r bytes from this channel, where r is the total number of bytes remaining in the specified subsequence of the given buffer array, that is,

 dsts[offset].remaining()
     + dsts[offset+1].remaining()
     + ... + dsts[offset+length-1].remaining()
 
at the moment that the read is attempted.

Suppose that a byte sequence of length n is read, where 0  < n <= r. Up to the first dsts[offset].remaining() bytes of this sequence are transferred into buffer dsts[offset], up to the next dsts[offset+1].remaining() bytes are transferred into buffer dsts[offset+1], and so forth, until the entire byte sequence is transferred into the given buffers. As many bytes as possible are transferred into each buffer, hence the final position of each updated buffer, except the last updated buffer, is guaranteed to be equal to that buffer's limit. The underlying operating system may impose a limit on the number of buffers that may be used in an I/O operation. Where the number of buffers (with bytes remaining), exceeds this limit, then the I/O operation is performed with the maximum number of buffers allowed by the operating system.

If a timeout is specified and the timeout elapses before the operation completes then it completes with the exception InterruptedByTimeoutException. Where a timeout occurs, and the implementation cannot guarantee that bytes have not been read, or will not be read from the channel into the given buffers, then further attempts to read from the channel will cause an unspecific runtime exception to be thrown.

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
timeout The maximum time for the I/O operation to complete
unit The time unit of the timeout argument
attachment The object to attach to the I/O operation; can be null
handler The handler for consuming the result
Throws:
IndexOutOfBoundsException If the pre-conditions for the offset and length parameter aren't met
IllegalArgumentException If the buffer is read-only
ReadPendingException If a read operation is already in progress on this channel
NotYetConnectedException If this channel is not yet connected
ShutdownChannelGroupException If the channel group has terminated
 
 	public <A> void read(ByteBuffer[] dstsint offsetint lengthlong timeoutTimeUnit unit,
 			A attachmentfinal CompletionHandler<Long, ? super A> handler) {
 
 		// Retrieve bytes, if any, from the internal buffer
 		final int x = this.reset(dsts[0]);
 
 		this..read(dstsoffsetlengthtimeoutunitattachment,
 				new CompletionHandler<Long, A>() {
 
 					public void completed(Long result, A attach) {
 						handler.completed(result + xattach);
 					}
 
 					public void failed(Throwable exc, A attach) {
 						handler.failed(excattach);
 					}
 				});
 	}

Wait for incoming data in a non-blocking mode. The received data will be stored by default in the internal buffer (By default, just one byte).

The byte read using this method will be available for the next read operation. That is, When attempting to read after receiving a read notification saying that there is data available on this stream, the byte will be first copied in the destination byte buffer and then perform the read operation which may be a blocking or non-blocking operation. So the user does not have to do that manually.

Parameters:
timeout The maximum time for the I/O operation to complete
unit The time unit of the timeout argument
attachment The object to attach to the I/O operation; can be null
handler The handler for consuming the result
Throws:
ReadPendingException If a read operation is already in progress on this channel
NotYetConnectedException If this channel is not yet connected
ShutdownChannelGroupException If the channel group has terminated
 
 	public <A> void awaitRead(long timeoutTimeUnit unitfinal A attachment,
 			final CompletionHandler<Integer, ? super A> handler) {
 
 		// Clear the internal buffer
 		this..clear();
 		// Perform an asynchronous read operation using the internal buffer
 		this..read(this.timeoutunitattachmenthandler);
 	}

Wait for incoming data in a non-blocking mode. The received data will be stored by default in the internal buffer (By default, just one byte).

This method behaves exactly in the same manner as awaitRead(long, TimeUnit, Object, CompletionHandler) but without a timeout defined.
awaitRead(0L, TimeUnit.MILLISECONDS, attachment, handler);

Parameters:
attachment
handler
See also:
awaitRead(long, TimeUnit, Object, CompletionHandler)
 
 	public <A> void awaitRead(final A attachment,
 			final CompletionHandler<Integer, ? super A> handler) {
 		awaitRead(0L, .attachmenthandler);
 	}

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

Deprecated:
(use writeBytes(...) instead)
Parameters:
src The buffer from which bytes are to be retrieved
Returns:
an instance of java.util.concurrent.Future containing the number of bytes written
See also:
java.nio.channels.AsynchronousByteChannel.write(java.nio.ByteBuffer)
 
 	public Future<Integerwrite(ByteBuffer src) {
 		return this..write(src);
 	}

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

This method initiates an asynchronous write operation to write a sequence of bytes to this channel from the given buffer. The method behaves in exactly the same manner as the writeBytes(ByteBuffer, long, TimeUnit) with java.lang.Integer.MAX_VALUE as a timeout and java.util.concurrent.TimeUnit.MILLISECONDS as a time unit.

Parameters:
src The buffer from which bytes are to be retrieved
Returns:
The number of bytes written
Throws:
WritePendingException If the channel does not allow more than one write to be outstanding and a previous write has not completed
ExecutionException
InterruptedException
Exception If any other type of errors occurs
See also:
writeBytes(ByteBuffer, long, TimeUnit)
 
 	public int writeBytes(ByteBuffer srcthrows Exception {
 	}

Write a sequence of bytes in blocking mode.

Parameters:
src the buffer containing the bytes to write
timeout the read timeout
unit the timeout unit
Returns:
The possible returned values are :
  • The number of bytes written if the operation was succeed
  • NioChannel.OP_STATUS_CLOSED if the channel is closed
  • NioChannel.OP_STATUS_WRITE_TIMEOUT if the operation was timed out
Throws:
Exception
ExecutionException
InterruptedException
 
 	public int writeBytes(ByteBuffer srclong timeoutTimeUnit unitthrows Exception {
 		return this..write(src).get(timeoutunit);
 	}
 
 	/*
 	 * (non-Javadoc)
 	 * 
 	 * @see java.nio.channels.AsynchronousByteChannel#write(java.nio.ByteBuffer,
 	 * java.lang.Object, java.nio.channels.CompletionHandler)
 	 */
 	public <A> void write(ByteBuffer src, A attachment,
 			CompletionHandler<Integer, ? super A> handler) {
 		write(src, 0L, .attachmenthandler);
 	}

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

This method initiates an asynchronous write operation to write a sequence of bytes to this channel from the given buffer. The handler parameter is a completion handler that is invoked when the write operation completes (or fails). The result passed to the completion handler is the number of bytes written.

If a timeout is specified and the timeout elapses before the operation completes then it completes with the exception InterruptedByTimeoutException. Where a timeout occurs, and the implementation cannot guarantee that bytes have not been written, or will not be written to the channel from the given buffer, then further attempts to write to the channel will cause an unspecific runtime exception to be thrown.

Otherwise this method works in the same manner as the NioChannel.write(ByteBuffer,Object,CompletionHandler) method.

Parameters:
src The buffer from which bytes are to be retrieved
timeout The maximum time for the I/O operation to complete
unit The time unit of the timeout argument
attachment The object to attach to the I/O operation; can be null
handler The handler for consuming the result
Throws:
WritePendingException If a write operation is already in progress on this channel
NotYetConnectedException If this channel is not yet connected
ShutdownChannelGroupException If the channel group has terminated
 
 	public <A> void write(ByteBuffer srclong timeoutTimeUnit unit, A attachment,
 			final CompletionHandler<Integer, ? super A> handler) {
 
 		this..write(srctimeoutunitattachmenthandler);
 	}

Writes a sequence of bytes to this channel from a subsequence of the given buffers. This operation, sometimes called a gathering write, is often useful when implementing network protocols that group data into segments consisting of one or more fixed-length headers followed by a variable-length body. The handler parameter is a completion handler that is invoked when the write operation completes (or fails). The result passed to the completion handler is the number of bytes written.

This method initiates a write of up to r bytes to this channel, where r is the total number of bytes remaining in the specified subsequence of the given buffer array, that is,

 srcs[offset].remaining()
     + srcs[offset+1].remaining()
     + ... + srcs[offset+length-1].remaining()
 
at the moment that the write is attempted.

Suppose that a byte sequence of length n is written, where 0 < n <=  r. Up to the first srcs[offset].remaining() bytes of this sequence are written from buffer srcs[offset], up to the next srcs[offset+1].remaining() bytes are written from buffer srcs[offset+1], and so forth, until the entire byte sequence is written. As many bytes as possible are written from each buffer, hence the final position of each updated buffer, except the last updated buffer, is guaranteed to be equal to that buffer's limit. The underlying operating system may impose a limit on the number of buffers that may be used in an I/O operation. Where the number of buffers (with bytes remaining), exceeds this limit, then the I/O operation is performed with the maximum number of buffers allowed by the operating system.

If a timeout is specified and the timeout elapses before the operation completes then it completes with the exception InterruptedByTimeoutException. Where a timeout occurs, and the implementation cannot guarantee that bytes have not been written, or will not be written to the channel from the given buffers, then further attempts to write to the channel will cause an unspecific runtime exception to be thrown.

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
timeout The maximum time for the I/O operation to complete
unit The time unit of the timeout argument
attachment The object to attach to the I/O operation; can be null
handler The handler for consuming the result
Throws:
IndexOutOfBoundsException If the pre-conditions for the offset and length parameter aren't met
WritePendingException If a write operation is already in progress on this channel
NotYetConnectedException If this channel is not yet connected
ShutdownChannelGroupException If the channel group has terminated
	public <A> void write(ByteBuffer[] srcsint offsetint lengthlong timeoutTimeUnit unit,
attachmentfinal CompletionHandler<Long, ? super A> handler) {
		this..write(srcsoffsetlengthtimeoutunitattachmenthandler);
	}

Returns:
the local address
Throws:
IOException
		return this..getLocalAddress();
	}

Returns:
the remote address
Throws:
IOException
		return this..getRemoteAddress();
	}

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. The effect on an outstanding read operation is system dependent and therefore not specified. The effect, if any, when there is data in the socket receive buffer that has not been read, or data arrives subsequently, is also system dependent.

Returns:
This channel
Throws:
NotYetConnectedException If this channel is not yet connected
ClosedChannelException If this channel is closed
IOException If some other I/O error occurs
	public final NioChannel shutdownInput() throws IOException {
		this..shutdownInput();
		return this;
	}

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. The effect on an outstanding write operation is system dependent and therefore not specified.

Returns:
This channel
Throws:
NotYetConnectedException If this channel is not yet connected
ClosedChannelException If this channel is closed
IOException If some other I/O error occurs
	public final NioChannel shutdownOutput() throws IOException {
		this..shutdownOutput();
		return this;
	}

Sets the value of a socket option.

Parameters:
name The socket option name
value The value of the socket option. A value of null may be a valid value for some socket options.
Returns:
This channel
Throws:
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
IOException If an I/O error occurs
See also:
java.net.StandardSocketOptions
	public <T> NioChannel setOption(SocketOption<T> name, T valuethrows IOException {
		this..setOption(namevalue);
		return this;
	}

Returns the value of a socket option.

Parameters:
name The socket option
Returns:
The value of the socket option. A value of null may be a valid value for some socket options.
Throws:
UnsupportedOperationException If the socket option is not supported by this channel
ClosedChannelException If this channel is closed
IOException If an I/O error occurs
See also:
java.net.StandardSocketOptions
	public <T> T getOption(SocketOption<T> namethrows IOException {
		return this..getOption(name);
	}

Returns a set of the socket options supported by this channel.

This method will continue to return the set of options even after the channel has been closed.

Returns:
A set of the socket options supported by this channel
	public Set<SocketOption<?>> supportedOptions() {
		return this..supportedOptions();
	public String toString() {
		return getName();
New to GrepCode? Check out our FAQ X