Java CharBuffer convert from ByteBuffer

Description

Java CharBuffer convert from ByteBuffer

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   ww w .j a  v  a2s . 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