org.apache.tomcat.util.net

## Class Nio2Channel

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

- All Implemented Interfaces:
    Closeable, AutoCloseable, AsynchronousByteChannel, AsynchronousChannel, Channel
  Direct Known Subclasses:
    SecureNio2Channel

      public class Nio2Channel
      extends Object
      implements 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 and Description
    --- | ---
    `protected SocketBufferHandler` | `bufHandler`
    `protected static ByteBuffer` | `emptyBuf`
    `protected AsynchronousSocketChannel` | `sc`
    `protected SocketWrapperBase<Nio2Channel>` | `socket`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `Nio2Channel(SocketBufferHandler bufHandler)`

  - ### Method Summary

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

    - #### sc

          protected AsynchronousSocketChannel sc

    - #### socket

          protected SocketWrapperBase<Nio2Channel> socket

    - #### bufHandler

          protected final SocketBufferHandler bufHandler

  - ### Constructor Detail

    - #### Nio2Channel

          public Nio2Channel(SocketBufferHandler bufHandler)

  - ### Method Detail

    - #### reset

          public void reset(AsynchronousSocketChannel channel,
                   SocketWrapperBase<Nio2Channel> socket)
                     throws IOException

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

    - #### free

          public void free()

      Free the channel memory

    - #### getSocket

          public SocketWrapperBase<Nio2Channel> getSocket()

    - #### 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 `AsynchronousChannel`
      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

    - #### getBufHandler

          public SocketBufferHandler getBufHandler()

    - #### getIOChannel

          public AsynchronousSocketChannel getIOChannel()

    - #### isClosing

          public boolean isClosing()

    - #### isHandshakeComplete

          public boolean isHandshakeComplete()

    - #### handshake

          public int handshake()
                        throws IOException

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

    - #### toString

          public String toString()

      Overrides:
        `toString` in class `Object`

    - #### read

          public Future<Integer> read(ByteBuffer dst)

      Specified by:
        `read` in interface `AsynchronousByteChannel`

    - #### read

          public <A> void read(ByteBuffer dst,
                      A attachment,
                      CompletionHandler<Integer,? super A> handler)

      Specified by:
        `read` in interface `AsynchronousByteChannel`

    - #### read

          public <A> void read(ByteBuffer dst,
                      long timeout,
                      TimeUnit unit,
                      A attachment,
                      CompletionHandler<Integer,? super A> handler)

    - #### read

          public <A> void read(ByteBuffer[] dsts,
                      int offset,
                      int length,
                      long timeout,
                      TimeUnit unit,
                      A attachment,
                      CompletionHandler<Long,? super A> handler)

    - #### write

          public Future<Integer> write(ByteBuffer src)

      Specified by:
        `write` in interface `AsynchronousByteChannel`

    - #### write

          public <A> void write(ByteBuffer src,
                       A attachment,
                       CompletionHandler<Integer,? super A> handler)

      Specified by:
        `write` in interface `AsynchronousByteChannel`

    - #### write

          public <A> void write(ByteBuffer src,
                       long timeout,
                       TimeUnit unit,
                       A attachment,
                       CompletionHandler<Integer,? super A> handler)

    - #### write

          public <A> void write(ByteBuffer[] srcs,
                       int offset,
                       int length,
                       long timeout,
                       TimeUnit unit,
                       A attachment,
                       CompletionHandler<Long,? super A> handler)

    - #### flush

          public Future<Boolean> flush()

    - #### setAppReadBufHandler

          public void setAppReadBufHandler(ApplicationBufferHandler handler)

    - #### getAppReadBufHandler

          protected ApplicationBufferHandler getAppReadBufHandler()

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