Package org.apache.tomcat.util.net
Class NioChannel
- java.lang.Object
- 
- org.apache.tomcat.util.net.NioChannel
 
- 
- All Implemented Interfaces:
- java.io.Closeable,- java.lang.AutoCloseable,- java.nio.channels.ByteChannel,- java.nio.channels.Channel,- java.nio.channels.GatheringByteChannel,- java.nio.channels.ReadableByteChannel,- java.nio.channels.ScatteringByteChannel,- java.nio.channels.WritableByteChannel
 - Direct Known Subclasses:
- SecureNioChannel
 
 public class NioChannel extends java.lang.Object implements java.nio.channels.ByteChannel, java.nio.channels.ScatteringByteChannel, java.nio.channels.GatheringByteChannelBase 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 SummaryFields Modifier and Type Field Description protected SocketBufferHandlerbufHandlerprotected static java.nio.ByteBufferemptyBufprotected java.nio.channels.SocketChannelscprotected static StringManagersmprotected NioEndpoint.NioSocketWrappersocketWrapper
 - 
Constructor SummaryConstructors Constructor Description NioChannel(SocketBufferHandler bufHandler)
 - 
Method SummaryAll Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidcheckInterruptStatus()This method should be used to check the interrupt status before attempting a write.voidclose()Closes this channel.voidclose(boolean force)Close the connection.booleanflush(boolean block, java.nio.channels.Selector s, long timeout)Returns true if the network buffer has been flushed out and is empty.booleanflushOutbound()Return true if the buffer wrote data.voidfree()Free the channel memoryprotected ApplicationBufferHandlergetAppReadBufHandler()SocketBufferHandlergetBufHandler()java.nio.channels.SocketChannelgetIOChannel()intgetOutboundRemaining()inthandshake(boolean read, boolean write)Performs SSL handshake hence is a no-op for the non-secure implementation.booleanisClosing()booleanisHandshakeComplete()booleanisOpen()Tells whether or not this channel is open.intread(java.nio.ByteBuffer dst)Reads a sequence of bytes from this channel into the given buffer.longread(java.nio.ByteBuffer[] dsts)longread(java.nio.ByteBuffer[] dsts, int offset, int length)voidreset(java.nio.channels.SocketChannel channel, NioEndpoint.NioSocketWrapper socketWrapper)Reset the channelvoidsetAppReadBufHandler(ApplicationBufferHandler handler)java.lang.StringtoString()intwrite(java.nio.ByteBuffer src)Writes a sequence of bytes to this channel from the given buffer.longwrite(java.nio.ByteBuffer[] srcs)longwrite(java.nio.ByteBuffer[] srcs, int offset, int length)
 
- 
- 
- 
Field Detail- 
smprotected static final StringManager sm 
 - 
emptyBufprotected static final java.nio.ByteBuffer emptyBuf 
 - 
bufHandlerprotected final SocketBufferHandler bufHandler 
 - 
scprotected java.nio.channels.SocketChannel sc 
 - 
socketWrapperprotected NioEndpoint.NioSocketWrapper socketWrapper 
 
- 
 - 
Constructor Detail- 
NioChannelpublic NioChannel(SocketBufferHandler bufHandler) 
 
- 
 - 
Method Detail- 
resetpublic void reset(java.nio.channels.SocketChannel channel, NioEndpoint.NioSocketWrapper socketWrapper) throws java.io.IOExceptionReset the channel- Parameters:
- channel- the socket channel
- socketWrapper- the socket wrapper
- Throws:
- java.io.IOException- If a problem was encountered resetting the channel
 
 - 
freepublic void free() Free the channel memory
 - 
flushpublic boolean flush(boolean block, java.nio.channels.Selector s, long timeout) throws java.io.IOExceptionReturns true if the network buffer has been flushed out and is empty.- Parameters:
- block- Unused. May be used when overridden
- s- Unused. May be used when overridden
- timeout- Unused. May be used when overridden
- Returns:
- Always returns truesince there is no network buffer in the regular channel
- Throws:
- java.io.IOException- Never for non-secure channel
 
 - 
