/** * Reads up to {@code buf.remaining()} bytes starting at position {@code pos} in this file to the * given buffer. Returns the number of bytes read or -1 if {@code pos} is greater than or equal to * the size of this file. */ public int read(long pos, ByteBuffer buf) { // since max is buf.remaining() (an int), result is guaranteed to be an int int bytesToRead = (int) bytesToRead(pos, buf.remaining()); if (bytesToRead > 0) { int remaining = bytesToRead; int blockIndex = blockIndex(pos); byte[] block = blocks[blockIndex]; int off = offsetInBlock(pos); remaining -= get(block, off, buf, length(off, remaining)); while (remaining > 0) { int index = ++blockIndex; block = blocks[index]; remaining -= get(block, 0, buf, length(remaining)); } } return bytesToRead; }
/** * Reads up to {@code len} bytes starting at position {@code pos} in this file to the given byte * array starting at offset {@code off}. Returns the number of bytes actually read or -1 if {@code * pos} is greater than or equal to the size of this file. */ public int read(long pos, byte[] b, int off, int len) { // since max is len (an int), result is guaranteed to be an int int bytesToRead = (int) bytesToRead(pos, len); if (bytesToRead > 0) { int remaining = bytesToRead; int blockIndex = blockIndex(pos); byte[] block = blocks[blockIndex]; int offsetInBlock = offsetInBlock(pos); int read = get(block, offsetInBlock, b, off, length(offsetInBlock, remaining)); remaining -= read; off += read; while (remaining > 0) { int index = ++blockIndex; block = blocks[index]; read = get(block, 0, b, off, length(remaining)); remaining -= read; off += read; } } return bytesToRead; }
/** * Reads up to {@code buf.remaining()} bytes starting at position {@code pos} in this file to the * given buffer. Returns the number of bytes read or -1 if {@code pos} is greater than or equal to * the size of this file. */ public int read(long pos, ByteBuffer buf) { // since max is buf.remaining() (an int), result is guaranteed to be an int int bytesToRead = (int) bytesToRead(pos, buf.remaining()); if (bytesToRead > 0) { int remaining = bytesToRead; int blockIndex = blockIndex(pos); byte[] block = blocks[blockIndex]; int off = offsetInBlock(pos); remaining -= get(block, off, buf, length(off, remaining)); while (remaining > 0) { int index = ++blockIndex; block = blocks[index]; remaining -= get(block, 0, buf, length(remaining)); } } return bytesToRead; }
/** * Reads up to {@code len} bytes starting at position {@code pos} in this file to the given byte * array starting at offset {@code off}. Returns the number of bytes actually read or -1 if {@code * pos} is greater than or equal to the size of this file. */ public int read(long pos, byte[] b, int off, int len) { // since max is len (an int), result is guaranteed to be an int int bytesToRead = (int) bytesToRead(pos, len); if (bytesToRead > 0) { int remaining = bytesToRead; int blockIndex = blockIndex(pos); byte[] block = blocks[blockIndex]; int offsetInBlock = offsetInBlock(pos); int read = get(block, offsetInBlock, b, off, length(offsetInBlock, remaining)); remaining -= read; off += read; while (remaining > 0) { int index = ++blockIndex; block = blocks[index]; read = get(block, 0, b, off, length(remaining)); remaining -= read; off += read; } } return bytesToRead; }