Source:
<code>public int positionedRead(long pos, byte[] b, int off, int len) throws IOException { Preconditions.checkArgument(len >= 0, "length should be non-negative"); Preconditions.checkArgument(off >= 0, "offset should be non-negative"); Preconditions.checkArgument(pos >= 0, "position should be non-negative"); if (len == 0) { return 0; } if (pos < 0 || pos >= mStatus.getLength()) { return -1; } int bytesRead = 0; long currentPosition = pos; long lengthToRead = Math.min(len, mStatus.getLength() - pos); <START> while (bytesRead < lengthToRead) { <END> long currentPage = currentPosition / mPageSize; int currentPageOffset = (int) (currentPosition % mPageSize); int bytesLeftInPage = (int) Math.min(mPageSize - currentPageOffset, lengthToRead - bytesRead); PageId pageId = new PageId(mStatus.getFileIdentifier(), currentPage); try (ReadableByteChannel cachedData = mCacheManager.get(pageId, currentPageOffset)) { if (cachedData != null) { ByteBuffer buf = ByteBuffer.wrap(b); buf.position(off + bytesRead); buf.limit(off + bytesRead + bytesLeftInPage); while (buf.position() != buf.limit()) { if (cachedData.read(buf) == -1) { break; } } Preconditions.checkState(buf.position() == buf.limit()); bytesRead += bytesLeftInPage; currentPosition += bytesLeftInPage; Metrics.BYTES_READ_CACHE.inc(bytesLeftInPage); } else { byte[] page = readExternalPage(currentPosition); mCacheManager.put(pageId, page); System.arraycopy(page, currentPageOffset, b, off + bytesRead, bytesLeftInPage); bytesRead += bytesLeftInPage; currentPosition += bytesLeftInPage; Metrics.BYTES_REQUESTED_EXTERNAL.inc(bytesLeftInPage); } } } Preconditions.checkState( bytesRead == len || (bytesRead < len && currentPosition == mStatus.getLength()), "Invalid number of bytes read - " + "bytes to read = %d, actual bytes read = %d, bytes remains in file %d", len, bytesRead, mStatus.getLength() - currentPosition); return bytesRead; }</code><technical_language>This shares a lot of code read(). refactored/reused</technical_language>

Target:
public int positionedRead(long pos, byte[] b, int off, int len) throws IOException { Preconditions.checkArgument(len >= 0, "length should be non-negative"); Preconditions.checkArgument(off >= 0, "offset should be non-negative"); Preconditions.checkArgument(pos >= 0, "position should be non-negative"); if (len == 0) { return 0; } if (pos < 0 || pos >= mStatus.getLength()) { return -1; } int bytesRead = 0; long currentPosition = pos; long lengthToRead = Math.min(len, mStatus.getLength() - pos); while (bytesRead < lengthToRead) { long currentPage = currentPosition / mPageSize; int currentPageOffset = (int) (currentPosition % mPageSize); int bytesLeftInPage = (int) Math.min(mPageSize - currentPageOffset, lengthToRead - bytesRead); PageId pageId = new PageId(mStatus.getFileIdentifier(), currentPage); try (ReadableByteChannel cachedData = mCacheManager.get(pageId, currentPageOffset)) { if (cachedData != null) { ByteBuffer buf = ByteBuffer.wrap(b); buf.position(off + bytesRead); buf.limit(off + bytesRead + bytesLeftInPage); while (buf.position() != buf.limit()) { if (cachedData.read(buf) == -1) { break; } } Preconditions.checkState(buf.position() == buf.limit()); bytesRead += bytesLeftInPage; currentPosition += bytesLeftInPage; Metrics.BYTES_READ_CACHE.inc(bytesLeftInPage); } else { byte[] page = readExternalPage(currentPosition); mCacheManager.put(pageId, page); System.arraycopy(page, currentPageOffset, b, off + bytesRead, bytesLeftInPage); bytesRead += bytesLeftInPage; currentPosition += bytesLeftInPage; Metrics.BYTES_REQUESTED_EXTERNAL.inc(bytesLeftInPage); } } } Preconditions.checkState( bytesRead == len || (bytesRead < len && currentPosition == mStatus.getLength()), "Invalid number of bytes positionread - read from position = %d, " + "bytes to read = %d, actual bytes read = %d, bytes remains in file %d", pos, len, bytesRead, mStatus.getLength() - currentPosition); return bytesRead; }