SocketBufferHandler (Apache Tomcat 9.0.120 API Documentation)
Description: declaration: package: org.apache.tomcat.util.net, class: SocketBufferHandler

# Class SocketBufferHandler

java.lang.Object
org.apache.tomcat.util.net.SocketBufferHandler

public class SocketBufferHandler extends Object
Manages read and write `ByteBuffer` instances for a socket connection, handling buffer state transitions between read and write modes.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `SocketBufferHandler(int readBufferSize, int writeBufferSize, boolean direct)`
  Creates a new SocketBufferHandler with the specified buffer sizes.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `configureReadBufferForRead()`
  Switches the read buffer into read mode.
  `void`
  `configureReadBufferForWrite()`
  Switches the read buffer into write mode.
  `void`
  `configureWriteBufferForRead()`
  Switches the write buffer into read mode.
  `void`
  `configureWriteBufferForWrite()`
  Switches the write buffer into write mode.
  `void`
  `expand(int newSize)`
  Expands both read and write buffers to the specified size.
  `void`
  `free()`
  Releases native resources for direct buffers, if applicable.
  `ByteBuffer`
  `getReadBuffer()`
  Returns the read buffer.
  `ByteBuffer`
  `getWriteBuffer()`
  Returns the write buffer.
  `boolean`
  `isReadBufferEmpty()`
  Checks whether the read buffer contains any data.
  `boolean`
  `isWriteBufferEmpty()`
  Checks whether the write buffer contains any data.
  `boolean`
  `isWriteBufferWritable()`
  Checks whether the write buffer has space for additional data.
  `void`
  `reset()`
  Resets both read and write buffers to their initial empty state.
  `void`
  `unReadReadBuffer(ByteBuffer returnedData)`
  Inserts previously read data back into the read buffer so it can be read again.

  ### Methods inherited from class Object

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

- ## Constructor Details

  - ### SocketBufferHandler

    public SocketBufferHandler(int readBufferSize, int writeBufferSize, boolean direct)
    Creates a new SocketBufferHandler with the specified buffer sizes.
    Parameters:
      `readBufferSize` - the size of the read buffer in bytes
      `writeBufferSize` - the size of the write buffer in bytes
      `direct` - whether to allocate direct (off-heap) buffers

- ## Method Details

  - ### configureReadBufferForWrite

    public void configureReadBufferForWrite()
    Switches the read buffer into write mode.

  - ### configureReadBufferForRead

    public void configureReadBufferForRead()
    Switches the read buffer into read mode.

  - ### getReadBuffer

    public ByteBuffer getReadBuffer()
    Returns the read buffer.
    Returns:
      the read buffer

  - ### isReadBufferEmpty

    public boolean isReadBufferEmpty()
    Checks whether the read buffer contains any data.
    Returns:
      `true` if the read buffer is empty

  - ### unReadReadBuffer

    public void unReadReadBuffer(ByteBuffer returnedData)
    Inserts previously read data back into the read buffer so it can be read again.
    Parameters:
      `returnedData` - the data to insert back into the buffer
    Throws:
      `BufferOverflowException` - if the buffer cannot accommodate the returned data

  - ### configureWriteBufferForWrite

    public void configureWriteBufferForWrite()
    Switches the write buffer into write mode.

  - ### configureWriteBufferForRead

    public void configureWriteBufferForRead()
    Switches the write buffer into read mode.

  - ### isWriteBufferWritable

    public boolean isWriteBufferWritable()
    Checks whether the write buffer has space for additional data.
    Returns:
      `true` if the write buffer can accept more data

  - ### getWriteBuffer

    public ByteBuffer getWriteBuffer()
    Returns the write buffer.
    Returns:
      the write buffer

  - ### isWriteBufferEmpty

    public boolean isWriteBufferEmpty()
    Checks whether the write buffer contains any data.
    Returns:
      `true` if the write buffer is empty

  - ### reset

    public void reset()
    Resets both read and write buffers to their initial empty state.

  - ### expand

    public void expand(int newSize)
    Expands both read and write buffers to the specified size.
    Parameters:
      `newSize` - the new buffer size in bytes

  - ### free

    public void free()
    Releases native resources for direct buffers, if applicable.