org.apache.tomcat.util.net

## 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.
  Version:
    1.0

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected SocketBufferHandler` | `bufHandler`
    `protected static ByteBuffer` | `emptyBuf`
    `protected NioEndpoint.Poller` | `poller`
    `protected SocketChannel` | `sc`
    `protected static StringManager` | `sm`
    `protected SocketWrapperBase<NioChannel>` | `socketWrapper`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `NioChannel(SocketChannel channel, SocketBufferHandler bufHandler)`

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `protected void` | `checkInterruptStatus()` This method should be used to check the interrupt status before attempting a write.
    `void` | `close()` Closes this channel.
    `void` | `close(boolean force)` Close the connection.
    `boolean` | `flush(boolean block, Selector s, long timeout)` Returns true if the network buffer has been flushed out and is empty.
    `boolean` | `flushOutbound()` Return true if the buffer wrote data.
    `void` | `free()` Free the channel memory
    `protected ApplicationBufferHandler` | `getAppReadBufHandler()`
    `Object` | `getAttachment()`
    `SocketBufferHandler` | `getBufHandler()`
    `SocketChannel` | `getIOChannel()`
    `int` | `getOutboundRemaining()`
    `NioEndpoint.Poller` | `getPoller()`
    `int` | `handshake(boolean read, boolean write)` Performs SSL handshake hence is a no-op for the non-secure implementation.
    `boolean` | `isClosing()`
    `boolean` | `isHandshakeComplete()`
    `boolean` | `isOpen()` Tells whether or not this channel is open.
    `int` | `read(ByteBuffer dst)` Reads a sequence of bytes from this channel into the given buffer.
    `long` | `read(ByteBuffer[] dsts)`
    `long` | `read(ByteBuffer[] dsts, int offset, int length)`
    `void` | `reset()` Reset the channel
    `void` | `setAppReadBufHandler(ApplicationBufferHandler handler)`
    `void` | `setIOChannel(SocketChannel IOChannel)`
    `void` | `setPoller(NioEndpoint.Poller poller)`
    `String` | `toString()`
    `int` | `write(ByteBuffer src)` Writes a sequence of bytes to this channel from the given buffer.
    `long` | `write(ByteBuffer[] srcs)`
    `long` | `write(ByteBuffer[] srcs, int offset, int length)`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- - ### Field Detail

    - #### sm

          protected static final StringManager sm

    - #### emptyBuf

          protected static final ByteBuffer emptyBuf

    - #### sc

          protected SocketChannel sc

    - #### socketWrapper

          protected SocketWrapperBase<NioChannel> socketWrapper

    - #### bufHandler

          protected final SocketBufferHandler bufHandler

    - #### poller

          protected NioEndpoint.Poller poller

  - ### Constructor Detail

    - #### NioChannel

          public NioChannel(SocketChannel channel,
                    SocketBufferHandler bufHandler)

  - ### Method Detail

    - #### reset

          public void reset()
                     throws IOException

      Reset the channel
      Throws:
        `IOException` - If a problem was encountered resetting the channel

    - #### free

          public void free()

      Free the channel memory

    - #### flush

          public boolean flush(boolean block,
                      Selector s,
                      long timeout)
                        throws IOException

      Returns 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 `true` since there is no network buffer in the regular channel
      Throws:
        `IOException` - Never for non-secure channel

    - #### close

          public void close()
                     throws IOException

      Closes this channel.
      Specified by:
        `close` in interface `Closeable`
      Specified by:
        `close` in interface `AutoCloseable`
      Specified by:
        `close` in interface `Channel`
      Throws:
        `IOException` - If an I/O error occurs

    - #### close

          public void close(boolean force)
                     throws IOException

      Close the connection.
      Parameters:
        `force` - Should the underlying socket be forcibly closed?
      Throws:
        `IOException` - If closing the secure channel fails.

    - #### isOpen

          public boolean isOpen()

      Tells whether or not this channel is open.
      Specified by:
        `isOpen` in interface `Channel`
      Returns:
        `true` if, and only if, this channel is open

    - #### write

          public int write(ByteBuffer src)
                    throws IOException

      Writes a sequence of bytes to this channel from the given buffer.
      Specified by:
        `write` in interface `WritableByteChannel`
      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

          public long write(ByteBuffer[] srcs)
                     throws IOException

      Specified by:
        `write` in interface `GatheringByteChannel`
      Throws:
        `IOException`

    - #### write

          public long write(ByteBuffer[] srcs,
                   int offset,
                   int length)
                     throws IOException

      Specified by:
        `write` in interface `GatheringByteChannel`
      Throws:
        `IOException`

    - #### read

          public int read(ByteBuffer dst)
                   throws IOException

      Reads a sequence of bytes from this channel into the given buffer.
      Specified by:
        `read` in interface `ReadableByteChannel`
      Parameters:
        `dst` - The buffer into which bytes are to be transferred
      Returns:
        The number of bytes read, possibly zero, or `-1` if the channel has reached end-of-stream
      Throws:
        `IOException` - If some other I/O error occurs

    - #### read

          public long read(ByteBuffer[] dsts)
                    throws IOException

      Specified by:
        `read` in interface `ScatteringByteChannel`
      Throws:
        `IOException`

    - #### read

          public long read(ByteBuffer[] dsts,
                  int offset,
                  int length)
                    throws IOException

      Specified by:
        `read` in interface `ScatteringByteChannel`
      Throws:
        `IOException`

    - #### getAttachment

          public Object getAttachment()

    - #### getBufHandler

          public SocketBufferHandler getBufHandler()

    - #### getPoller

          public NioEndpoint.Poller getPoller()

    - #### getIOChannel

          public SocketChannel getIOChannel()

    - #### isClosing

          public boolean isClosing()

    - #### isHandshakeComplete

          public boolean isHandshakeComplete()

    - #### handshake

          public int handshake(boolean read,
                      boolean write)
                        throws IOException

      Performs 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:
        `IOException` - Never for non-secure channel

    - #### setPoller

          public void setPoller(NioEndpoint.Poller poller)

    - #### setIOChannel

          public void setIOChannel(SocketChannel IOChannel)

    - #### toString

          public String toString()

      Overrides:
        `toString` in class `Object`

    - #### getOutboundRemaining

          public int getOutboundRemaining()

    - #### flushOutbound

          public boolean flushOutbound()
                                throws IOException

      Return true if the buffer wrote data. NO-OP for non-secure channel.
      Returns:
        Always returns `false` for non-secure channel
      Throws:
        `IOException` - Never for non-secure channel

    - #### checkInterruptStatus

          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.
      Throws:
        `IOException` - If the current thread was interrupted

    - #### setAppReadBufHandler

          public void setAppReadBufHandler(ApplicationBufferHandler handler)

    - #### getAppReadBufHandler

          protected ApplicationBufferHandler getAppReadBufHandler()

Copyright © 2000-2021 Apache Software Foundation. All Rights Reserved.