Is there a way to get a direct zero position ByteBufferrelative to another, given only two objects ByteBuffer, if it is known that this is a subsequence of another buffer?
I know that this can be done using an indirect array with support ByteBufferusing the method arrayOffset():
int getRelativeBufferOffset(ByteBuffer parentBuffer, ByteBuffer childBuffer)
{
return childBuffer.arrayOffset() - parentBuffer.arrayOffset();
}
void example()
{
ByteBuffer buffer1 = ByteBuffer.allocate(10000);
buffer1 .position(22);
ByteBuffer buffer2 = buffer1.slice();
buffer2.position(55);
ByteBuffer buffer3 = buffer2.slice();
getRelativeBufferOffset(buffer1, buffer2);
getRelativeBufferOffset(buffer2, buffer3);
getRelativeBufferOffset(buffer1, buffer3);
}
I think there is nothing free for direct buffers. To get something like this, the best option I can think of is an extension ByteBufferto store the reference to the buffer from which it was created (parent buffer), and the zero position relative to the parent zero position in which it was created.
EDIT: , , ByteBuffer, . , - .