我正在使用 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的数据吗?
最佳答案
如果不知道更大的代码流是什么样子,就很难推荐最佳实践,但假设您正在读取传入的网络流,更好的模式可能是使用一系列管道处理程序,例如:
HeaderHandler --> decoder, returns null until 14 bytes are read
InflaterDecoder --> Inflates the remainder (will ZLibDecoder work ?)
AppHandler --> Receives the inflated buffer
但要直接回答你的第一个问题,ChannelBufferInputStream.readBytes()会,引用javadoc:
Returns the number of read bytes by this stream so far.
关于java - 如何用InflaterInputStream包装netty的ChannelBufferInputStream来膨胀数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21349259/