static int readHeapBuffer(InputStream f, ByteBuffer buf) throws IOException {
    int bytesRead = f.read(buf.array(), buf.arrayOffset() + buf.position(), buf.remaining());
    if (bytesRead < 0) {
      // if this resulted in EOF, don't update position
      return bytesRead;
    } else {
      buf.position(buf.position() + bytesRead);
      return bytesRead;
    }
  }
@Test
  public void testHeapPositionAndLimit() throws Exception {
    ByteBuffer readBuffer = ByteBuffer.allocate(20);
    readBuffer.position(5);
    readBuffer.limit(13);
    readBuffer.mark();

    MockInputStream stream = new MockInputStream(7);

    int len = DelegatingSeekableInputStream.readHeapBuffer(stream, readBuffer);
    Assert.assertEquals(7, len);
    Assert.assertEquals(12, readBuffer.position());
    Assert.assertEquals(13, readBuffer.limit());

    len = DelegatingSeekableInputStream.readHeapBuffer(stream, readBuffer);
    Assert.assertEquals(1, len);
    Assert.assertEquals(13, readBuffer.position());
    Assert.assertEquals(13, readBuffer.limit());

    len = DelegatingSeekableInputStream.readHeapBuffer(stream, readBuffer);
    Assert.assertEquals(0, len);

    readBuffer.reset();
    Assert.assertEquals("Buffer contents should match", ByteBuffer.wrap(TEST_ARRAY, 0, 8), readBuffer);
  }