title: Using Java's ByteBuffer to read millions of messages
body: <p>Here's my problem: one big gzipped file; millions of messages. </p>
  
  <p>Each message consists of:</p>
  
  <pre><code>***************** *************** ****************** 
  * 2-byte LENGTH * * 1-byte TYPE * * N-byte PAYLOAD * , where N = (LENGTH-1).
  ***************** *************** ******************
  </code></pre>
  
  <p>Depending on the <code>TYPE</code>, I need to read a few bytes from an offset in the <code>PAYLOAD</code> and choose to accept or reject the message.</p>
  
  <p>I know how to do this sort of thing using java.io.DataInputStream, but this seems like a perfect application of java.nio.ByteBuffer (<a href="http://www.javamex.com/tutorials/io/nio_buffers.shtml" rel="nofollow noreferrer">see here!</a>). However, I need some help setting it up.</p>
  
  <p>So, <em>how do I use ByteBuffer to read messages from my gzipped file?</em></p>
  
  <p><strong>Update</strong></p>
  
  <p>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!</p>