情况:DelimiterBasedFrameDecoder extends ByteToMessageDecoder
和ByteToMessageDecoder
确实将未处理的字节保存在名为ByteBuf
的{{1}}中。
我想在另一个处理程序中手动调用此处理程序来清空此cumulation
cumulation
。
我尝试了什么:
ByteBuf
问题:我尝试的东西不起作用,因为没有下一个处理程序,所以Netty告诉我字节丢失了:
废弃的入站邮件UnpooledHeapByteBuf(ridx:0,widx:57,cap: 57)到达管道的尾部。请检查你的 管道配置。
答案 0 :(得分:3)
您可以将处理程序包装在EmbeddedChannel中,并使用writeInbound()和readInbound()。检查我们的单元测试以获取用法示例以及javadoc。
答案 1 :(得分:0)
PublicCumulationDelimiterBasedFrameDecoder frameDecoder = (PublicCumulationDelimiterBasedFrameDecoder) cp.get("frameDecoder");
ByteBuf bytesNotProcessed = frameDecoder.internalBuffer();
if (bytesNotProcessed != Unpooled.EMPTY_BUFFER) { PublicCumulationDelimiterBasedFrameDecoder tmpDelimiterBasedFrameDecoder = new PublicCumulationDelimiterBasedFrameDecoder(2048, false, true, Unpooled.wrappedBuffer(new byte[] { 0x03 }));
EmbeddedChannel ec = new EmbeddedChannel(tmpDelimiterBasedFrameDecoder, ... other handlers...);
ByteBuf wrapper = Unpooled.buffer();
wrapper.writeBytes(bytesNotProcessed);
ec.writeInbound(wrapper);// make it process the bytes
// put the remaining bytes (if any) back into the original buffer
bytesNotProcessed.clear();
bytesNotProcessed.writeBytes(tmpDelimiterBasedFrameDecoder.internalBuffer());
}
其中PublicCumulationDelimiterBasedFrameDecoder
就是这样:
public class PublicCumulationDelimiterBasedFrameDecoder extends DelimiterBasedFrameDecoder {
public PublicCumulationDelimiterBasedFrameDecoder(int maxFrameLength, boolean stripDelimiter, boolean failFast, ByteBuf delimiter) {
super(maxFrameLength, stripDelimiter, failFast, delimiter);
}
public ByteBuf internalBuffer() {
return super.internalBuffer();
}
}