closepublic void close() throws java.io.IOExceptionCloses this channel.- Specified by:
- closein interface- java.lang.AutoCloseable
- Specified by:
- closein interface- java.nio.channels.Channel
- Specified by:
- closein interface- java.io.Closeable
- Throws:
- java.io.IOException- If an I/O error occurs
 
 - 
closepublic void close(boolean force) throws java.io.IOExceptionClose the connection.- Parameters:
- force- Should the underlying socket be forcibly closed?
- Throws:
- java.io.IOException- If closing the secure channel fails.
 
 - 
isOpenpublic boolean isOpen() Tells whether or not this channel is open.- Specified by:
- isOpenin interface- java.nio.channels.Channel
- Returns:
- trueif, and only if, this channel is open
 
 - 
writepublic int write(java.nio.ByteBuffer src) throws java.io.IOExceptionWrites a sequence of bytes to this channel from the given buffer.- Specified by:
- writein interface- java.nio.channels.WritableByteChannel
- Parameters:
- src- The buffer from which bytes are to be retrieved
- Returns:
- The number of bytes written, possibly zero
- Throws:
- java.io.IOException- If some other I/O error occurs
 
 - 
writepublic long write(java.nio.ByteBuffer[] srcs) throws java.io.IOException- Specified by:
- writein interface- java.nio.channels.GatheringByteChannel
- Throws:
- java.io.IOException
 
 - 
writepublic long write(java.nio.ByteBuffer[] srcs, int offset, int length) throws java.io.IOException- Specified by:
- writein interface- java.nio.channels.GatheringByteChannel
- Throws:
- java.io.IOException
 
 - 
readpublic int read(java.nio.ByteBuffer dst) throws java.io.IOExceptionReads a sequence of bytes from this channel into the given buffer.- Specified by:
- readin interface- java.nio.channels.ReadableByteChannel
- Parameters:
- dst- The buffer into which bytes are to be transferred
- Returns:
- The number of bytes read, possibly zero, or -1if the channel has reached end-of-stream
- Throws:
- java.io.IOException- If some other I/O error occurs
 
 - 
readpublic long read(java.nio.ByteBuffer[] dsts) throws java.io.IOException- Specified by:
- readin interface- java.nio.channels.ScatteringByteChannel
- Throws:
- java.io.IOException
 
 - 
readpublic long read(java.nio.ByteBuffer[] dsts, int offset, int length) throws java.io.IOException- Specified by:
- readin interface- java.nio.channels.ScatteringByteChannel
- Throws:
- java.io.IOException
 
 - 
getBufHandlerpublic SocketBufferHandler getBufHandler() 
 - 
getIOChannelpublic java.nio.channels.SocketChannel getIOChannel() 
 - 
isClosingpublic boolean isClosing() 
 - 
isHandshakeCompletepublic boolean isHandshakeComplete() 
 - 
handshakepublic int handshake(boolean read, boolean write) throws java.io.IOExceptionPerforms SSL handshake hence is a no-op for the non-secure implementation.- Parameters:
- read- Unused in non-secure implementation
- write- Unused in non-secure implementation
- Returns:
- Always returns zero
- Throws:
- java.io.IOException- Never for non-secure channel
 
 - 
toStringpublic java.lang.String toString() - Overrides:
- toStringin class- java.lang.Object
 
 - 
getOutboundRemainingpublic int getOutboundRemaining() 
 - 
flushOutboundpublic boolean flushOutbound() throws java.io.IOExceptionReturn true if the buffer wrote data. NO-OP for non-secure channel.- Returns:
- Always returns falsefor non-secure channel
- Throws:
- java.io.IOException- Never for non-secure channel
 
 - 
checkInterruptStatusprotected void checkInterruptStatus() throws java.io.IOExceptionThis 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.- Throws:
- java.io.IOException- If the current thread was interrupted
 
 - 
setAppReadBufHandlerpublic void setAppReadBufHandler(ApplicationBufferHandler handler) 
 - 
getAppReadBufHandlerprotected ApplicationBufferHandler getAppReadBufHandler() 
 
- 
 
-