Java ByteBuffer convert to ShortBuffer

Description

Java ByteBuffer convert to ShortBuffer

import java.nio.ByteBuffer;
import java.nio.ShortBuffer;

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 a  va2 s  . co  m*/
      System.out.println("Byte Buffer");
      while (bb.hasRemaining())
         System.out.println(bb.position() + " -> " + bb.get());

      ShortBuffer sb = ((ByteBuffer) bb.rewind()).asShortBuffer();
      System.out.println("Short Buffer");
      while (sb.hasRemaining())
         System.out.println(sb.position() + " -> " + sb.get());
   }
}



PreviousNext

Related