Mega Code Archive

 
Categories / Java Tutorial / File
 

Test views of long elements in a ByteBuffer

import java.nio.ByteBuffer; import java.nio.ByteOrder; /**  * Test views of long elements in a ByteBuffer.  *   */ public class MainClass {   public static void main(String[] argv) throws Exception {     ByteBuffer bb = ByteBuffer.allocate(20);     bb.put((byte) 0x07);     bb.put((byte) 0x08);     bb.put((byte) 0x09);     bb.put((byte) 0x10);     bb.put((byte) 0x11);     bb.put((byte) 0x12);     bb.put((byte) 0x13);     bb.put((byte) 0x14);     bb.position(1).limit(5);     bb.mark();     System.out.println("Expect an exception here");     System.out.println("" + bb.order().toString() + ": " + bb.getLong());   } } /**/ Expect an exception here Exception in thread "main" java.nio.BufferUnderflowException at java.nio.Buffer.nextGetIndex(Buffer.java:404) at java.nio.HeapByteBuffer.getLong(HeapByteBuffer.java:387) at MainClass.main(MainClass.java:25)