Fix ByteBufferAccumulator minSize

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
Lachlan Roberts 2020-11-16 18:43:22 +11:00
parent a1aa5dcd14
commit 5788fe609d
2 changed files with 13 additions and 3 deletions

View File

@ -63,6 +63,16 @@ public class ByteBufferAccumulator implements AutoCloseable
return _bufferPool; return _bufferPool;
} }
/**
* Get the last buffer of the accumulator, this can be written to directly to avoid copying into the accumulator.
* @param minAllocationSize new buffers will be allocated to have at least this size.
* @return a buffer with at least {@code minSize} space to write into.
*/
public ByteBuffer ensureBuffer(int minAllocationSize)
{
return ensureBuffer(1, minAllocationSize);
}
/** /**
* Get the last buffer of the accumulator, this can be written to directly to avoid copying into the accumulator. * Get the last buffer of the accumulator, this can be written to directly to avoid copying into the accumulator.
* @param minSize the smallest amount of remaining space before a new buffer is allocated. * @param minSize the smallest amount of remaining space before a new buffer is allocated.
@ -72,7 +82,7 @@ public class ByteBufferAccumulator implements AutoCloseable
public ByteBuffer ensureBuffer(int minSize, int minAllocationSize) public ByteBuffer ensureBuffer(int minSize, int minAllocationSize)
{ {
ByteBuffer buffer = _buffers.isEmpty() ? BufferUtil.EMPTY_BUFFER : _buffers.get(_buffers.size() - 1); ByteBuffer buffer = _buffers.isEmpty() ? BufferUtil.EMPTY_BUFFER : _buffers.get(_buffers.size() - 1);
if (BufferUtil.space(buffer) <= minSize) if (BufferUtil.space(buffer) < minSize)
{ {
buffer = _bufferPool.acquire(minAllocationSize, false); buffer = _bufferPool.acquire(minAllocationSize, false);
_buffers.add(buffer); _buffers.add(buffer);
@ -90,7 +100,7 @@ public class ByteBufferAccumulator implements AutoCloseable
{ {
while (buffer.hasRemaining()) while (buffer.hasRemaining())
{ {
ByteBuffer b = ensureBuffer(0, buffer.remaining()); ByteBuffer b = ensureBuffer(buffer.remaining());
int pos = BufferUtil.flipToFill(b); int pos = BufferUtil.flipToFill(b);
BufferUtil.put(buffer, b); BufferUtil.put(buffer, b);
BufferUtil.flipToFlush(b, pos); BufferUtil.flipToFlush(b, pos);

View File

@ -496,7 +496,7 @@ public abstract class CompressExtension extends AbstractExtension
{ {
while (true) while (true)
{ {
ByteBuffer buffer = accumulator.ensureBuffer(0, outputLength); ByteBuffer buffer = accumulator.ensureBuffer(8, outputLength);
int compressed = deflater.deflate(buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.capacity() - buffer.limit(), Deflater.SYNC_FLUSH); int compressed = deflater.deflate(buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.capacity() - buffer.limit(), Deflater.SYNC_FLUSH);
buffer.limit(buffer.limit() + compressed); buffer.limit(buffer.limit() + compressed);