title: Get direct ByteBuffer slice position relative to original ByteBuffer
body: <p>Is there any way of getting a direct <code>ByteBuffer</code> zero position relative to another, given only the two <code>ByteBuffer</code> objects, if it is known that it is a subsequence of the other buffer? </p>
  
  <p>I know that this can be done with a non-direct array backed <code>ByteBuffer</code> using the <code>arrayOffset()</code> method like so:</p>
  
  <pre><code>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();
  
    // returns 22
    getRelativeBufferOffset(buffer1, buffer2);
  
    // returns 55
    getRelativeBufferOffset(buffer2, buffer3);
  
    // returns 77
    getRelativeBufferOffset(buffer1, buffer3);
  }
  </code></pre>
  
  <p>I'm thinking that there isn't anything that exists for free for direct buffers. In order to get something similar the best option I can think of is to extend <code>ByteBuffer</code> to store a reference to the buffer from which it was created (the parent buffer) and the zero position relative to the parent's zero position at which it was created.</p>
  
  <p>EDIT: Annoyingly it looks like I can't extend <code>ByteBuffer</code> because none of its constructors are visible. I guess I will have to write some kind of wrapper class instead.</p>