It depends:
1 2 3 4 5 6 7
| ByteBuffer byteBuff1 = ByteBuffer.wrap(new byte[]{1, 2, 3, 4}); ByteBuffer byteBuff2 = ByteBuffer.allocateDirect(16).order(ByteOrder.nativeOrder()).put(new byte[]{1, 2, 3, 4}); IntBuffer intBuff1 = ByteBuffer.wrap(byteBuff1.array()).asIntBuffer(); System.out.println(intBuff1.toString()); IntBuffer intBuff2 = ByteBuffer.wrap(byteBuff2.array()).asIntBuffer(); System.out.println(intBuff2.toString()); |
The stuff involving byteBuff1 works perfectly fine, but with byteBuff2 it fails with an UnsupportedOperationException becuase there is no explicit array backing the buffer.
-=EDIT=-
1
| ByteBuffer byteBuff2 = ByteBuffer.allocate(16).order(ByteOrder.nativeOrder()).put(new byte[]{1, 2, 3, 4}); |
This works too, so it really just depends on how they allocate the buffer in their JNI stuff!