title: How to convert a ByteBuffer to long[] in Java
body: <p>Can someone please let me know how to convert a a ByteBuffer into a long[]?</p>
  
  <p>I had a long[] to begin with, which I converted to a ByteBuffer using the following piece of code:</p>
  
  <hr>
  
  <pre><code>byte[] convertToByteArray(long[] data) {
      ByteBuffer byteBuffer = ByteBuffer.allocate(data.length * 8);;
      LongBuffer longBuffer = byteBuffer.asLongBuffer();
      longBuffer.put(data);
      return byteBuffer.array();
  }
  </code></pre>
  
  <hr>
  
  <p>I would now like to do the reverse, and write a few tests comparing the various arrays before and after conversion.</p>
  
  <p>I tried the following, but it fails with an NPE on JDK 1.6.0_26:</p>
  
  <hr>
  
  <pre><code>long[] convertToLongArray(byte[] bArray) {
      ByteBuffer byteBuffer = ByteBuffer.wrap(bArray);
      LongBuffer longBuffer = byteBuffer.asLongBuffer();
      return longBuffer.array();
  }
  </code></pre>
  
  <hr>
  
  <p>Regards,
  Roshan</p>