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

public Class NioChannel

extends Object
implements ByteChannel, ScatteringByteChannel, GatheringByteChannel
Class Inheritance
All Implemented Interfaces
java.nio.channels.GatheringByteChannel, java.nio.channels.WritableByteChannel, java.nio.channels.Channel, java.io.Closeable, java.lang.AutoCloseable, java.nio.channels.ScatteringByteChannel, java.nio.channels.ReadableByteChannel, java.nio.channels.ByteChannel
Known Direct Subclasses
org.apache.tomcat.util.net.SecureNioChannel
Imports
java.io.IOException, java.nio.ByteBuffer, java.nio.channels.ByteChannel, .ClosedChannelException, .GatheringByteChannel, .ScatteringByteChannel, .SocketChannel, org.apache.tomcat.util.net.NioEndpoint.NioSocketWrapper, org.apache.tomcat.util.res.StringManager

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 NioChannel
protected static final ByteBuffer
protected SocketChannel
protected static final StringManager
protected NioEndpoint.NioSocketWrapper

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected void
checkInterruptStatus()

This method should be used to check the interrupt status before attempting a write.

public void
close()

Implements java.nio.channels.Channel.close.

Closes this channel.

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

Close the connection.

public boolean

Returns:

Always returns false for non-secure channel
flushOutbound
()

Return true if the buffer wrote data.

public void
free()

Free the channel memory

protected ApplicationBufferHandler
public SocketBufferHandler
public SocketChannel
public int
pack-priv NioEndpoint.NioSocketWrapper

Returns:

the socketWrapper
getSocketWrapper
()

public int

Returns:

Always returns zero
handshake
(boolean
Unused in non-secure implementation
read
,
boolean
Unused in non-secure implementation
write
)

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 int

Returns:

The number of bytes read, possibly zero, or -1 if the channel has reached end-of-stream
read
(ByteBuffer
The buffer into which bytes are to be transferred
dst
)

Implements java.nio.channels.ReadableByteChannel.read.

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

public 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 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
)

Implements java.nio.channels.ScatteringByteChannel.read.

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

public void
reset(SocketChannel
the socket channel
channel
,
NioEndpoint.NioSocketWrapper
the socket wrapper
socketWrapper
)

Reset the channel

public void
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

public int

Returns:

The number of bytes written, possibly zero
write
(ByteBuffer
The buffer from which bytes are to be retrieved
src
)

Implements java.nio.channels.WritableByteChannel.write.

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

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

public 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
)

Implements java.nio.channels.GatheringByteChannel.write.

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

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

appReadBufHandlerback to summary
private ApplicationBufferHandler appReadBufHandler
bufHandlerback to summary
protected final SocketBufferHandler bufHandler
CLOSED_NIO_CHANNELback to summary
pack-priv static final NioChannel CLOSED_NIO_CHANNEL
emptyBufback to summary
protected static final ByteBuffer emptyBuf
scback to summary
protected SocketChannel sc
smback to summary
protected static final StringManager sm
socketWrapperback to summary
protected NioEndpoint.NioSocketWrapper socketWrapper

Constructor Detail

NioChannelback to summary
public NioChannel(SocketBufferHandler bufHandler)

Method Detail

checkInterruptStatusback to summary
protected void checkInterruptStatus() throws IOException

This method should be used to check the interrupt status before attempting a write. If a thread has been interrupted and the interrupt has not been cleared then an attempt to write to the socket will fail. When this happens the socket is removed from the poller without the socket being selected. This results in a connection limit leak for NIO as the endpoint expects the socket to be selected even in error conditions.

Exceptions
IOException:
If the current thread was interrupted
closeback to summary
public void close() throws IOException

Implements java.nio.channels.Channel.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.
flushOutboundback to summary
public boolean flushOutbound() throws IOException

Return true if the buffer wrote data. NO-OP for non-secure channel.

Returns:boolean

Always returns false for non-secure channel

Exceptions
IOException:
Never for non-secure channel
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 SocketChannel getIOChannel()
getOutboundRemainingback to summary
public int getOutboundRemaining()
getSocketWrapperback to summary
pack-priv NioEndpoint.NioSocketWrapper getSocketWrapper()
Returns:NioEndpoint.NioSocketWrapper

the socketWrapper

handshakeback to summary
public int handshake(boolean read, boolean write) throws IOException

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

Parameters
read:boolean

Unused in non-secure implementation

write:boolean

Unused in 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 int read(ByteBuffer dst) throws IOException

Implements java.nio.channels.ReadableByteChannel.read.

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

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

Annotations
@Override
Exceptions
IOException:
If some other I/O error occurs
readback to summary
public 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

Annotations
@Override
Exceptions
IOException:
If some other I/O error occurs
readback to summary
public long read(ByteBuffer[] dsts, int offset, int length) throws IOException

Implements 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

Annotations
@Override
Exceptions
IOException:
If some other I/O error occurs
resetback to summary
public void reset(SocketChannel channel, NioEndpoint.NioSocketWrapper socketWrapper) throws IOException

Reset the channel

Parameters
channel:SocketChannel

the socket channel

socketWrapper:NioEndpoint.NioSocketWrapper

the socket wrapper

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 int write(ByteBuffer src) throws IOException

Implements java.nio.channels.WritableByteChannel.write.

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

Parameters
src:ByteBuffer

The buffer from which bytes are to be retrieved

Returns:int

The number of bytes written, possibly zero

Annotations
@Override
Exceptions
IOException:
If some other I/O error occurs
writeback to summary
public 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

Annotations
@Override
Exceptions
IOException:
If some other I/O error occurs
writeback to summary
public long write(ByteBuffer[] srcs, int offset, int length) throws IOException

Implements 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

Annotations
@Override
Exceptions
IOException:
If some other I/O error occurs