Flaugh24 commented on code in PR #2390:
URL: https://github.com/apache/ignite-3/pull/2390#discussion_r1296035876


##########
modules/file-transfer/src/main/java/org/apache/ignite/internal/network/file/ChunkedFileReader.java:
##########
@@ -51,63 +61,59 @@ private ChunkedFileReader(String fileName, RandomAccessFile 
raf, int chunkSize)
      * @param file File.
      * @param chunkSize Chunk size.
      * @return Chunked file reader.
-     * @throws IOException If an I/O error occurs.
+     * @throws FileNotFoundException If the file does not exist.
      */
-    static ChunkedFileReader open(File file, int chunkSize) throws IOException 
{
-        return new ChunkedFileReader(file.getName(), new 
RandomAccessFile(file, "r"), chunkSize);
+    static ChunkedFileReader open(File file, int chunkSize) throws 
FileNotFoundException {
+        return new ChunkedFileReader(file.length(), new 
BufferedInputStream(new FileInputStream(file)), chunkSize);
     }
 
     /**
      * Returns {@code false} if there are no more chunks to read. Otherwise, 
returns {@code true}.
      *
      * @return {@code false} if there are no more chunks to read. Otherwise, 
returns {@code true}.
      */
-    boolean hasNextChunk() throws IOException {
-        return raf.getFilePointer() != raf.length();
+    boolean hasNextChunk() {
+        return offset < length;
     }
 
     /**
-     * Reads the next chunk.
+     * Reads the next chunk. If there are no more chunks to read, throws an 
exception. If the last chunk is read successfully, closes the
+     * file.
      *
      * @return Chunk data.
+     * @throws IllegalStateException If there are no more chunks to read.
      * @throws IOException If an I/O error occurs.
      */
     byte[] readNextChunk() throws IOException {
         if (!hasNextChunk()) {
-            throw new IOException("No more chunks to read");
+            throw new IllegalStateException("No more chunks to read");
         }
 
-        int toRead = (int) Math.min(chunkSize, length() - offset());
+        int toRead = (int) Math.min(chunkSize, length - offset);
         byte[] data = new byte[toRead];
-        raf.read(data);
-        return data;
-    }
+        stream.read(data);
+        offset += toRead;
+        nextChunkNumber++;
 
-    /**
-     * Returns the file name.
-     *
-     * @return File name.
-     */
-    String fileName() {
-        return fileName;
-    }
+        if (offset == length) {
+            stream.close();
+        }
 
-    /**
-     * Returns the current chunk offset.
-     *
-     * @return Chunk size.
-     */
-    long offset() throws IOException {
-        return raf.getFilePointer();
+        return data;
     }
 
     /**
-     * Returns the file length.
+     * Returns the number of the next chunk to read.
      *
-     * @return File length.
+     * @return The number of the next chunk to read.
+     * @throws IllegalStateException If there are no more chunks to read.
      */
-    long length() throws IOException {
-        return raf.length();
+    int nextChunkNumber() {
+        if (!hasNextChunk()) {
+            throw new IllegalStateException("No more chunks to read");

Review Comment:
   I would expect that there is a next chunk if it has a number. So we have to 
return a special value (e.g., -1) or throw an exception to indicate that there 
are no more chunks to read. Actually, I don't see a difference between those 
approaches because it is expected that a user has checked the absence of the 
next chunk by calling hasNextChunk().



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