Java ByteBuffer convert to IntBuffer

Description

Java ByteBuffer convert to IntBuffer

import java.nio.ByteBuffer;
import java.nio.IntBuffer;

public class Main {
   public static void main(String[] args) {
      ByteBuffer bb = ByteBuffer.wrap(new byte[] { 0, 0, 0, 0, 0, 0, 0, 'a' });
      bb.rewind();//from   w w w . ja  va 2 s .c  om
      System.out.println("Byte Buffer");
      while (bb.hasRemaining())
         System.out.println(bb.position() + " -> " + bb.get());

      IntBuffer ib = ((ByteBuffer) bb.rewind()).asIntBuffer();
      System.out.println("Int Buffer");
      while (ib.hasRemaining())
         System.out.println(ib.position() + " -> " + ib.get());
   }
}



PreviousNext

Related