@Test
  public void testSubBuffer() {
    ByteBuffer bb1 = ByteBuffer.allocateDirect(10);
    ByteBuffer bb2 = ByteBuffer.allocateDirect(10);
    MultiByteBuff multi = new MultiByteBuff(bb1, bb2);
    long l1 = 1234L, l2 = 100L;
    multi.putLong(l1);
    multi.putLong(l2);
    multi.rewind();
    ByteBuffer sub = multi.asSubByteBuffer(Bytes.SIZEOF_LONG);
    assertEquals(bb1, sub);
    assertEquals(l1, ByteBufferUtils.toLong(sub, sub.position()));
    multi.skip(Bytes.SIZEOF_LONG);
    sub = multi.asSubByteBuffer(Bytes.SIZEOF_LONG);
    assertNotEquals(bb1, sub);
    assertNotEquals(bb2, sub);
    assertEquals(l2, ByteBufferUtils.toLong(sub, sub.position()));
    multi.rewind();
    ObjectIntPair<ByteBuffer> p = new ObjectIntPair<>();
    multi.asSubByteBuffer(8, Bytes.SIZEOF_LONG, p);
    assertNotEquals(bb1, p.getFirst());
    assertNotEquals(bb2, p.getFirst());
    assertEquals(0, p.getSecond());
    assertEquals(l2, ByteBufferUtils.toLong(sub, p.getSecond()));
  }
Test right answer though we span many sub-buffers.
hbase-common/src/test/java/org/apache/hadoop/hbase/nio/TestMultiByteBuff.java