java ByteBuffer to int array conversion fails with UnsupportedOperationException

Viewed 25

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)

1 Answers

The #array() methods of all the buffer classes are defined to throw UnsupportedOperationException:

If this buffer is not backed by an accessible array

Also, modifying the array returned by #array() modifies the buffer:

Modifications to this buffer's content will cause the returned array's content to be modified, and vice versa.

And you're calling it on an IntBuffer that was created via ByteBuffer#asIntBuffer(), which:

Creates a view [emphasis added] of this byte buffer as an int buffer.

The content of the new buffer will start at this buffer's current position. Changes to this buffer's content will be visible in the new buffer, and vice versa [emphasis added]; the two buffers' position, limit, and mark values will be independent.

[...]

Combine all that information and you can see why calling array() on your IntBuffer throws an UnsupportedOperationException. If it were to return an int[], then changing the int[] would not be capable of modifying the ByteBuffer. This is a problem, because modifying the int[] is defined to modify the IntBuffer, and since your IntBuffer is only a view, modifying the IntBuffer is defined to also modify the ByteBuffer. But it's not possible to directly "link" a byte[] and an int[], hence the operation is not supported.

Related