Package org.apache.tomcat.util.net

## Class Nio2Channel

- java.lang.Object
- - org.apache.tomcat.util.net.Nio2Channel

- All Implemented Interfaces:
    `java.io.Closeable`, `java.lang.AutoCloseable`, `java.nio.channels.AsynchronousByteChannel`, `java.nio.channels.AsynchronousChannel`, `java.nio.channels.Channel`
  Direct Known Subclasses:
    `SecureNio2Channel`

      public class Nio2Channel
      extends java.lang.Object
      implements java.nio.channels.AsynchronousByteChannel

  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

    Fields
    Modifier and Type | Field | Description
    --- | --- | ---
    `protected SocketBufferHandler` | `bufHandler` |
    `protected static java.nio.ByteBuffer` | `emptyBuf` |
    `protected java.nio.channels.AsynchronousSocketChannel` | `sc` |
    `protected SocketWrapperBase<Nio2Channel>` | `socketWrapper` |

  - ### Constructor Summary

    Constructors
    Constructor | Description
    --- | ---
    `Nio2Channel​(SocketBufferHandler bufHandler)` |

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method | Description
    --- | --- | ---
    `void` | `close()` | Closes this channel.
    `void` | `close​(boolean force)` | Close the connection.
    `java.util.concurrent.Future<java.lang.Boolean>` | `flush()` |
    `void` | `free()` | Free the channel memory
    `protected ApplicationBufferHandler` | `getAppReadBufHandler()` |
    `SocketBufferHandler` | `getBufHandler()` |
    `java.nio.channels.AsynchronousSocketChannel` | `getIOChannel()` |
    `int` | `handshake()` | 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.
    `java.util.concurrent.Future<java.lang.Integer>` | `read​(java.nio.ByteBuffer dst)` |
    `<A> void` | `read​(java.nio.ByteBuffer[] dsts, int offset, int length, long timeout, java.util.concurrent.TimeUnit unit, A attachment, java.nio.channels.CompletionHandler<java.lang.Long,​? super A> handler)` |
    `<A> void` | `read​(java.nio.ByteBuffer dst, long timeout, java.util.concurrent.TimeUnit unit, A attachment, java.nio.channels.CompletionHandler<java.lang.Integer,​? super A> handler)` |
    `<A> void` | `read​(java.nio.ByteBuffer dst, A attachment, java.nio.channels.CompletionHandler<java.lang.Integer,​? super A> handler)` |
    `void` | `reset​(java.nio.channels.AsynchronousSocketChannel channel, SocketWrapperBase<Nio2Channel> socketWrapper)` | Reset the channel.
    `void` | `setAppReadBufHandler​(ApplicationBufferHandler handler)` |
    `java.lang.String` | `toString()` |
    `java.util.concurrent.Future<java.lang.Integer>` | `write​(java.nio.ByteBuffer src)` |
    `<A> void` | `write​(java.nio.ByteBuffer[] srcs, int offset, int length, long timeout, java.util.concurrent.TimeUnit unit, A attachment, java.nio.channels.CompletionHandler<java.lang.Long,​? super A> handler)` |
    `<A> void` | `write​(java.nio.ByteBuffer src, long timeout, java.util.concurrent.TimeUnit unit, A attachment, java.nio.channels.CompletionHandler<java.lang.Integer,​? super A> handler)` |
    `<A> void` | `write​(java.nio.ByteBuffer src, A attachment, java.nio.channels.CompletionHandler<java.lang.Integer,​? super A> handler)` |

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

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

