1
我正在扩展ChannelInboundHandlerAdapter
并且想要读取确切的字节数。Netty读取确切字节数
public class Reader extends ChannelInboundHandlerAdapter{
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg){
ByteBuf b = (ByteBuf) msg;
byte size = b.readByte();
//Now I want to read exactly size bytes from the channel
//and then again read the number of bytes and read the bytes...
}
}
问题是可能发生这种情况,我们从ByteBuf
读取少于所需的字节。如何阅读更多Channel
?
其实,是的。非常感谢。但是'ReplayingDecoder'之间的区别呢?何时使用哪一个? –
@ St.Antario请看'ReplayingDecoder' javadocs。用例子有很好的解释。简单地说''ReplayingDecoder'做的更多,你不需要检查'in.readableBytes()
ReplayingDecoder可以帮助您处理底层流数据的复杂情况,例如数据不够。如果你不关心性能,它会太多地减少你的代码逻辑复杂性。 – Kaneg