Top Description Fields Constructors Methods
org.apache.tomcat.util.net

public Class Nio2Channel

extends Object
implements AsynchronousByteChannel
Class Inheritance
All Implemented Interfaces
java.nio.channels.AsynchronousByteChannel, java.nio.channels.AsynchronousChannel, java.nio.channels.Channel, java.io.Closeable, java.lang.AutoCloseable
Known Direct Subclasses
org.apache.tomcat.util.net.SecureNio2Channel
Imports
java.io.IOException, java.nio.ByteBuffer, java.nio.channels.AsynchronousByteChannel, .AsynchronousSocketChannel, .ClosedChannelException, .CompletionHandler, java.util.concurrent.ExecutionException, .Future, .TimeUnit, .TimeoutException

Base class for a SocketChannel wrapper used by the endpoint. This way, logic for an SSL socket channel remains the same as for a non SSL, making sure we don't need to code for any exception cases.

Field Summary

Modifier and TypeField and Description
private ApplicationBufferHandler
protected final SocketBufferHandler
pack-priv static final Nio2Channel
private static final Future<Boolean>
private static final Future<Integer>
protected static final ByteBuffer
protected AsynchronousSocketChannel
protected SocketWrapperBase<Nio2Channel>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
close()

Implements java.nio.channels.AsynchronousChannel.close.

Closes this channel.

public void
close(boolean
Should the underlying socket be forcibly closed?
force
)

Close the connection.

public Future<Boolean>
public void
free()

Free the channel memory

protected ApplicationBufferHandler
public SocketBufferHandler
public AsynchronousSocketChannel
pack-priv SocketWrapperBase<Nio2Channel>
public int

Returns:

Always returns zero
handshake
()

Performs SSL handshake hence is a no-op for the non-secure implementation.

public boolean
public boolean
public boolean

Returns:

true if, and only if, this channel is open
isOpen
()

Implements java.nio.channels.Channel.isOpen.

Tells whether or not this channel is open.

public Future<Integer>
read(ByteBuffer
The buffer into which bytes are to be transferred
dst
)

Implements java.nio.channels.AsynchronousByteChannel.read.

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

public <A> void
read(ByteBuffer
The buffer into which bytes are to be transferred
dst
,
A
The object to attach to the I/O operation; can be null
attachment
,
CompletionHandler<Integer, ? super A>
The completion handler
handler
)

Implements java.nio.channels.AsynchronousByteChannel.read.

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

public <A> void
read(ByteBuffer dst, long timeout, TimeUnit unit, A attachment, CompletionHandler<Integer, ? super A> handler)

public <A> void
read(ByteBuffer[] dsts, int offset, int length, long timeout, TimeUnit unit, A attachment, CompletionHandler<Long, ? super A> handler)

public void
reset(AsynchronousSocketChannel
The new async channel to associate with this NIO2 channel
channel
,
SocketWrapperBase<Nio2Channel>
The new socket to associate with this NIO2 channel
socketWrapper
)

Reset the channel.

public void
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

public Future<Integer>
write(ByteBuffer
The buffer from which bytes are to be retrieved
src
)

Implements java.nio.channels.AsynchronousByteChannel.write.

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

public <A> void
write(ByteBuffer
The buffer from which bytes are to be retrieved
src
,
A
The object to attach to the I/O operation; can be null
attachment
,
CompletionHandler<Integer, ? super A>
The completion handler object
handler
)

Implements java.nio.channels.AsynchronousByteChannel.write.

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

public <A> void
write(ByteBuffer src, long timeout, TimeUnit unit, A attachment, CompletionHandler<Integer, ? super A> handler)

public <A> void
write(ByteBuffer[] srcs, int offset, int length, long timeout, TimeUnit unit, A attachment, CompletionHandler<Long, ? super A> handler)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

appReadBufHandlerback to summary
private ApplicationBufferHandler appReadBufHandler
bufHandlerback to summary
protected final SocketBufferHandler bufHandler
CLOSED_NIO2_CHANNELback to summary
pack-priv static final Nio2Channel CLOSED_NIO2_CHANNEL
DONEback to summary
private static final Future<Boolean> DONE
DONE_INTback to summary
private static final Future<Integer> DONE_INT
emptyBufback to summary
protected static final ByteBuffer emptyBuf
scback to summary
protected AsynchronousSocketChannel sc
socketWrapperback to summary
protected SocketWrapperBase<Nio2Channel> socketWrapper

Constructor Detail

Nio2Channelback to summary
public Nio2Channel(SocketBufferHandler bufHandler)

Method Detail

closeback to summary
public void close() throws IOException

Implements java.nio.channels.AsynchronousChannel.close.

Closes this channel.

Annotations
@Override
Exceptions
IOException:
If an I/O error occurs
closeback to summary
public void close(boolean force) throws IOException

Close the connection.

Parameters
force:boolean

Should the underlying socket be forcibly closed?

Exceptions
IOException:
If closing the secure channel fails.
flushback to summary
public Future<Boolean> flush()
freeback to summary
public void free()

