Java Tutorial/File/ShortBuffer
Версия от 17:44, 31 мая 2010; (обсуждение)
Convert ByteBuffer to a 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());
}
}
/*
*/
Short Buffer 0 -> 0 1 -> 0 2 -> 0 3 -> 97
Use while loop to read a 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());
}
}
/*
*/
Short Buffer 0 -> 0 1 -> 0 2 -> 0 3 -> 97