When converting a ByteBuffer to an int array java unexpectedly throws an UnsupportedOperationException.
However as a byte array the information is correctly represented.
int[] intArray = new int[]{1, 2};
ByteBuffer byteBuffer = ByteBuffer.allocate(intArray.length * 4);
IntBuffer ib = byteBuffer.asIntBuffer();
ib.put(intArray);
System.out.println(Arrays.toString(byteBuffer.array())); // prints [0, 0, 0, 1, 0, 0, 0, 2]
System.out.println(Arrays.toString(ib.array())); // Fails with exception UnsupportedOperationException
Reading through other posts I saw this exception in case there was no array to back up the ByteBuffer, but in this case the buffer has enough bytes allocated and should be able to return an int[].
Why is this exception thrown?
(Using java11)