Convert ByteBuffer to a ShortBuffer in Java
Description
The following code shows how to convert ByteBuffer to a ShortBuffer.
Example
// ww w. java 2 s . com
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();
ShortBuffer sb = ((ByteBuffer) bb.rewind()).asShortBuffer();
System.out.println("Short Buffer");
while (sb.hasRemaining())
System.out.println(sb.position() + " -> " + sb.get());
}
}
The code above generates the following result.