# Class Nio2Endpoint.Nio2SocketWrapper

java.lang.Object
org.apache.tomcat.util.net.SocketWrapperBase<Nio2Channel>
org.apache.tomcat.util.net.Nio2Endpoint.Nio2SocketWrapper

Enclosing class:
  `Nio2Endpoint`

public static class Nio2Endpoint.Nio2SocketWrapper extends SocketWrapperBase<Nio2Channel>

- ## Nested Class Summary

  ### Nested classes/interfaces inherited from class SocketWrapperBase

  `SocketWrapperBase.BlockingMode, SocketWrapperBase.CompletionCheck, SocketWrapperBase.CompletionHandlerCall, SocketWrapperBase.CompletionState, SocketWrapperBase.OperationState<A>, SocketWrapperBase.VectoredIOCompletionHandler<A>`

- ## Field Summary

  ### Fields inherited from class SocketWrapperBase

  `bufferedWriteSize, closed, COMPLETE_READ, COMPLETE_READ_WITH_COMPLETION, COMPLETE_WRITE, COMPLETE_WRITE_WITH_COMPLETION, localAddr, localName, localPort, nonBlockingWriteBuffer, previousIOException, READ_DATA, readOperation, readPending, remoteAddr, remoteHost, remotePort, sm, sniHostName, socketBufferHandler, writeOperation, writePending`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `Nio2SocketWrapper(Nio2Channel channel, Nio2Endpoint endpoint)`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `boolean`
  `awaitReadComplete(long timeout, TimeUnit unit)`
  If an asynchronous read operation is pending, this method will block until the operation completes, or the specified amount of time has passed.
  `boolean`
  `awaitWriteComplete(long timeout, TimeUnit unit)`
  If an asynchronous write operation is pending, this method will block until the operation completes, or the specified amount of time has passed.
  `SendfileDataBase`
  `createSendfileData(String filename, long pos, long length)`
  Creates a sendfile data object for the specified file.
  `void`
  `doClientAuth(SSLSupport sslSupport)`
  Require the client to perform CLIENT-CERT authentication if it hasn't already done so.
  `protected void`
  `doClose()`
  Perform the actual close.
  `protected void`
  `doWrite(boolean block, ByteBuffer from)`
  Write the contents of the ByteBuffer to the socket.
  `protected void`
  `flushBlocking()`
  Writes all remaining data from the buffers and blocks until the write is complete.
  `protected boolean`
  `flushNonBlocking()`
  Flushes remaining buffered data using a non-blocking write.
  `Nio2Endpoint.SendfileData`
  `getSendfileData()`
  `SSLSupport`
  `getSslSupport()`
  Obtain an SSLSupport instance for this socket.
  `boolean`
  `hasAsyncIO()`
  Allows using NIO2 style read/write.
  `boolean`
  `hasDataToRead()`
  Checks if there is data available to read from the socket.
  `boolean`
  `hasDataToWrite()`
  Checks if there is data waiting to be written to the socket.
  `boolean`
  `hasPerOperationTimeout()`
  Allows indicating if the connector supports per operation timeout.
  `boolean`
  `isReadPending()`
  Allows checking if an asynchronous read operation is currently pending.
  `boolean`
  `isReadyForRead()`
  Checks if the socket is ready for reading.
  `boolean`
  `isReadyForWrite()`
  Checks to see if there are any writes pending and if there are calls `SocketWrapperBase.registerWriteInterest()` to trigger a callback once the pending writes have completed.
  `boolean`
  `isWritePending()`
  Allows checking if an asynchronous write operation is currently pending.
  `boolean`
  `needSemaphores()`
  Allows indicating if the connector needs semaphores.
  `protected <A> SocketWrapperBase<Nio2Channel>.OperationState<A>`
  `newOperationState(boolean read, ByteBuffer[] buffers, int offset, int length, SocketWrapperBase.BlockingMode block, long timeout, TimeUnit unit, A attachment, SocketWrapperBase.CompletionCheck check, CompletionHandler<Long, ? super A> handler, Semaphore semaphore, SocketWrapperBase<Nio2Channel>.VectoredIOCompletionHandler<A> completion)`
  Creates a new operation state for vectored I/O operations.
  `protected void`
  `populateLocalAddr()`
  Populates the cached local IP address from the underlying socket.
  `protected void`
  `populateLocalName()`
  Populates the cached local host name from the underlying socket.
  `protected void`
  `populateLocalPort()`
  Populates the cached local port number from the underlying socket.
  `protected void`
  `populateRemoteAddr()`
  Populates the cached remote IP address from the underlying socket.
  `protected void`
  `populateRemoteHost()`
  Populates the cached remote host name from the underlying socket.
  `protected void`
  `populateRemotePort()`
  Populates the cached remote port number from the underlying socket.
  `SendfileState`
  `processSendfile(SendfileDataBase sendfileData)`
  Starts the sendfile process.
  `int`
  `read(boolean block, byte[] b, int off, int len)`
  Reads data from the socket into a byte array.
  `int`
  `read(boolean block, ByteBuffer to)`
  Reads data from the socket into a ByteBuffer.
  `void`
  `registerReadInterest()`
  Registers interest in read events.
  `void`
  `registerWriteInterest()`
  Registers interest in write events.
  `void`
  `setAppReadBufHandler(ApplicationBufferHandler handler)`
  Sets the application-level read buffer handler.
  `void`
  `setSendfileData(Nio2Endpoint.SendfileData sf)`
  `protected void`
  `writeNonBlocking(byte[] buf, int off, int len)`
  Transfers the data to the socket write buffer (writing that data to the socket if the buffer fills up using a non-blocking write) until either all the data has been transferred and space remains in the socket write buffer or a non-blocking write leaves data in the socket write buffer.
  `protected void`
  `writeNonBlocking(ByteBuffer from)`
  Transfers the data to the socket write buffer (writing that data to the socket if the buffer fills up using a non-blocking write) until either all the data has been transferred and space remains in the socket write buffer or a non-blocking write leaves data in the socket write buffer.
  `protected void`
  `writeNonBlockingInternal(ByteBuffer from)`
  Separate method so it can be re-used by the socket write buffer to write data to the network

  ### Methods inherited from class SocketWrapperBase

  `buffersArrayHasRemaining, canWrite, checkError, close, decrementKeepAlive, doWrite, execute, flush, getCurrentProcessor, getEndpoint, getError, getLocalAddr, getLocalName, getLocalPort, getLock, getNegotiatedProtocol, getReadTimeout, getRemoteAddr, getRemoteHost, getRemotePort, getSniHostName, getSocket, getSocketBufferHandler, getSslSupport, getWriteTimeout, isClosed, isSecure, isUpgraded, populateReadBuffer, populateReadBuffer, processSocket, read, read, read, reset, setCurrentProcessor, setError, setKeepAliveLeft, setNegotiatedProtocol, setReadTimeout, setSecure, setSniHostName, setUpgraded, setWriteTimeout, takeCurrentProcessor, toString, transfer, transfer, unRead, vectoredOperation, write, write, write, write, write, writeBlocking, writeBlocking`

  ### Methods inherited from class Object

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

