@Test public void write() throws Exception { FileOperator operator = new FileOperator(randomAccessFile.getChannel()); Buffer buffer1 = new Buffer().writeUtf8("Hello, World"); operator.write(0, buffer1, 5); assertEquals(", World", buffer1.readUtf8()); Buffer buffer2 = new Buffer().writeUtf8("icopter!"); operator.write(3, buffer2, 7); assertEquals("!", buffer2.readUtf8()); assertEquals(ByteString.encodeUtf8("Helicopter"), snapshot()); }
public void write(long pos, Buffer source, long byteCount) throws IOException { if (byteCount < 0 || byteCount > source.size()) { throw new IndexOutOfBoundsException(); } while (byteCount > 0L) { try { int toWrite = (int) Math.min(BUFFER_SIZE, byteCount); source.read(byteArray, 0, toWrite); byteBuffer.limit(toWrite); do { int bytesWritten = fileChannel.write(byteBuffer, pos); pos += bytesWritten; } while (byteBuffer.hasRemaining()); byteCount -= toWrite; } finally { byteBuffer.clear(); } } }
FileOperator { public void write(long pos, Buffer source, long byteCount) throws IOException { if (byteCount < 0 || byteCount > source.size()) { throw new IndexOutOfBoundsException(); } while (byteCount > 0L) { try { int toWrite = (int) Math.min(BUFFER_SIZE, byteCount); source.read(byteArray, 0, toWrite); byteBuffer.limit(toWrite); do { int bytesWritten = fileChannel.write(byteBuffer, pos); pos += bytesWritten; } while (byteBuffer.hasRemaining()); byteCount -= toWrite; } finally { byteBuffer.clear(); } } } }
FileOperator { public void write(long pos, Buffer source, long byteCount) throws IOException { if (byteCount < 0 || byteCount > source.size()) { throw new IndexOutOfBoundsException(); } while (byteCount > 0L) { try { int toWrite = (int) Math.min(BUFFER_SIZE, byteCount); source.read(byteArray, 0, toWrite); byteBuffer.limit(toWrite); do { int bytesWritten = fileChannel.write(byteBuffer, pos); pos += bytesWritten; } while (byteBuffer.hasRemaining()); byteCount -= toWrite; } finally { byteBuffer.clear(); } } } FileOperator(FileChannel fileChannel); }
FileOperator { public void write(long pos, Buffer source, long byteCount) throws IOException { if (byteCount < 0 || byteCount > source.size()) { throw new IndexOutOfBoundsException(); } while (byteCount > 0L) { try { int toWrite = (int) Math.min(BUFFER_SIZE, byteCount); source.read(byteArray, 0, toWrite); byteBuffer.limit(toWrite); do { int bytesWritten = fileChannel.write(byteBuffer, pos); pos += bytesWritten; } while (byteBuffer.hasRemaining()); byteCount -= toWrite; } 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 write(long pos, Buffer source, long byteCount) throws IOException { if (byteCount < 0 || byteCount > source.size()) { throw new IndexOutOfBoundsException(); } while (byteCount > 0L) { try { int toWrite = (int) Math.min(BUFFER_SIZE, byteCount); source.read(byteArray, 0, toWrite); byteBuffer.limit(toWrite); do { int bytesWritten = fileChannel.write(byteBuffer, pos); pos += bytesWritten; } while (byteBuffer.hasRemaining()); byteCount -= toWrite; } finally { byteBuffer.clear(); } } } FileOperator(FileChannel fileChannel); FileChannel getFileChannel(); void write(long pos, Buffer source, long byteCount); void read(long pos, Buffer sink, long byteCount);  }