1

I got the UnsupportedOperationException in the following code:

byte[] temp = ByteBuffer.allocateDirect(10).array();

I checked the java specification, it says:

UnsupportedOperationException - If this buffer is not backed by an accessible array

Here are my questions:

(1) Is that mean if the byteBuffer is created through ByteBuffer.allocateDirect, this byteBuffer is always not backed by an accessible array?

(2) I know this byteBuffer is allocated outside of Java heap, but what's implementation of this allocation. Maybe through OS system call? So whether the array is accessible depends on the OS we use?

1
  • 1
    From the docs for allocateDirect: "Whether or not it has a backing array is unspecified." So yes, it's all implementation-specific... Commented Feb 17, 2015 at 16:01

1 Answer 1

1

Well, as to part (1), no, the buffer does not have to be backed by a Java array - and isn't when constructed like this.

As to (2), the actual implementation will be dependent on the underlying operating system, as the implementation uses native OS calls to actually implement the array. On Linux you would expect it's using a malloc() kernel call to reserve some memory which is then read/written directly using further native calls; other systems would obviously have their own equivalents and the JVM would call these.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.