# 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
Description
`protected final SocketBufferHandler`
`bufHandler`
`protected static final ByteBuffer`
`emptyBuf`
`protected AsynchronousSocketChannel`
`sc`
`protected SocketWrapperBase<Nio2Channel>`
`socketWrapper`

## Constructor Summary

Constructors
Constructor
Description
`Nio2Channel(SocketBufferHandler bufHandler)`

## Method Summary

Modifier and Type
Method
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()`
`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, long timeout, TimeUnit unit, A attachment, CompletionHandler<Integer, ? super A> handler)`
`<A> void`
`read(ByteBuffer dst, A attachment, CompletionHandler<Integer, ? super A> handler)`
`void`
`reset(AsynchronousSocketChannel channel, SocketWrapperBase<Nio2Channel> socketWrapper)`
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, long timeout, TimeUnit unit, A attachment, CompletionHandler<Integer, ? super A> handler)`
`<A> void`
`write(ByteBuffer src, A attachment, CompletionHandler<Integer, ? super A> handler)`

### Methods inherited from class Object

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

## Field Details

### emptyBuf

protected static final ByteBuffer emptyBuf

### bufHandler

protected final SocketBufferHandler bufHandler

### sc

protected AsynchronousSocketChannel sc

### socketWrapper

protected SocketWrapperBase<Nio2Channel> socketWrapper

## Constructor Details

### Nio2Channel

public Nio2Channel(SocketBufferHandler bufHandler)

## Method Details

### reset

public void reset(AsynchronousSocketChannel channel, SocketWrapperBase<Nio2Channel> socketWrapper) throws 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:
  `IOException` - If a problem was encountered resetting the channel

### free

public void free()
Free the channel memory

### close

public void close() throws IOException
Closes this channel.

Specified by:
  `close` in interface `AsynchronousChannel`
Specified by:
  `close` in interface `AutoCloseable`
Specified by:
  `close` in interface `Channel`
Specified by:
  `close` in interface `Closeable`
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()