Free the channel memory

getAppReadBufHandlerback to summary
protected ApplicationBufferHandler getAppReadBufHandler()
getBufHandlerback to summary
public SocketBufferHandler getBufHandler()
getIOChannelback to summary
public AsynchronousSocketChannel getIOChannel()
getSocketWrapperback to summary
pack-priv SocketWrapperBase<Nio2Channel> getSocketWrapper()
handshakeback to summary
public int handshake() throws IOException

Performs SSL handshake hence is a no-op for the non-secure implementation.

Returns:int

Always returns zero

Exceptions
IOException:
Never for non-secure channel
isClosingback to summary
public boolean isClosing()
isHandshakeCompleteback to summary
public boolean isHandshakeComplete()
isOpenback to summary
public boolean isOpen()

Implements java.nio.channels.Channel.isOpen.

Tells whether or not this channel is open.

Returns:boolean

true if, and only if, this channel is open

Annotations
@Override
readback to summary
public Future<Integer> read(ByteBuffer dst)

Implements java.nio.channels.AsynchronousByteChannel.read.

Doc from java.nio.channels.AsynchronousByteChannel.read.

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 method behaves in exactly the same manner as the read(ByteBuffer,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 the number of bytes read or -1 if no bytes could be read because the channel has reached end-of-stream.

Parameters
dst:ByteBuffer

The buffer into which bytes are to be transferred

Returns:Future<Integer>

A Future representing the result of the operation

Annotations
@Override
readback to summary
public <A> void read(ByteBuffer dst, A attachment, CompletionHandler<Integer, ? super A> handler)

Implements java.nio.channels.AsynchronousByteChannel.read.

Doc from java.nio.channels.AsynchronousByteChannel.read.

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.

The read operation may 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 time that the read is attempted. Where r is 0, the read operation completes immediately with a result of 0 without initiating an I/O operation.

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

Buffers are not safe for use by multiple concurrent threads so care should be taken to not access the buffer until the operation has completed.

This method may be invoked at any time. Some channel types may not allow more than one read to be outstanding at any given time. If a thread initiates a read operation before a previous read operation has completed then a ReadPendingException will be thrown.

Parameters
<A>
The type of the attachment
dst:ByteBuffer

The buffer into which bytes are to be transferred

attachment:A

The object to attach to the I/O operation; can be null

handler:CompletionHandler<Integer, ? super A>

The completion handler

Annotations
@Override
readback to summary
public <A> void read(ByteBuffer dst, long timeout, TimeUnit unit, A attachment, CompletionHandler<Integer, ? super A> handler)
readback to summary
public <A> void read(ByteBuffer[] dsts, int offset, int length, long timeout, TimeUnit unit, A attachment, CompletionHandler<Long, ? super A> handler)
resetback to summary
public void reset(AsynchronousSocketChannel channel, SocketWrapperBase<Nio2Channel> socketWrapper) throws IOException

Reset the channel.

Parameters
channel:AsynchronousSocketChannel

The new async channel to associate with this NIO2 channel

socketWrapper:SocketWrapperBase<Nio2Channel>

The new socket to associate with this NIO2 channel

Exceptions
IOException:
If a problem was encountered resetting the channel
setAppReadBufHandlerback to summary
public void setAppReadBufHandler(ApplicationBufferHandler handler)
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override
writeback to summary
public Future<Integer> write(ByteBuffer src)

Implements java.nio.channels.AsynchronousByteChannel.write.

Doc from java.nio.channels.AsynchronousByteChannel.write.

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 write(ByteBuffer,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 the number of bytes written.

Parameters
src:ByteBuffer

The buffer from which bytes are to be retrieved

Returns:Future<Integer>

A Future representing the result of the operation

Annotations
@Override
writeback to summary
public <A> void write(ByteBuffer src, A attachment, CompletionHandler<Integer, ? super A> handler)

Implements java.nio.channels.AsynchronousByteChannel.write.

Doc from java.nio.channels.AsynchronousByteChannel.write.

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.

The write operation may 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 time that the write is attempted. Where r is 0, the write operation completes immediately with a result of 0 without initiating an I/O operation.

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

Buffers are not safe for use by multiple concurrent threads so care should be taken to not access the buffer until the operation has completed.

This method may be invoked at any time. Some channel types may not allow more than one write to be outstanding at any given time. If a thread initiates a write operation before a previous write operation has completed then a WritePendingException will be thrown.

Parameters
<A>
The type of the attachment
src:ByteBuffer

The buffer from which bytes are to be retrieved

attachment:A

The object to attach to the I/O operation; can be null

handler:CompletionHandler<Integer, ? super A>

The completion handler object

Annotations
@Override
writeback to summary
public <A> void write(ByteBuffer src, long timeout, TimeUnit unit, A attachment, CompletionHandler<Integer, ? super A> handler)
writeback to summary
public <A> void write(ByteBuffer[] srcs, int offset, int length, long timeout, TimeUnit unit, A attachment, CompletionHandler<Long, ? super A> handler)