开发者

Using Java's ByteBuffer to read millions of messages

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 o开发者_开发百科ffset 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!


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