szetszwo commented on code in PR #1488:
URL: https://github.com/apache/ratis/pull/1488#discussion_r3539325983
##########
ratis-examples/src/main/java/org/apache/ratis/examples/filestore/FileInfo.java:
##########
@@ -88,6 +86,29 @@ ByteString read(CheckedFunction<Path, Path, IOException>
resolver, long offset,
}
}
+ void streamRead(CheckedFunction<Path, Path, IOException> resolver, long
offset, long length,
+ WritableByteChannel stream) throws IOException {
+ if (offset + length > getWriteSize()) {
+ throw new IOException("Failed to read Wrote: offset (=" + offset
+ + " + length (=" + length + ") > size = " + getWriteSize()
+ + ", path=" + getRelativePath());
+ }
+
+ try (FileChannel in = FileUtils.newFileChannel(
+ resolver.apply(getRelativePath()), StandardOpenOption.READ)) {
+ long transferred = 0;
+ while (transferred < length) {
+ final long n = in.transferTo(offset + transferred, length -
transferred, stream);
+ if (n <= 0) {
+ break;
+ }
Review Comment:
According to the javadoc, n == 0 is legal and n < 0 will never happen.
Let's add some Preconditions instead.
```java
long transferred = 0;
while (transferred < length) {
final long n = in.transferTo(offset + transferred, length -
transferred, stream);
Preconditions.assertTrue(n >= 0);
transferred += n;
}
Preconditions.assertSame(length, transferred, "transferred");
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]