Java ByteBuffer convert to CharBuffer

Description

Java ByteBuffer convert to CharBuffer

import java.nio.ByteBuffer;
import java.nio.CharBuffer;

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.j  av a  2  s .com
      System.out.println("Byte Buffer");
      while (bb.hasRemaining())
         System.out.println(bb.position() + " -> " + bb.get());

      CharBuffer cb = ((ByteBuffer) bb.rewind()).asCharBuffer();
      System.out.println("Char Buffer");
      while (cb.hasRemaining())
         System.out.println(cb.position() + " -> " + cb.get());
   }
}



PreviousNext

Related