Top Description Fields Constructors Methods
org.eclipse.jetty.io

public Class ByteBufferAccumulator

extends Object
implements AutoCloseable
Class Inheritance
All Implemented Interfaces
java.lang.AutoCloseable
Imports
java.io.IOException, .OutputStream, java.nio.ByteBuffer, java.util.ArrayList, .List, org.eclipse.jetty.util.BufferUtil

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
ensureBuffer(int, int)

Field Summary

Modifier and TypeField and Description
private final ByteBufferPool
private final List<RetainableByteBuffer>
private final boolean

Constructor Summary

AccessConstructor and Description
public
public
ByteBufferAccumulator(ByteBufferPool bufferPool, boolean direct)

Method Summary

Modifier and TypeMethod and Description
public void
close()

Implements java.lang.AutoCloseable.close.

Closes this resource, relinquishing any underlying resources.

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

public RetainableByteBuffer

Returns:

a buffer with at least minSize space to write into.
ensureBuffer
(int
new buffers will be allocated to have at least this size.
minAllocationSize
)

Get the last buffer of the accumulator, this can be written to directly to avoid copying into the accumulator.

public RetainableByteBuffer

Returns:

a buffer with at least minSize space to write into.
ensureBuffer
(int
the smallest amount of remaining space before a new buffer is allocated.
minSize
,
int
new buffers will be allocated to have at least this size.
minAllocationSize
)

Get the last buffer of the accumulator, this can be written to directly to avoid copying into the accumulator.

public int

Returns:

the total length of the content in the accumulator.
getLength
()

Get the amount of bytes which have been accumulated.

public ByteBuffer
public RetainableByteBuffer

Returns:

a buffer containing all content written to the accumulator.
takeRetainableByteBuffer
()

Take the combined buffer containing all content written to the accumulator.

public byte[]
toByteArray()

Returns: a newly allocated byte array containing all content written into the accumulator.

public RetainableByteBuffer

Returns:

a buffer containing all content written to the accumulator.
toRetainableByteBuffer
()

Take the combined buffer containing all content written to the accumulator.

public void
writeTo(ByteBuffer byteBuffer)

public void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

_bufferPoolback to summary
private final ByteBufferPool _bufferPool
_buffersback to summary
private final List<RetainableByteBuffer> _buffers
_directback to summary
private final boolean _direct

Constructor Detail

ByteBufferAccumulatorback to summary
public ByteBufferAccumulator()
ByteBufferAccumulatorback to summary
public ByteBufferAccumulator(ByteBufferPool bufferPool, boolean direct)

Method Detail

closeback to summary
public void close()

Implements java.lang.AutoCloseable.close.

Doc from java.lang.AutoCloseable.close.

Closes this resource, relinquishing any underlying resources. This method is invoked automatically on objects managed by the try-with-resources statement.

Annotations
@Override
copyBufferback to summary
public void copyBuffer(ByteBuffer source)
copyBytesback to summary
public void copyBytes(byte[] buf, int offset, int length)
ensureBufferback to summary
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:int

new buffers will be allocated to have at least this size.

Returns:RetainableByteBuffer

a buffer with at least minSize space to write into.

ensureBufferback to summary
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:int

the smallest amount of remaining space before a new buffer is allocated.

minAllocationSize:int

new buffers will be allocated to have at least this size.

Returns:RetainableByteBuffer

a buffer with at least minSize space to write into.

getLengthback to summary
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:int

the total length of the content in the accumulator.

takeByteBufferback to summary
public ByteBuffer takeByteBuffer()
takeRetainableByteBufferback to summary
public RetainableByteBuffer takeRetainableByteBuffer()

Take the combined buffer containing all content written to the accumulator. The caller is responsible for releasing this RetainableByteBuffer.

Returns:RetainableByteBuffer

a buffer containing all content written to the accumulator.

See Also
toRetainableByteBuffer()
toByteArrayback to summary
public byte[] toByteArray()
Returns:byte[]

a newly allocated byte array containing all content written into the accumulator.

toRetainableByteBufferback to summary
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:RetainableByteBuffer

a buffer containing all content written to the accumulator.

See Also
takeRetainableByteBuffer(), close()
writeToback to summary
public void writeTo(ByteBuffer byteBuffer)
writeToback to summary
public void writeTo(OutputStream out) throws IOException