Class ByteBufferAggregator

java.lang.Object
org.eclipse.jetty.io.ByteBufferAggregator

public class ByteBufferAggregator extends Object

Aggregates data into a single ByteBuffer of a specified maximum size.

The buffer automatically grows as data is written to it, up until it reaches the specified maximum size. Once the buffer is full, the aggregator will not aggregate any more bytes until its buffer is taken out, after which a new aggregate/take buffer cycle can start.

The buffers are taken from the supplied ByteBufferPool or freshly allocated if one is not supplied.

  • Constructor Details

    • ByteBufferAggregator

      public ByteBufferAggregator(ByteBufferPool bufferPool, boolean direct, int startSize, int maxSize)
      Creates a ByteBuffer aggregator.
      Parameters:
      bufferPool - The ByteBufferPool from which to acquire the buffers
      direct - whether to get direct buffers
      startSize - the starting size of the buffer
      maxSize - the maximum size of the buffer which must be greater than startSize
  • Method Details

    • length

      public int length()
      Get the currently aggregated length.
      Returns:
      The current total aggregated bytes.
    • aggregate

      public boolean aggregate(ByteBuffer buffer)
      Aggregates the given ByteBuffer. This copies bytes up to the specified maximum size, at which time this method returns true and takeRetainableByteBuffer() must be called for this method to accept aggregating again.
      Parameters:
      buffer - the buffer to copy into this aggregator; its position is updated according to the number of aggregated bytes
      Returns:
      true if the aggregator's buffer is full and should be taken, false otherwise
    • takeRetainableByteBuffer

      public RetainableByteBuffer takeRetainableByteBuffer()
      Takes the buffer out of the aggregator. Once the buffer has been taken out, the aggregator resets itself and a new buffer will be acquired from the pool during the next aggregate(ByteBuffer) call.
      Returns:
      the aggregated buffer, or null if nothing has been buffered yet
    • toString

      public String toString()
      Overrides:
      toString in class Object