Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Use Java's ByteBuffer.arrayOffset() Method

Tags:

java

buffer

nio

The Java API documentation for ByteBuffer.arrayOffset states:

Returns the offset within this buffer's backing array of the first element of the buffer (optional operation).

I cannot figure out how to get arrayOffset() to return a non-zero value. Does anyone know how to change the result of a call to the ByteBuffer.arrayOffset() method?

like image 805
user2053359 Avatar asked Feb 13 '23 13:02

user2053359


1 Answers

Updated to describe the use of slice on array backed ByteBuffer.

The array offset points to an offset in a backing buffer. One of the ways to get a value other than 0 is to create an array backed ByteBuffer, then call slice. The result of slice will have an offset value corresponding to the current position of the original ByteBuffer. The array offset is only useful when getting the backing array to interact with directly

like image 116
Brett Okken Avatar answered Feb 15 '23 04:02

Brett Okken