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 29, 2020
1 parent 8b72f13 commit 07a3cee
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 23 deletions.
Expand Up @@ -28,10 +28,10 @@

public class ByteAccumulator
{
private List<ByteBuffer> chunks = new ArrayList<>();
private final List<ByteBuffer> chunks = new ArrayList<>();
private final int maxSize;
private int length = 0;
private ByteBufferPool bufferPool;
private final ByteBufferPool bufferPool;

public ByteAccumulator(int maxOverallBufferSize)
{
Expand All @@ -44,17 +44,18 @@ public ByteAccumulator(int maxOverallBufferSize, ByteBufferPool bufferPool)
this.bufferPool = bufferPool;
}

public void copyChunk(ByteBuffer buffer, int length)
public void copyChunk(ByteBuffer buffer)
{
int length = buffer.position();
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 (length > 0)
{
buffer.limit(length);
chunks.add(buffer);
chunks.add((ByteBuffer) buffer.flip());
this.length += length;
}
else
Expand Down Expand Up @@ -124,6 +125,6 @@ void recycle()
bufferPool.release(chunk);
}

chunks = new ArrayList<>();
chunks.clear();
}
}
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(buf, position);
accumulator.copyChunk((ByteBuffer)buf.position(position));
return read;
}
position += read;
}
accumulator.copyChunk(buf, position);
accumulator.copyChunk((ByteBuffer) buf.position(position));
return position;
}

Expand Down
Expand Up @@ -103,20 +103,20 @@ public void testRecycle()
// 1
ByteBuffer buf = accumulator.newByteBuffer(10);
byte[] hello = "Hello".getBytes(UTF_8);
System.arraycopy(hello, 0, buf.array(), 0, hello.length);
accumulator.copyChunk(buf, hello.length);
buf.put(hello);
accumulator.copyChunk(buf);

// 2
buf = accumulator.newByteBuffer(10);
byte[] space = " ".getBytes(UTF_8);
System.arraycopy(space, 0, buf.array(), 0, space.length);
accumulator.copyChunk(buf, space.length);
buf.put(space);
accumulator.copyChunk(buf);

// 3
buf = accumulator.newByteBuffer(10);
byte[] world = "World".getBytes(UTF_8);
System.arraycopy(world, 0, buf.array(), 0, world.length);
accumulator.copyChunk(buf, world.length);
buf.put(world);
accumulator.copyChunk(buf);

assertThat("Length", accumulator.getLength(), is(hello.length + space.length + world.length));

Expand All @@ -130,26 +130,26 @@ public void testRecycle()
// 1
ByteBuffer buf = accumulator.newByteBuffer(10);
byte[] olleh = "olleH".getBytes(UTF_8);
System.arraycopy(olleh, 0, buf.array(), 0, olleh.length);
accumulator.copyChunk(buf, olleh.length);
buf.put(olleh);
accumulator.copyChunk(buf);

// 2
buf = accumulator.newByteBuffer(10);
byte[] space = " ".getBytes(UTF_8);
System.arraycopy(space, 0, buf.array(), 0, space.length);
accumulator.copyChunk(buf, space.length);
buf.put(space);
accumulator.copyChunk(buf);

// 3
buf = accumulator.newByteBuffer(10);
byte[] dlrow = "dlroW".getBytes(UTF_8);
System.arraycopy(dlrow, 0, buf.array(), 0, dlrow.length);
accumulator.copyChunk(buf, dlrow.length);
buf.put(dlrow);
accumulator.copyChunk(buf);

// 4
buf = accumulator.newByteBuffer(10);
byte[] done = " enoD".getBytes(UTF_8);
System.arraycopy(done, 0, buf.array(), 0, done.length);
accumulator.copyChunk(buf, done.length);
buf.put(done);
accumulator.copyChunk(buf);

assertThat("Length", accumulator.getLength(), is(olleh.length + space.length + dlrow.length + done.length));

Expand Down

0 comments on commit 07a3cee

Please sign in to comment.