我正在编写一个编码器,用于在JSON消息末尾添加空字符,以便在消息被分片时可以解码。
我找到了这个示例 ->点击,其中在最后调用了ByteBuf.retain()
来将现有的ByteBuf写入输出。为什么要这样做,它为什么是必需的?
以下是我的编码器:
public class FrameEncoder extends MessageToMessageEncoder<ByteBuf> {
@Override
protected void encode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
out.add(msg.retain());
out.add(ctx.alloc().buffer(1).writeByte(NUL));
}
}