使用Java的ByteBuffer读取数百万条消息

发布于 2024-09-11 14:36:58 字数 683 浏览 7 评论 0原文

这是我的问题:一个大的 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 的正确途径。谢谢!

Here's my problem: one big gzipped file; millions of messages.

Each message consists of:

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

Depending on the TYPE, I need to read a few bytes from an offset in the PAYLOAD and choose to accept or reject the message.

I know how to do this sort of thing using java.io.DataInputStream, but this seems like a perfect application of java.nio.ByteBuffer (see here!). However, I need some help setting it up.

So, how do I use ByteBuffer to read messages from my gzipped file?

Update

I guess what I'd like to see is a skeletal implementation of code that could get me on the right track to using ByteBuffer effectively. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡水深流 2024-09-18 14:36:58

很抱歉没有回答您的问题,但我不确定在这里使用 ByteBuffer 比 DataInputStream 有什么好处。您可能希望将流通过 GZIPInputStream 来膨胀数据。

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

使用 ByteBuffer,您可能只是包装从输入流读取的字节,没有任何优势。

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.

鲸落 2024-09-18 14:36:58

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

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

Netty http://www.jboss.org/netty

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

Instead of writing everything for your protocol at a low level, why not use a library like Mina or Netty? They can provide you with fairly easy to implement a solution.

Mina http://mina.apache.org/

Netty http://www.jboss.org/netty

As an aside, we use Mina in Red5 and we lots of implementers processing millions and millions of messages.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文