diff --git a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/extensions/compress/ByteAccumulator.java b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/extensions/compress/ByteAccumulator.java index efdf7741e8d8..3e40d0b673af 100644 --- a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/extensions/compress/ByteAccumulator.java +++ b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/extensions/compress/ByteAccumulator.java @@ -46,23 +46,23 @@ public ByteAccumulator(int maxOverallBufferSize, ByteBufferPool bufferPool) public void copyChunk(ByteBuffer buffer) { - int length = buffer.position(); + int length = buffer.remaining(); if (this.length + length > maxSize) { String err = String.format("Resulting message size [%,d] is too large for configured max of [%,d]", this.length + length, maxSize); throw new MessageTooLargeException(err); } - if (length > 0) + if (buffer.hasRemaining()) { - chunks.add((ByteBuffer)buffer.flip()); + chunks.add(buffer); this.length += length; } else { // release 0 length buffer directly if (bufferPool != null) - bufferPool.release(buffer); + bufferPool.release((ByteBuffer)buffer.clear()); } } @@ -122,7 +122,7 @@ void recycle() } for (ByteBuffer chunk : chunks) { - bufferPool.release(chunk); + bufferPool.release((ByteBuffer)chunk.clear()); } chunks.clear(); 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 215c45607870..a7ecade99151 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 @@ -193,12 +193,12 @@ int copyChunk(Inflater inflater, ByteAccumulator accumulator, ByteBuffer buf) th int read = inflater.inflate(buf.array(), position, capacity - position); if (read <= 0) { - accumulator.copyChunk((ByteBuffer)buf.position(position)); + accumulator.copyChunk((ByteBuffer)buf.position(position).flip()); return read; } position += read; } - accumulator.copyChunk((ByteBuffer)buf.position(position)); + accumulator.copyChunk((ByteBuffer)buf.position(position).flip()); return position; } diff --git a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/compress/ByteAccumulatorTest.java b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/compress/ByteAccumulatorTest.java index e7e44d49fe7e..1c3dd1ffaead 100644 --- a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/compress/ByteAccumulatorTest.java +++ b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/compress/ByteAccumulatorTest.java @@ -103,19 +103,19 @@ public void testRecycle() // 1 ByteBuffer buf = accumulator.newByteBuffer(10); byte[] hello = "Hello".getBytes(UTF_8); - buf.put(hello); + buf.put(hello).flip(); accumulator.copyChunk(buf); // 2 buf = accumulator.newByteBuffer(10); byte[] space = " ".getBytes(UTF_8); - buf.put(space); + buf.put(space).flip(); accumulator.copyChunk(buf); // 3 buf = accumulator.newByteBuffer(10); byte[] world = "World".getBytes(UTF_8); - buf.put(world); + buf.put(world).flip(); accumulator.copyChunk(buf); assertThat("Length", accumulator.getLength(), is(hello.length + space.length + world.length)); @@ -130,25 +130,25 @@ public void testRecycle() // 1 ByteBuffer buf = accumulator.newByteBuffer(10); byte[] olleh = "olleH".getBytes(UTF_8); - buf.put(olleh); + buf.put(olleh).flip(); accumulator.copyChunk(buf); // 2 buf = accumulator.newByteBuffer(10); byte[] space = " ".getBytes(UTF_8); - buf.put(space); + buf.put(space).flip(); accumulator.copyChunk(buf); // 3 buf = accumulator.newByteBuffer(10); byte[] dlrow = "dlroW".getBytes(UTF_8); - buf.put(dlrow); + buf.put(dlrow).flip(); accumulator.copyChunk(buf); // 4 buf = accumulator.newByteBuffer(10); byte[] done = " enoD".getBytes(UTF_8); - buf.put(done); + buf.put(done).flip(); accumulator.copyChunk(buf); assertThat("Length", accumulator.getLength(), is(olleh.length + space.length + dlrow.length + done.length));