开发者

Using Java's ByteBuffer to read millions of messages

开发者 https://www.devze.com 2023-01-08 13:07 出处:网络
Here\'s my problem: one big gzipped file; millions of messages. Each message consists of: ***************** *************** ******************

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

精彩评论

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