- ## Constructor Details

  - ### Nio2SocketWrapper

    public Nio2SocketWrapper(Nio2Channel channel, Nio2Endpoint endpoint)

- ## Method Details

  - ### setSendfileData

    public void setSendfileData(Nio2Endpoint.SendfileData sf)

  - ### getSendfileData

    public Nio2Endpoint.SendfileData getSendfileData()

  - ### isReadyForRead

    public boolean isReadyForRead() throws IOException
    Description copied from class: `SocketWrapperBase`
    Checks if the socket is ready for reading.
    Specified by:
      `isReadyForRead` in class `SocketWrapperBase<Nio2Channel>`
    Returns:
      `true` if data is available to read
    Throws:
      `IOException` - If an I/O error occurs

  - ### isReadyForWrite

    public boolean isReadyForWrite()
    Description copied from class: `SocketWrapperBase`
    Checks to see if there are any writes pending and if there are calls `SocketWrapperBase.registerWriteInterest()` to trigger a callback once the pending writes have completed.

    Note: Once this method has returned `false` it MUST NOT be called again until the pending write has completed and the callback has been fired. TODO: Modify `SocketWrapperBase.registerWriteInterest()` so the above restriction is enforced there rather than relying on the caller.

    Overrides:
      `isReadyForWrite` in class `SocketWrapperBase<Nio2Channel>`
    Returns:
      `true` if no writes are pending and data can be written otherwise `false`

  - ### read

    public int read(boolean block, byte[] b, int off, int len) throws IOException
    Description copied from class: `SocketWrapperBase`
    Reads data from the socket into a byte array.
    Specified by:
      `read` in class `SocketWrapperBase<Nio2Channel>`
    Parameters:
      `block` - Whether to block
      `b` - The byte array to read into
      `off` - The offset in the array
      `len` - The number of bytes to read
    Returns:
      the number of bytes read
    Throws:
      `IOException` - If an I/O error occurs

  - ### read

    public int read(boolean block, ByteBuffer to) throws IOException
    Description copied from class: `SocketWrapperBase`
    Reads data from the socket into a ByteBuffer.
    Specified by:
      `read` in class `SocketWrapperBase<Nio2Channel>`
    Parameters:
      `block` - Whether to block
      `to` - The ByteBuffer to read into
    Returns:
      the number of bytes read
    Throws:
      `IOException` - If an I/O error occurs

  - ### doClose

    protected void doClose()
    Description copied from class: `SocketWrapperBase`
    Perform the actual close. The closed atomic boolean guarantees this will be called only once per wrapper.
    Specified by:
      `doClose` in class `SocketWrapperBase<Nio2Channel>`

  - ### hasAsyncIO

    public boolean hasAsyncIO()
    Description copied from class: `SocketWrapperBase`
    Allows using NIO2 style read/write.
    Overrides:
      `hasAsyncIO` in class `SocketWrapperBase<Nio2Channel>`
    Returns:
      `true` if the connector has the capability enabled

  - ### needSemaphores

    public boolean needSemaphores()
    Description copied from class: `SocketWrapperBase`
    Allows indicating if the connector needs semaphores.
    Overrides:
      `needSemaphores` in class `SocketWrapperBase<Nio2Channel>`
    Returns:
      This default implementation always returns `false`

  - ### hasPerOperationTimeout

    public boolean hasPerOperationTimeout()
    Description copied from class: `SocketWrapperBase`
    Allows indicating if the connector supports per operation timeout.
    Overrides:
      `hasPerOperationTimeout` in class `SocketWrapperBase<Nio2Channel>`
    Returns:
      This default implementation always returns `false`

  - ### newOperationState

    protected <A> SocketWrapperBase<Nio2Channel>.OperationState<A> newOperationState(boolean read, ByteBuffer[] buffers, int offset, int length, SocketWrapperBase.BlockingMode block, long timeout, TimeUnit unit, A attachment, SocketWrapperBase.CompletionCheck check, CompletionHandler<Long, ? super A> handler, Semaphore semaphore, SocketWrapperBase<Nio2Channel>.VectoredIOCompletionHandler<A> completion)
    Description copied from class: `SocketWrapperBase`
    Creates a new operation state for vectored I/O operations.
    Specified by:
      `newOperationState` in class `SocketWrapperBase<Nio2Channel>`
    Type Parameters:
      `A` - The attachment type
    Parameters:
      `read` - Whether this is a read operation
      `buffers` - The buffers for the operation
      `offset` - The offset in the buffer array
      `length` - The number of buffers
      `block` - The blocking mode
      `timeout` - The timeout duration
      `unit` - The timeout time unit
      `attachment` - An attachment object
      `check` - The completion check
      `handler` - The completion handler
      `semaphore` - The semaphore for synchronization
      `completion` - The vectored I/O completion handler
    Returns:
      a new operation state instance

  - ### writeNonBlocking

    protected void writeNonBlocking(byte[] buf, int off, int len) throws IOException
    Transfers the data to the socket write buffer (writing that data to the socket if the buffer fills up using a non-blocking write) until either all the data has been transferred and space remains in the socket write buffer or a non-blocking write leaves data in the socket write buffer. After an incomplete write, any data remaining to be transferred to the socket write buffer will be copied to the socket write buffer. If the remaining data is too big for the socket write buffer, the socket write buffer will be filled and the additional data written to the non-blocking write buffer.

    Overridden for NIO2 to enable a gathering write to be used to write all of the remaining data in a single additional write should a non-blocking write leave data in the buffer.

    Overrides:
      `writeNonBlocking` in class `SocketWrapperBase<Nio2Channel>`
    Parameters:
      `buf` - The byte array containing the data to be written
      `off` - The offset within the byte array of the data to be written
      `len` - The length of the data to be written
    Throws:
      `IOException` - If an IO error occurs during the write

  - ### writeNonBlocking

    protected void writeNonBlocking(ByteBuffer from) throws IOException
    Transfers the data to the socket write buffer (writing that data to the socket if the buffer fills up using a non-blocking write) until either all the data has been transferred and space remains in the socket write buffer or a non-blocking write leaves data in the socket write buffer. After an incomplete write, any data remaining to be transferred to the socket write buffer will be copied to the socket write buffer. If the remaining data is too big for the socket write buffer, the socket write buffer will be filled and the additional data written to the non-blocking write buffer.

    Overridden for NIO2 to enable a gathering write to be used to write all of the remaining data in a single additional write should a non-blocking write leave data in the buffer.

    Overrides:
      `writeNonBlocking` in class `SocketWrapperBase<Nio2Channel>`
    Parameters:
      `from` - The ByteBuffer containing the data to be written
    Throws:
      `IOException` - If an IO error occurs during the write

  - ### writeNonBlockingInternal

    protected void writeNonBlockingInternal(ByteBuffer from) throws IOException
    Separate method so it can be re-used by the socket write buffer to write data to the network

    Overridden for NIO2 to enable a gathering write to be used to write all of the remaining data in a single additional write should a non-blocking write leave data in the buffer.

    Overrides:
      `writeNonBlockingInternal` in class `SocketWrapperBase<Nio2Channel>`
    Parameters:
      `from` - The ByteBuffer containing the data to be written
    Throws:
      `IOException` - If an IO error occurs during the write

  - ### doWrite

    protected void doWrite(boolean block, ByteBuffer from) throws IOException
    Description copied from class: `SocketWrapperBase`
    Write the contents of the ByteBuffer to the socket. For blocking writes either then entire contents of the buffer will be written or an IOException will be thrown. Partial blocking writes will not occur.
    Specified by:
      `doWrite` in class `SocketWrapperBase<Nio2Channel>`
    Parameters:
      `block` - Ignored since this method is only called in the blocking case
      `from` - the ByteBuffer containing the data to be written
    Throws:
      `IOException` - If an I/O error such as a timeout occurs during the write

  - ### flushBlocking

    protected void flushBlocking() throws IOException
    Description copied from class: `SocketWrapperBase`
    Writes all remaining data from the buffers and blocks until the write is complete.
    Overrides:
      `flushBlocking` in class `SocketWrapperBase<Nio2Channel>`
    Throws:
      `IOException` - If an IO error occurs during the write

  - ### flushNonBlocking

    protected boolean flushNonBlocking() throws IOException
    Description copied from class: `SocketWrapperBase`
    Flushes remaining buffered data using a non-blocking write.
    Specified by:
      `flushNonBlocking` in class `SocketWrapperBase<Nio2Channel>`
    Returns:
      `true` if data remains to be flushed after this method completes, otherwise `false`
    Throws:
      `IOException` - If an IO error occurs during the write

  - ### hasDataToRead

    public boolean hasDataToRead()
    Description copied from class: `SocketWrapperBase`
    Checks if there is data available to read from the socket.
    Overrides:
      `hasDataToRead` in class `SocketWrapperBase<Nio2Channel>`
    Returns:
      `true` since it is always safe to attempt a read

  - ### hasDataToWrite

    public boolean hasDataToWrite()
    Description copied from class: `SocketWrapperBase`
    Checks if there is data waiting to be written to the socket.
    Overrides:
      `hasDataToWrite` in class `SocketWrapperBase<Nio2Channel>`
    Returns:
      `true` if there is pending write data

  - ### isReadPending

    public boolean isReadPending()
    Description copied from class: `SocketWrapperBase`
    Allows checking if an asynchronous read operation is currently pending.
    Overrides:
      `isReadPending` in class `SocketWrapperBase<Nio2Channel>`
    Returns:
      `true` if the endpoint supports asynchronous IO and a read operation is being processed asynchronously

  - ### isWritePending

    public boolean isWritePending()
    Description copied from class: `SocketWrapperBase`
    Allows checking if an asynchronous write operation is currently pending.
    Overrides:
      `isWritePending` in class `SocketWrapperBase<Nio2Channel>`
    Returns:
      `true` if the endpoint supports asynchronous IO and a write operation is being processed asynchronously

  - ### awaitReadComplete

    public boolean awaitReadComplete(long timeout, TimeUnit unit)
    Description copied from class: `SocketWrapperBase`
    If an asynchronous read operation is pending, this method will block until the operation completes, or the specified amount of time has passed.
    Overrides:
      `awaitReadComplete` in class `SocketWrapperBase<Nio2Channel>`
    Parameters:
      `timeout` - The maximum amount of time to wait
      `unit` - The unit for the timeout
    Returns:
      `true` if the read operation is complete, `false` if the operation is still pending and the specified timeout has passed

  - ### awaitWriteComplete

    public boolean awaitWriteComplete(long timeout, TimeUnit unit)
    Description copied from class: `SocketWrapperBase`
    If an asynchronous write operation is pending, this method will block until the operation completes, or the specified amount of time has passed.
    Overrides:
      `awaitWriteComplete` in class `SocketWrapperBase<Nio2Channel>`
    Parameters:
      `timeout` - The maximum amount of time to wait
      `unit` - The unit for the timeout
    Returns:
      `true` if the read operation is complete, `false` if the operation is still pending and the specified timeout has passed

  - ### registerReadInterest

    public void registerReadInterest()
    Description copied from class: `SocketWrapperBase`
    Registers interest in read events.
    Specified by:
      `registerReadInterest` in class `SocketWrapperBase<Nio2Channel>`

  - ### registerWriteInterest

    public void registerWriteInterest()
    Description copied from class: `SocketWrapperBase`
    Registers interest in write events.
    Specified by:
      `registerWriteInterest` in class `SocketWrapperBase<Nio2Channel>`

  - ### createSendfileData

    public SendfileDataBase createSendfileData(String filename, long pos, long length)
    Description copied from class: `SocketWrapperBase`
    Creates a sendfile data object for the specified file.
    Specified by:
      `createSendfileData` in class `SocketWrapperBase<Nio2Channel>`
    Parameters:
      `filename` - The file to send
      `pos` - The starting position in the file
      `length` - The number of bytes to send
    Returns:
      a new sendfile data object

  - ### processSendfile

    public SendfileState processSendfile(SendfileDataBase sendfileData)
    Description copied from class: `SocketWrapperBase`
    Starts the sendfile process. It is expected that if the sendfile process does not complete during this call and does not report an error, that the caller will not add the socket to the poller (or equivalent). That is the responsibility of this method.
    Specified by:
      `processSendfile` in class `SocketWrapperBase<Nio2Channel>`
    Parameters:
      `sendfileData` - Data representing the file to send
    Returns:
      The state of the sendfile process after the first write.

  - ### populateRemoteAddr

    protected void populateRemoteAddr()
    Description copied from class: `SocketWrapperBase`
    Populates the cached remote IP address from the underlying socket.
    Specified by:
      `populateRemoteAddr` in class `SocketWrapperBase<Nio2Channel>`

  - ### populateRemoteHost

    protected void populateRemoteHost()
    Description copied from class: `SocketWrapperBase`
    Populates the cached remote host name from the underlying socket.
    Specified by:
      `populateRemoteHost` in class `SocketWrapperBase<Nio2Channel>`

  - ### populateRemotePort

    protected void populateRemotePort()
    Description copied from class: `SocketWrapperBase`
    Populates the cached remote port number from the underlying socket.
    Specified by:
      `populateRemotePort` in class `SocketWrapperBase<Nio2Channel>`

  - ### populateLocalName

    protected void populateLocalName()
    Description copied from class: `SocketWrapperBase`
    Populates the cached local host name from the underlying socket.
    Specified by:
      `populateLocalName` in class `SocketWrapperBase<Nio2Channel>`

  - ### populateLocalAddr

    protected void populateLocalAddr()
    Description copied from class: `SocketWrapperBase`
    Populates the cached local IP address from the underlying socket.
    Specified by:
      `populateLocalAddr` in class `SocketWrapperBase<Nio2Channel>`

  - ### populateLocalPort

    protected void populateLocalPort()
    Description copied from class: `SocketWrapperBase`
    Populates the cached local port number from the underlying socket.
    Specified by:
      `populateLocalPort` in class `SocketWrapperBase<Nio2Channel>`

  - ### getSslSupport

    public SSLSupport getSslSupport()
    Description copied from class: `SocketWrapperBase`
    Obtain an SSLSupport instance for this socket.
    Specified by:
      `getSslSupport` in class `SocketWrapperBase<Nio2Channel>`
    Returns:
      An SSLSupport instance for this socket.

  - ### doClientAuth

    public void doClientAuth(SSLSupport sslSupport) throws IOException
    Description copied from class: `SocketWrapperBase`
    Require the client to perform CLIENT-CERT authentication if it hasn't already done so.
    Specified by:
      `doClientAuth` in class `SocketWrapperBase<Nio2Channel>`
    Parameters:
      `sslSupport` - The SSL/TLS support instance currently being used by the connection that may need updating after the client authentication
    Throws:
      `IOException` - If authentication is required then there will be I/O with the client and this exception will be thrown if that goes wrong

  - ### setAppReadBufHandler

    public void setAppReadBufHandler(ApplicationBufferHandler handler)
    Description copied from class: `SocketWrapperBase`
    Sets the application-level read buffer handler.
    Specified by:
      `setAppReadBufHandler` in class `SocketWrapperBase<Nio2Channel>`
    Parameters:
      `handler` - The application buffer handler