Class ByteBufferOutputStream

java.lang.Object
java.io.OutputStream
org.jdrupes.httpcodec.util.ByteBufferOutputStream
All Implemented Interfaces:
Closeable, Flushable, AutoCloseable

An OutputStream that is backed by a ByteBuffer assigned to the stream.

If the buffer becomes full, one or more buffers are allocated as intermediate storage. Their content is copied to the next assigned buffer(s).

While writing to this stream, remaining() should be checked regularly and the production of data should be suspended if possible when no more space is left to avoid the usage of intermediate storage.

  • Constructor Details

    • ByteBufferOutputStream

      Creates a new instance with an unset overflow buffer size.
    • ByteBufferOutputStream

      public ByteBufferOutputStream(int overflowBufferSize)
      Creates a new instance with the given overflow buffer size.
      Parameters:
      overflowBufferSize - the overflow buffer size to use
  • Method Details

    • overflowBufferSize

      public int overflowBufferSize()
      Returns the size of the buffers that will be allocated as overflow buffers.
      Returns:
      the allocation size for the overflow buffers
    • setOverflowBufferSize

      public void setOverflowBufferSize(int overflowBufferSize)
      The size of the buffers that are allocated if the assigned buffer overflows.

      If not set, buffers are allocated with one fourth of the size of the assigned buffer or 4096 if no buffer has been assigned yet.

      Parameters:
      overflowBufferSize - the size
    • clear

      public void clear()
      Clear any buffered data and prepares the buffer for reuse.
    • assignBuffer

      public void assignBuffer(ByteBuffer buffer)
      Assign a new buffer to this output stream.

      If the previously used buffer had become full and intermediate storage was allocated, the data from the intermediate storage is copied to the new buffer first. Then, the new buffer is used for all subsequent write operations.

      Parameters:
      buffer - the buffer to use
    • write

      public void write(int data) throws IOException
      Specified by:
      write in class OutputStream
      Throws:
      IOException
    • write

      public void write(byte[] data, int offset, int length) throws IOException
      Overrides:
      write in class OutputStream
      Throws:
      IOException
    • write

      public void write(ByteBuffer data)
      Copies the data from the given buffer to this output stream.
      Parameters:
      data - the buffer
    • write

      public void write(ByteBuffer data, int length)
      Copies length bytes from the given buffer to this output stream.
      Parameters:
      data - the buffer
      length - the number of bytes to copy
    • remaining

      public long remaining()
      Returns the number of bytes remaining in the assigned buffer.

      A negative value indicates that the assigned buffer is full and one or more overflow buffers are being used. The absolute value of the negative number is the number of bytes in the overflow buffer(s).

      Returns:
      the bytes remaining or buffered (negative value)
    • close

      public void close() throws IOException
      Does not have any effect.

      May be called for consistent usage of the output stream.

      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class OutputStream
      Throws:
      IOException - if there is still data in intermediate storage
      See Also:
    • bytesWritten

      public long bytesWritten()
      The number of bytes written to this output stream.
      Returns:
      the number of bytes written