Package org.eclipse.jetty.io
Class ByteBufferAccumulator
java.lang.Object
org.eclipse.jetty.io.ByteBufferAccumulator
- All Implemented Interfaces:
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 Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
void
copyBuffer
(ByteBuffer source) void
copyBytes
(byte[] buf, int offset, int length) ensureBuffer
(int minAllocationSize) Get the last buffer of the accumulator, this can be written to directly to avoid copying into the accumulator.ensureBuffer
(int minSize, int minAllocationSize) Get the last buffer of the accumulator, this can be written to directly to avoid copying into the accumulator.int
Get the amount of bytes which have been accumulated.Take the combined buffer containing all content written to the accumulator.byte[]
Take the combined buffer containing all content written to the accumulator.void
writeTo
(OutputStream out) void
writeTo
(ByteBuffer byteBuffer)
-
Constructor Details
-
ByteBufferAccumulator
public ByteBufferAccumulator() -
ByteBufferAccumulator
-
-
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
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
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
-
takeRetainableByteBuffer
Take the combined buffer containing all content written to the accumulator. The caller is responsible for releasing thisRetainableByteBuffer
.- Returns:
- a buffer containing all content written to the accumulator.
- See Also:
-
takeByteBuffer
-
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
-
writeTo
- Throws:
IOException
-
close
public void close()- Specified by:
close
in interfaceAutoCloseable
-