@Test public void read() throws Exception { write(ByteString.encodeUtf8("Hello, World")); FileOperator operator = new FileOperator(randomAccessFile.getChannel()); Buffer buffer = new Buffer(); operator.read(0, buffer, 5); assertEquals("Hello", buffer.readUtf8()); operator.read(4, buffer, 5); assertEquals("o, Wo", buffer.readUtf8()); }
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);  }