From 5788fe609dae59ca82dfc21489f41e5bba3add40 Mon Sep 17 00:00:00 2001 From: Lachlan Roberts Date: Mon, 16 Nov 2020 18:43:22 +1100 Subject: [PATCH] Fix ByteBufferAccumulator minSize Signed-off-by: Lachlan Roberts --- .../eclipse/jetty/io/ByteBufferAccumulator.java | 14 ++++++++++++-- .../extensions/compress/CompressExtension.java | 2 +- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/jetty-io/src/main/java/org/eclipse/jetty/io/ByteBufferAccumulator.java b/jetty-io/src/main/java/org/eclipse/jetty/io/ByteBufferAccumulator.java index 7638bd30123..cf6d2dd9c66 100644 --- a/jetty-io/src/main/java/org/eclipse/jetty/io/ByteBufferAccumulator.java +++ b/jetty-io/src/main/java/org/eclipse/jetty/io/ByteBufferAccumulator.java @@ -63,6 +63,16 @@ public class ByteBufferAccumulator implements AutoCloseable 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. @@ -72,7 +82,7 @@ public class ByteBufferAccumulator implements AutoCloseable 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); @@ -90,7 +100,7 @@ public class ByteBufferAccumulator implements AutoCloseable { 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); diff --git a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/extensions/compress/CompressExtension.java b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/extensions/compress/CompressExtension.java index ce819d2af52..8dd90067c1b 100644 --- a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/extensions/compress/CompressExtension.java +++ b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/extensions/compress/CompressExtension.java @@ -496,7 +496,7 @@ public abstract class CompressExtension extends AbstractExtension { 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);