Class ByteBufferAccumulator

java.lang.Object
org.eclipse.jetty.io.ByteBufferAccumulator
All Implemented Interfaces:
AutoCloseable

public class ByteBufferAccumulator extends Object implements AutoCloseable
Accumulates data into a list of ByteBuffers which can then be combined into a single buffer or written to an OutputStream. The buffer list automatically grows as data is written to it, the buffers are taken from the supplied ByteBufferPool or freshly allocated if one is not supplied. The method ensureBuffer(int, int) is used to write directly to the last buffer stored in the buffer list, if there is less than a certain amount of space available in that buffer then a new one will be allocated and returned instead.
See Also:
  • Constructor Details

    • ByteBufferAccumulator

      public ByteBufferAccumulator()
    • ByteBufferAccumulator

      public ByteBufferAccumulator(ByteBufferPool bufferPool, boolean direct)
  • Method Details

    • getLength

      public int getLength()
      Get the amount of bytes which have been accumulated. This will add up the remaining of each buffer in the accumulator.
      Returns:
      the total length of the content in the accumulator.
    • ensureBuffer

      public RetainableByteBuffer ensureBuffer(int minAllocationSize)
      Get the last buffer of the accumulator, this can be written to directly to avoid copying into the accumulator.
      Parameters:
      minAllocationSize - new buffers will be allocated to have at least this size.
      Returns:
      a buffer with at least minSize space to write into.
    • ensureBuffer

      public RetainableByteBuffer ensureBuffer(int minSize, int minAllocationSize)
      Get the last buffer of the accumulator, this can be written to directly to avoid copying into the accumulator.
      Parameters:
      minSize - the smallest amount of remaining space before a new buffer is allocated.
      minAllocationSize - new buffers will be allocated to have at least this size.
      Returns:
      a buffer with at least minSize space to write into.
    • copyBytes

      public void copyBytes(byte[] buf, int offset, int length)
    • copyBuffer

      public void copyBuffer(ByteBuffer source)
    • takeRetainableByteBuffer

      public RetainableByteBuffer takeRetainableByteBuffer()
      Take the combined buffer containing all content written to the accumulator. The caller is responsible for releasing this RetainableByteBuffer.
      Returns:
      a buffer containing all content written to the accumulator.
      See Also:
    • takeByteBuffer

      public ByteBuffer takeByteBuffer()
    • toRetainableByteBuffer

      public RetainableByteBuffer toRetainableByteBuffer()
      Take the combined buffer containing all content written to the accumulator. The returned buffer is still contained within the accumulator and will be released when the accumulator is closed.
      Returns:
      a buffer containing all content written to the accumulator.
      See Also:
    • toByteArray

      public byte[] toByteArray()
      Returns:
      a newly allocated byte array containing all content written into the accumulator.
    • writeTo

      public void writeTo(ByteBuffer byteBuffer)
    • writeTo

      public void writeTo(OutputStream out) throws IOException
      Throws:
      IOException
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable