Skip to content

Commit

Permalink
Fix ByteBufferAccumulator minSize
Browse files Browse the repository at this point in the history
Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
  • Loading branch information
lachlan-roberts committed Nov 16, 2020
1 parent a1aa5dc commit 5788fe6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ public ByteBufferPool getByteBufferPool()
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.
* @param minSize the smallest amount of remaining space before a new buffer is allocated.
Expand All @@ -72,7 +82,7 @@ public ByteBufferPool getByteBufferPool()
public ByteBuffer ensureBuffer(int minSize, int minAllocationSize)
{
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);
_buffers.add(buffer);
Expand All @@ -90,7 +100,7 @@ public void copyBuffer(ByteBuffer buffer)
{
while (buffer.hasRemaining())
{
ByteBuffer b = ensureBuffer(0, buffer.remaining());
ByteBuffer b = ensureBuffer(buffer.remaining());
int pos = BufferUtil.flipToFill(b);
BufferUtil.put(buffer, b);
BufferUtil.flipToFlush(b, pos);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ private void compress(FrameEntry entry, boolean first)
{
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);
buffer.limit(buffer.limit() + compressed);

Expand Down

0 comments on commit 5788fe6

Please sign in to comment.