# Class SecureNio2Channel

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

All Implemented Interfaces:
  `Closeable`, `AutoCloseable`, `AsynchronousByteChannel`, `AsynchronousChannel`, `Channel`

public class SecureNio2Channel extends Nio2Channel
Implementation of a secure socket channel for NIO2.

## Field Summary

Fields
Modifier and Type
Field
Description
`protected boolean`
`closed`
`protected boolean`
`closing`
`protected final Nio2Endpoint`
`endpoint`
`protected ByteBuffer`
`netInBuffer`
`protected ByteBuffer`
`netOutBuffer`
`protected boolean`
`sniComplete`
`protected SSLEngine`
`sslEngine`

### Fields inherited from class org.apache.tomcat.util.net.Nio2Channel

`bufHandler, emptyBuf, sc, socketWrapper`

## Constructor Summary

Constructors
Constructor
Description
`SecureNio2Channel(SocketBufferHandler bufHandler, Nio2Endpoint endpoint)`

## Method Summary

Modifier and Type
Method
Description
`void`
`close()`
Sends an SSL close message, will not physically close the connection here.
To close the connection, you could do something like
`void`
`close(boolean force)`
Close the connection.
`Future<Boolean>`
`flush()`
Flush the channel.
`void`
`free()`
Free the channel memory
`ByteBuffer`
`getEmptyBuf()`
`SSLEngine`
`getSslEngine()`
`SSLSupport`
`getSSLSupport()`
`int`
`handshake()`
Performs SSL handshake, non blocking, but performs NEED_TASK on the same thread.
`protected int`
`handshakeInternal(boolean async)`
`protected SSLEngineResult`
`handshakeUnwrap()`
Perform handshake unwrap
`protected SSLEngineResult`
`handshakeWrap()`
Performs the WRAP function
`boolean`
`isClosing()`
`boolean`
`isHandshakeComplete()`
`Future<Integer>`
`read(ByteBuffer dst)`
Reads a sequence of bytes from this channel into the given buffer.
`<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)`
`void`
`rehandshake()`
Force a blocking handshake to take place for this key.
`void`
`reset(AsynchronousSocketChannel channel, SocketWrapperBase<Nio2Channel> socket)`
Reset the channel.
`protected SSLEngineResult.HandshakeStatus`
`tasks()`
Executes all the tasks needed on the same thread.
`Future<Integer>`
`write(ByteBuffer src)`
Writes a sequence of bytes to this channel from the given buffer.
`<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)`

### Methods inherited from class org.apache.tomcat.util.net.Nio2Channel

`getAppReadBufHandler, getBufHandler, getIOChannel, isOpen, read, setAppReadBufHandler, toString, write`

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

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

## Field Details

### endpoint

protected final Nio2Endpoint endpoint

### netInBuffer

protected ByteBuffer netInBuffer

### netOutBuffer

protected ByteBuffer netOutBuffer

### sslEngine

protected SSLEngine sslEngine

### sniComplete

protected volatile boolean sniComplete

### closed

protected boolean closed

### closing

protected boolean closing

## Constructor Details

### SecureNio2Channel

public SecureNio2Channel(SocketBufferHandler bufHandler, Nio2Endpoint endpoint)

## Method Details

### reset

public void reset(AsynchronousSocketChannel channel, SocketWrapperBase<Nio2Channel> socket) throws IOException
Description copied from class: `Nio2Channel`
Reset the channel.

Overrides:
  `reset` in class `Nio2Channel`
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()
Description copied from class: `Nio2Channel`
Free the channel memory

Overrides:
  `free` in class `Nio2Channel`

### flush

public Future<Boolean> flush()
Flush the channel.

Overrides:
  `flush` in class `Nio2Channel`
Returns:
  `true` if the network buffer has been flushed out and is empty else `false` (as a future)

### handshake

public int handshake() throws IOException
Performs SSL handshake, non blocking, but performs NEED_TASK on the same thread. Hence, you should never call this method using your Acceptor thread, as you would slow down your system significantly.

The return for this operation is 0 if the handshake is complete and a positive value if it is not complete. In the event of a positive value coming back, the appropriate read/write will already have been called with an appropriate CompletionHandler.

Overrides:
  `handshake` in class `Nio2Channel`
Returns:
  0 if hand shake is complete, negative if the socket needs to close and positive if the handshake is incomplete
Throws:
  `IOException` - if an error occurs during the handshake

### handshakeInternal

protected int handshakeInternal(boolean async) throws IOException

Throws:
  `IOException`

### rehandshake

public void rehandshake() throws IOException
Force a blocking handshake to take place for this key. This requires that both network and application buffers have been emptied out prior to this call taking place, or a IOException will be thrown.

Throws:
  `IOException` - - if an IO exception occurs or if application or network buffers contain data
  `SocketTimeoutException` - - if a socket operation timed out

### tasks

protected SSLEngineResult.HandshakeStatus tasks()
Executes all the tasks needed on the same thread.

Returns:
  the status

### handshakeWrap

protected SSLEngineResult handshakeWrap() throws IOException
Performs the WRAP function

Returns:
  the result
Throws:
  `IOException` - An IO error occurred

### handshakeUnwrap

protected SSLEngineResult handshakeUnwrap() throws IOException
Perform handshake unwrap

Returns:
  the result
Throws:
  `IOException` - An IO error occurred

### getSSLSupport

public SSLSupport getSSLSupport()

### close

public void close() throws IOException
Sends an SSL close message, will not physically close the connection here.
To close the connection, you could do something like

       close();
       while (isOpen() && !myTimeoutFunction()) Thread.sleep(25);
       if ( isOpen() ) close(true); //forces a close if you timed out

Specified by:
  `close` in interface `AsynchronousChannel`
Specified by:
  `close` in interface `AutoCloseable`
Specified by:
  `close` in interface `Channel`
Specified by:
  `close` in interface `Closeable`
Overrides:
  `close` in class `Nio2Channel`
Throws:
  `IOException` - if an I/O error occurs
  `IOException` - if there is data on the outgoing network buffer and we are unable to flush it

### close

public void close(boolean force) throws IOException
Description copied from class: `Nio2Channel`
Close the connection.

Overrides:
  `close` in class `Nio2Channel`
Parameters:
  `force` - Should the underlying socket be forcibly closed?
Throws:
  `IOException` - If closing the secure channel fails.

### read

public Future<Integer> read(ByteBuffer dst)
Reads a sequence of bytes from this channel into the given buffer.

Specified by:
  `read` in interface `AsynchronousByteChannel`
Overrides:
  `read` in class `Nio2Channel`
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:
  `IllegalStateException` - if the handshake was not completed

### write

public Future<Integer> write(ByteBuffer src)
Writes a sequence of bytes to this channel from the given buffer.

Specified by:
  `write` in interface `AsynchronousByteChannel`
Overrides:
  `write` in class `Nio2Channel`
Parameters:
  `src` - The buffer from which bytes are to be retrieved
Returns:
  The number of bytes written, possibly zero

### read

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

Overrides:
  `read` in class `Nio2Channel`

### read

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

Overrides:
  `read` in class `Nio2Channel`

### write

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

Overrides:
  `write` in class `Nio2Channel`

### write

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

Overrides:
  `write` in class `Nio2Channel`

### isHandshakeComplete

public boolean isHandshakeComplete()

Overrides:
  `isHandshakeComplete` in class `Nio2Channel`

### isClosing

public boolean isClosing()

Overrides:
  `isClosing` in class `Nio2Channel`

### getSslEngine

public SSLEngine getSslEngine()

### getEmptyBuf

public ByteBuffer getEmptyBuf()