Java I/O How to - Convert ByteBuffer to DoubleBuffer








Question

We would like to know how to convert ByteBuffer to DoubleBuffer.

Answer

import java.nio.ByteBuffer;
import java.nio.DoubleBuffer;
//from ww  w .j  a  va  2  s  . c  om
public class MainClass {
  public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.wrap(new byte[] { 0, 0, 0, 0, 0, 0, 0, 'a' });
    bb.rewind();
    DoubleBuffer db = ((ByteBuffer) bb.rewind()).asDoubleBuffer();
    System.out.println("Double Buffer");
    while (db.hasRemaining())
      System.out.println(db.position() + " -> " + db.get());
  }
}

The code above generates the following result.