amaliujia commented on code in PR #1488:
URL: https://github.com/apache/ratis/pull/1488#discussion_r3526119963


##########
ratis-examples/src/main/java/org/apache/ratis/examples/filestore/FileInfo.java:
##########
@@ -88,6 +89,34 @@ 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 (SeekableByteChannel in = Files.newByteChannel(
+        resolver.apply(getRelativePath()), StandardOpenOption.READ)) {
+      in.position(offset);
+      long remaining = length;
+      while (remaining > 0) {
+        final int chunkSize = FileStoreCommon.getChunkSize(remaining);
+        final ByteBuffer buffer = ByteBuffer.allocateDirect(chunkSize);
+        final int n = in.read(buffer);
+        if (n <= 0) {
+          break;
+        }
+        buffer.flip();
+        stream.write(buffer);
+        remaining -= n;
+      }
+    } finally {
+      stream.close();
+    }

Review Comment:
   Done



-- 
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]

Reply via email to