public long transferTo(long pos, long count, WritableByteChannel dest) throws IOException {
    long bytesToRead = bytesToRead(pos, count);

    if (bytesToRead > 0) {
      long remaining = bytesToRead;

      int blockIndex = blockIndex(pos);
      byte[] block = blocks[blockIndex];
      int off = offsetInBlock(pos);

      ByteBuffer buf = ByteBuffer.wrap(block, off, length(off, remaining));
      while (buf.hasRemaining()) {
        remaining -= dest.write(buf);
      }
      Java8Compatibility.clear(buf);

      while (remaining > 0) {
        int index = ++blockIndex;
        block = blocks[index];

        buf = ByteBuffer.wrap(block, 0, length(remaining));
        while (buf.hasRemaining()) {
          remaining -= dest.write(buf);
        }
        Java8Compatibility.clear(buf);
      }
    }

    return Math.max(bytesToRead, 0); // don't return -1 for this method
  }
Transfers up to {@code count} bytes to the given channel starting at position {@code pos} in
this file. Returns the number of bytes transferred, possibly 0. Note that unlike all other read
methods in this class, this method does not return -1 if {@code pos} is greater than or equal
to the current size. This for consistency with {@link FileChannel#transferTo}, which this
method is primarily intended as an implementation of.
jimfs/src/main/java/com/google/common/jimfs/RegularFile.java