3

这是我的问题:一个大的 gzip 文件;数百万条消息。

每条消息包括:

***************** *************** ****************** 
* 2-byte LENGTH * * 1-byte TYPE * * N-byte PAYLOAD * , where N = (LENGTH-1).
***************** *************** ******************

根据TYPE,我需要从 中的偏移量中读取几个字节,PAYLOAD然后选择接受或拒绝消息。

我知道如何使用 java.io.DataInputStream 来做这种事情,但这似乎是 java.nio.ByteBuffer 的完美应用(见这里!)。但是,我需要一些帮助来设置它。

那么,如何使用 ByteBuffer 从我的 gzip 文件中读取消息?

更新

我想我想看到的是代码的骨架实现,它可以让我走上正确的轨道来有效地使用 ByteBuffer。谢谢!

4

2 回答 2

2

Sorry to not answer your question, but I'm not sure there's any benefit to using ByteBuffer over DataInputStream here. You'll probably want to put your stream through GZIPInputStream to inflate the data.

GZIPInputStream gzipInput = new GZIPInputStream(yourInputStream);
DataInputStream dataInput = new DataInputStream(gzipInput);

With ByteBuffer you'd probably just be wrapping the bytes read from your input stream, with no advantage.

于 2010-07-24T21:20:51.430 回答
-1

为什么不使用像 Mina 或 Netty 这样的库,而不是在低级别为您的协议编写所有内容?他们可以为您提供相当容易实施的解决方案。

米娜http://mina.apache.org/

网状http://www.jboss.org/netty

顺便说一句,我们在 Red5 中使用 Mina,我们有很多实现者处理数百万条消息。

于 2011-09-13T15:01:53.183 回答