@Test public void readBounds() throws Exception { FileOperator operator = new FileOperator(randomAccessFile.getChannel()); Buffer buffer = new Buffer(); try { operator.read(0, buffer, -1L); fail(); } catch (IndexOutOfBoundsException ignored) { } }
public void read(long pos, Buffer sink, long byteCount) throws IOException { if (byteCount < 0) { throw new IndexOutOfBoundsException(); } while (byteCount > 0L) { try { byteBuffer.limit((int) Math.min(BUFFER_SIZE, byteCount)); if (fileChannel.read(byteBuffer, pos) == -1) { throw new EOFException(); } int bytesRead = byteBuffer.position(); sink.write(byteArray, 0, bytesRead); pos += bytesRead; byteCount -= bytesRead; } finally { byteBuffer.clear(); } } }
FileOperator { public void read(long pos, Buffer sink, long byteCount) throws IOException { if (byteCount < 0) { throw new IndexOutOfBoundsException(); } while (byteCount > 0L) { try { byteBuffer.limit((int) Math.min(BUFFER_SIZE, byteCount)); if (fileChannel.read(byteBuffer, pos) == -1) { throw new EOFException(); } int bytesRead = byteBuffer.position(); sink.write(byteArray, 0, bytesRead); pos += bytesRead; byteCount -= bytesRead; } finally { byteBuffer.clear(); } } } }
FileOperator { public void read(long pos, Buffer sink, long byteCount) throws IOException { if (byteCount < 0) { throw new IndexOutOfBoundsException(); } while (byteCount > 0L) { try { byteBuffer.limit((int) Math.min(BUFFER_SIZE, byteCount)); if (fileChannel.read(byteBuffer, pos) == -1) { throw new EOFException(); } int bytesRead = byteBuffer.position(); sink.write(byteArray, 0, bytesRead); pos += bytesRead; byteCount -= bytesRead; } finally { byteBuffer.clear(); } } } FileOperator(FileChannel fileChannel); }
FileOperator { public void read(long pos, Buffer sink, long byteCount) throws IOException { if (byteCount < 0) { throw new IndexOutOfBoundsException(); } while (byteCount > 0L) { try { byteBuffer.limit((int) Math.min(BUFFER_SIZE, byteCount)); if (fileChannel.read(byteBuffer, pos) == -1) { throw new EOFException(); } int bytesRead = byteBuffer.position(); sink.write(byteArray, 0, bytesRead); pos += bytesRead; byteCount -= bytesRead; } finally { byteBuffer.clear(); } } } FileOperator(FileChannel fileChannel); FileChannel getFileChannel(); void write(long pos, Buffer source, long byteCount); void read(long pos, Buffer sink, long byteCount); }
FileOperator { public void read(long pos, Buffer sink, long byteCount) throws IOException { if (byteCount < 0) { throw new IndexOutOfBoundsException(); } while (byteCount > 0L) { try { byteBuffer.limit((int) Math.min(BUFFER_SIZE, byteCount)); if (fileChannel.read(byteBuffer, pos) == -1) { throw new EOFException(); } int bytesRead = byteBuffer.position(); sink.write(byteArray, 0, bytesRead); pos += bytesRead; byteCount -= bytesRead; } finally { byteBuffer.clear(); } } } FileOperator(FileChannel fileChannel); FileChannel getFileChannel(); void write(long pos, Buffer source, long byteCount); void read(long pos, Buffer sink, long byteCount);  }