- - ### Field Detail

    - #### emptyBuf

          protected static final java.nio.ByteBuffer emptyBuf

    - #### bufHandler

          protected final SocketBufferHandler bufHandler

    - #### sc

          protected java.nio.channels.AsynchronousSocketChannel sc

    - #### socketWrapper

          protected SocketWrapperBase<Nio2Channel> socketWrapper

  - ### Constructor Detail

    - #### Nio2Channel

          public Nio2Channel​(SocketBufferHandler bufHandler)

  - ### Method Detail

    - #### reset

          public void reset​(java.nio.channels.AsynchronousSocketChannel channel,
                            SocketWrapperBase<Nio2Channel> socketWrapper)
                     throws java.io.IOException

      Reset the channel.
      Parameters:
        `channel` - The new async channel to associate with this NIO2 channel
        `socketWrapper` - The new socket to associate with this NIO2 channel
      Throws:
        `java.io.IOException` - If a problem was encountered resetting the channel

    - #### free

          public void free()

      Free the channel memory

    - #### close

          public void close()
                     throws java.io.IOException

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

    - #### close

          public void close​(boolean force)
                     throws java.io.IOException

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

    - #### isOpen

          public boolean isOpen()

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

    - #### getBufHandler

          public SocketBufferHandler getBufHandler()

    - #### getIOChannel

          public java.nio.channels.AsynchronousSocketChannel getIOChannel()

    - #### isClosing

          public boolean isClosing()

    - #### isHandshakeComplete

          public boolean isHandshakeComplete()

    - #### handshake

          public int handshake()
                        throws java.io.IOException

      Performs SSL handshake hence is a no-op for the non-secure implementation.
      Returns:
        Always returns zero
      Throws:
        `java.io.IOException` - Never for non-secure channel

    - #### toString

          public java.lang.String toString()

      Overrides:
        `toString` in class `java.lang.Object`

    - #### read

          public java.util.concurrent.Future<java.lang.Integer> read​(java.nio.ByteBuffer dst)

      Specified by:
        `read` in interface `java.nio.channels.AsynchronousByteChannel`

    - #### read

          public <A> void read​(java.nio.ByteBuffer dst,
                               A attachment,
                               java.nio.channels.CompletionHandler<java.lang.Integer,​? super A> handler)

      Specified by:
        `read` in interface `java.nio.channels.AsynchronousByteChannel`

    - #### read

          public <A> void read​(java.nio.ByteBuffer dst,
                               long timeout,
                               java.util.concurrent.TimeUnit unit,
                               A attachment,
                               java.nio.channels.CompletionHandler<java.lang.Integer,​? super A> handler)

    - #### read

          public <A> void read​(java.nio.ByteBuffer[] dsts,
                               int offset,
                               int length,
                               long timeout,
                               java.util.concurrent.TimeUnit unit,
                               A attachment,
                               java.nio.channels.CompletionHandler<java.lang.Long,​? super A> handler)

    - #### write

          public java.util.concurrent.Future<java.lang.Integer> write​(java.nio.ByteBuffer src)

      Specified by:
        `write` in interface `java.nio.channels.AsynchronousByteChannel`

    - #### write

          public <A> void write​(java.nio.ByteBuffer src,
                                A attachment,
                                java.nio.channels.CompletionHandler<java.lang.Integer,​? super A> handler)

      Specified by:
        `write` in interface `java.nio.channels.AsynchronousByteChannel`

    - #### write

          public <A> void write​(java.nio.ByteBuffer src,
                                long timeout,
                                java.util.concurrent.TimeUnit unit,
                                A attachment,
                                java.nio.channels.CompletionHandler<java.lang.Integer,​? super A> handler)

    - #### write

          public <A> void write​(java.nio.ByteBuffer[] srcs,
                                int offset,
                                int length,
                                long timeout,
                                java.util.concurrent.TimeUnit unit,
                                A attachment,
                                java.nio.channels.CompletionHandler<java.lang.Long,​? super A> handler)

    - #### flush

          public java.util.concurrent.Future<java.lang.Boolean> flush()

    - #### setAppReadBufHandler

          public void setAppReadBufHandler​(ApplicationBufferHandler handler)

    - #### getAppReadBufHandler

          protected ApplicationBufferHandler getAppReadBufHandler()