No more memory allocation when using grouped packets

This commit is contained in:
themode 2020-12-26 21:10:27 +01:00
parent feb4783988
commit 6a9461546c

View File

@ -9,20 +9,11 @@ public class GroupedPacketHandler extends MessageToByteEncoder<FramedPacket> {
@Override
protected void encode(ChannelHandlerContext ctx, FramedPacket msg, ByteBuf out) {
final ByteBuf packet = msg.body;
out.setBytes(0, packet, 0, packet.writerIndex());
out.writerIndex(packet.writerIndex());
}
@Override
protected ByteBuf allocateBuffer(ChannelHandlerContext ctx, FramedPacket msg, boolean preferDirect) {
final int size = msg.body.writerIndex();
if (preferDirect) {
return ctx.alloc().directBuffer(size, size);
} else {
return ctx.alloc().heapBuffer(size, size);
}
return msg.body.retainedSlice();
}
}