Convert ByteBuffer to a LongBuffer in Java
Description
The following code shows how to convert ByteBuffer to a LongBuffer.
Example
/* ww w. ja va 2 s . c om*/
import java.nio.ByteBuffer;
import java.nio.LongBuffer;
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();
LongBuffer lb = ((ByteBuffer) bb.rewind()).asLongBuffer();
System.out.println("Long Buffer");
while (lb.hasRemaining())
System.out.println(lb.position() + " -> " + lb.get());
}
}
The code above generates the following result.