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