Skip to content

Commit

Permalink
Fix issue jetty#5499
Browse files Browse the repository at this point in the history
this PR let the ByteAccumulator recyclable. after invoke ByteAccumulator.transferTo method
we can invoke ByteAccumulator.recycle method to reuse byte[] via ByteAccumulator.newByteArray method

Signed-off-by: Baoyi Chen <chen.bao.yi@qq.com>
  • Loading branch information
leonchen83 committed Oct 30, 2020
1 parent 0b22a92 commit 5112a2b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
Expand Up @@ -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());
}
}

Expand Down Expand Up @@ -122,7 +122,7 @@ void recycle()
}
for (ByteBuffer chunk : chunks)
{
bufferPool.release(chunk);
bufferPool.release((ByteBuffer)chunk.clear());
}

chunks.clear();
Expand Down
Expand Up @@ -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;
}

Expand Down
Expand Up @@ -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));
Expand All @@ -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));
Expand Down

0 comments on commit 5112a2b

Please sign in to comment.