Java ShortBuffer convert from ByteBuffer

Description

Java ShortBuffer convert from ByteBuffer

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();// w  w  w  .  j av  a 2s  .  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