@Test
  public void testMoveBufferToStream() throws IOException {
    final int arrayOffset = 7;
    final int initialPosition = 10;
    final int endPadding = 5;
    byte[] arrayWrapper = new byte[arrayOffset + initialPosition + array.length + endPadding];
    System.arraycopy(array, 0, arrayWrapper, arrayOffset + initialPosition, array.length);
    ByteBuffer buffer =
      ByteBuffer.wrap(arrayWrapper, arrayOffset, initialPosition + array.length).slice();
    assertEquals(initialPosition + array.length, buffer.limit());
    assertEquals(0, buffer.position());
    buffer.position(initialPosition);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ByteBufferUtils.moveBufferToStream(bos, buffer, array.length);
    assertArrayEquals(array, bos.toByteArray());
    assertEquals(initialPosition + array.length, buffer.position());
  }
Test copying to stream from buffer.
hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestByteBufferUtils.java