首页javanio_bufferJava I/O - 如何将ByteBuffer转换为ShortBuffer

Java I/O - 如何将ByteBuffer转换为ShortBuffer

我们想知道如何将ByteBuffer转换为ShortBuffer。
import java.nio.ByteBuffer;
import java.nio.ShortBuffer;

public class MainClass {
  public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.wrap(new byte[] { 0, 0, 0, 0, 0, 0, 0, 'a' });
    bb.rewind();
    ShortBuffer sb = ((ByteBuffer) bb.rewind()).asShortBuffer();
    System.out.println("Short Buffer");
    while (sb.hasRemaining())
      System.out.println(sb.position() + " -> " + sb.get());

  }
}