2014-01-25 12 views
1

我正在使用netty,並且必須解析ChannelBufferInputStream中的二進制數據。下面是我使用的代碼:如何使用InflaterInputStream封裝netty的ChannelBufferInputStream以擴充數據?

ins.skipBytes(14); // skip 14 bytes header 
byte[] b = new byte[195]; // note that 195 is the length of data after inflation 
(new InflaterInputStream(ins)).read(b, 0, 195); 

可正常工作,但它設置在後195個字節的ChannelBufferInputStream的標誌。不用說,標記應該少於195個字節後設置。

是否有可能得到否。從輸入流中讀取'實際'字節,以便我可以自己設置標記?或者還有其他一些方法來誇大Netty中的ChannelBuffer數據嗎?

回答

1

不知道大碼流的樣子,就很難推薦一個最佳實踐,但假設你正在閱讀的傳入網絡數據流,一個更好的模式可能是使用管道處理的像一個序列:

HeaderHandler --> decoder, returns null until 14 bytes are read 
    InflaterDecoder --> Inflates the remainder (will ZLibDecoder work ?) 
     AppHandler --> Receives the inflated buffer 

但直接回答你的第一個問題,ChannelBufferInputStream.readBytes()會,引述的Javadoc:

由此流返回讀取的字節數爲止。