Skip to content

Commit

Permalink
cache: release the ByteBuf allocated when calling ByteBuf#readBytes(int)
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua-F authored and Adam- committed Feb 19, 2018
1 parent a2ecca9 commit bb5be37
Showing 1 changed file with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ protected void encode(ChannelHandlerContext ctx, ArchiveResponsePacket archiveRe
// - 3 for the header
int chunkSize = Math.min(file.readableBytes(), CHUNK_SIZE - 3);

out.writeBytes(file.readBytes(chunkSize));
writeChunk(file.readBytes(chunkSize), out);

while (file.isReadable())
{
out.writeByte(0xff);

chunkSize = Math.min(file.readableBytes(), CHUNK_SIZE - 1);
out.writeBytes(file.readBytes(chunkSize));
writeChunk(file.readBytes(chunkSize), out);
}

int size = out.readableBytes() - pos;
Expand All @@ -71,4 +71,16 @@ protected void encode(ChannelHandlerContext ctx, ArchiveResponsePacket archiveRe
archiveResponse.getData().length, size);
}

private void writeChunk(ByteBuf chunk, ByteBuf out)
{
try
{
out.writeBytes(chunk);
}
finally
{
chunk.release();
}
}

}

0 comments on commit bb5be37

Please sign in to comment.