Fixed buffer recycling.

This commit is contained in:
Simone Bordet 2014-01-14 12:43:37 +01:00
parent df60fd5c2d
commit 9dcde09cba
1 changed files with 11 additions and 7 deletions

View File

@ -42,10 +42,10 @@ public class Generator
{ {
id &= 0xFF_FF; id &= 0xFF_FF;
int remaining = content == null ? 0 : content.remaining(); int contentLength = content == null ? 0 : content.remaining();
Result result = new Result(byteBufferPool, callback); Result result = new Result(byteBufferPool, callback);
while (remaining > 0 || lastContent) while (contentLength > 0 || lastContent)
{ {
ByteBuffer buffer = byteBufferPool.acquire(8, false); ByteBuffer buffer = byteBufferPool.acquire(8, false);
BufferUtil.clearToFill(buffer); BufferUtil.clearToFill(buffer);
@ -55,22 +55,26 @@ public class Generator
buffer.put((byte)0x01); buffer.put((byte)0x01);
buffer.put((byte)frameType.code); buffer.put((byte)frameType.code);
buffer.putShort((short)id); buffer.putShort((short)id);
int length = Math.min(MAX_CONTENT_LENGTH, remaining); int length = Math.min(MAX_CONTENT_LENGTH, contentLength);
buffer.putShort((short)length); buffer.putShort((short)length);
buffer.putShort((short)0); buffer.putShort((short)0);
buffer.flip(); buffer.flip();
if (remaining == 0) if (contentLength == 0)
break; break;
// Slice to content to avoid copying // Slice the content to avoid copying
int limit = content.limit(); int limit = content.limit();
content.limit(content.position() + length); content.limit(content.position() + length);
ByteBuffer slice = content.slice(); ByteBuffer slice = content.slice();
result = result.append(slice, recycle); // Don't recycle the slice
result = result.append(slice, false);
content.position(content.limit()); content.position(content.limit());
content.limit(limit); content.limit(limit);
remaining -= length; contentLength -= length;
// Recycle the content buffer if needed
if (recycle && contentLength == 0)
result = result.append(content, true);
} }
return result; return result;