Class NioChannel
java.lang.Object
org.apache.tomcat.util.net.NioChannel
- All Implemented Interfaces:
Closeable, AutoCloseable, ByteChannel, Channel, GatheringByteChannel, ReadableByteChannel, ScatteringByteChannel, WritableByteChannel
- Direct Known Subclasses:
SecureNioChannel
public class NioChannel
extends Object
implements ByteChannel, ScatteringByteChannel, GatheringByteChannel
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
FieldsModifier and TypeFieldDescriptionprotected final SocketBufferHandlerThe buffer handler for socket I/O operations.protected static final ByteBufferEmpty byte buffer used when no data is available.protected SocketChannelThe underlying socket channel.protected static final StringManagerString manager for this class.protected NioEndpoint.NioSocketWrapperThe socket wrapper associated with this channel. -
Constructor Summary
ConstructorsConstructorDescriptionNioChannel(SocketBufferHandler bufHandler) Creates a new NIO channel with the specified buffer handler. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidThis method should be used to check the interrupt status before attempting a write.voidclose()Closes this channel.voidclose(boolean force) Close the connection.booleanReturn true if the buffer wrote data.voidfree()Free the channel memoryprotected ApplicationBufferHandlerReturns the application-level read buffer handler.Returns the buffer handler for this channel.Returns the underlying socket channel.intReturns the number of bytes remaining in the outbound buffer.inthandshake(boolean read, boolean write) Performs SSL handshake hence is a no-op for the non-secure implementation.booleanIndicates whether the channel is in the process of closing.booleanIndicates whether the SSL handshake is complete.booleanisOpen()Tells whether or not this channel is open.intread(ByteBuffer dst) Reads a sequence of bytes from this channel into the given buffer.longread(ByteBuffer[] dsts) longread(ByteBuffer[] dsts, int offset, int length) voidreset(SocketChannel channel, NioEndpoint.NioSocketWrapper socketWrapper) Reset the channelvoidSets the application-level read buffer handler.toString()intwrite(ByteBuffer src) Writes a sequence of bytes to this channel from the given buffer.longwrite(ByteBuffer[] srcs) longwrite(ByteBuffer[] srcs, int offset, int length)
-
Field Details
-
sm
String manager for this class. -
emptyBuf
Empty byte buffer used when no data is available. -
bufHandler
The buffer handler for socket I/O operations. -
sc
The underlying socket channel. -
socketWrapper
The socket wrapper associated with this channel.
-
-
Constructor Details
-
NioChannel
Creates a new NIO channel with the specified buffer handler.- Parameters:
bufHandler- The buffer handler for socket I/O operations
-
-
Method Details
-
reset
public void reset(SocketChannel channel, NioEndpoint.NioSocketWrapper socketWrapper) throws IOException Reset the channel- Parameters:
channel- the socket channelsocketWrapper- the socket wrapper- Throws:
IOException- If a problem was encountered resetting the channel
-
free
public void free()Free the channel memory -
close
Closes this channel.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceChannel- Specified by:
closein interfaceCloseable- Throws:
IOException- If an I/O error occurs
-
close
Close the connection.- Parameters:
force- Should the underlying socket be forcibly closed?- Throws:
IOException- If closing the secure channel fails.
-
isOpen
-
write
Writes a sequence of bytes to this channel from the given buffer.- Specified by:
writein interfaceWritableByteChannel- Parameters:
src- The buffer from which bytes are to be retrieved- Returns:
- The number of bytes written, possibly zero
- Throws:
IOException- If some other I/O error occurs
-
write
- Specified by:
writein interfaceGatheringByteChannel- Throws:
IOException
-
write
- Specified by:
writein interfaceGatheringByteChannel- Throws:
IOException
-
read
Reads a sequence of bytes from this channel into the given buffer.- Specified by:
readin interfaceReadableByteChannel- 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:
IOException- If some other I/O error occurs
-
read
- Specified by:
readin interfaceScatteringByteChannel- Throws:
IOException
-
read
- Specified by:
readin interfaceScatteringByteChannel- Throws:
IOException
-
getBufHandler
Returns the buffer handler for this channel.- Returns:
- the buffer handler
-
getIOChannel
Returns the underlying socket channel.- Returns:
- the socket channel
-
isClosing
public boolean isClosing()Indicates whether the channel is in the process of closing.- Returns:
falsefor non-secure channels
-
isHandshakeComplete
public boolean isHandshakeComplete()Indicates whether the SSL handshake is complete.- Returns:
truefor non-secure channels (no handshake required)
-
handshake
Performs SSL handshake hence is a no-op for the non-secure implementation.- Parameters:
read- Unused in non-secure implementationwrite- Unused in non-secure implementation- Returns:
- Always returns zero
- Throws:
IOException- Never for non-secure channel
-
toString
-
getOutboundRemaining
public int getOutboundRemaining()Returns the number of bytes remaining in the outbound buffer.- Returns:
- Always returns 0 for non-secure channels
-
flushOutbound
Return true if the buffer wrote data. NO-OP for non-secure channel.- Returns:
- Always returns
falsefor non-secure channel - Throws:
IOException- Never for non-secure channel
-
checkInterruptStatus
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.- Throws:
IOException- If the current thread was interrupted
-
setAppReadBufHandler
Sets the application-level read buffer handler.- Parameters:
handler- The application buffer handler
-
getAppReadBufHandler
Returns the application-level read buffer handler.- Returns:
- the application buffer handler
-