vinaysbadami commented on a change in pull request #2464:
URL: https://github.com/apache/hadoop/pull/2464#discussion_r523919820



##########
File path: 
hadoop-tools/hadoop-azure/src/main/java/org/apache/hadoop/fs/azurebfs/services/AbfsInputStream.java
##########
@@ -162,14 +172,86 @@ private int readOneBlock(final byte[] b, final int off, 
final int len) throws IO
       throw new IndexOutOfBoundsException();
     }
 
-    //If buffer is empty, then fill the buffer.
-    if (bCursor == limit) {
+    if (firstRead && this.readSmallFilesCompletely
+        && contentLength <= bufferSize) { //  Read small files completely
+      if (readFileCompletely() == -1) {
+        return -1;
+      }
+    } else if (firstRead && this.optimizeFooterRead && fCursor
+        == contentLength - FOOTER_DELTA) {  //  Read the last one block if the
+      // read is for the footer
+      if (readLastBlock() == -1) {
+        return -1;
+      }
+    } else {
+      if (readOneBlock(b) == -1) {
+        return -1;
+      }
+    }
+    fCursorAfterLastRead = fCursor;
+
+    //If there is anything in the buffer, then return lesser of (requested 
bytes) and (bytes in buffer)
+    //(bytes returned may be less than requested)
+    int bytesRemaining = limit - bCursor;
+    int bytesToRead = Math.min(len, bytesRemaining);
+    System.arraycopy(buffer, bCursor, b, off, bytesToRead);
+    bCursor += bytesToRead;
+    if (statistics != null) {
+      statistics.incrementBytesRead(bytesToRead);
+    }
+    if (streamStatistics != null) {
+      // Bytes read from the local buffer.
+      streamStatistics.bytesReadFromBuffer(bytesToRead);
+      streamStatistics.bytesRead(bytesToRead);
+    }
+    return bytesToRead;
+  }
+
+  private long readFileCompletely() throws IOException {
+    bCursor = (int) getPos();
+    //  Read full file in case the file size <= buffer size
+    buffer = new byte[bufferSize];
+    long bytesRead = readInternal(0, buffer, 0, (int) contentLength, true);
+    firstRead = false;
+
+    if (bytesRead == -1) {
+      return -1;
+    }
+
+    limit = (int) bytesRead;
+    fCursor = bytesRead;
+    return bytesRead;
+  }
+
+  private long readLastBlock() throws IOException {
+    bCursor = (int) (
+        ((contentLength < bufferSize) ? contentLength : bufferSize)
+            - FOOTER_DELTA);
+    buffer = new byte[bufferSize];
+    long startPos = (contentLength < bufferSize)
+        ? 0
+        : contentLength - bufferSize;
+    long bytesRead = readInternal(startPos, buffer, 0, bufferSize, true);
+    firstRead = false;
+
+    if (bytesRead == -1) {
+      return -1;
+    }
+
+    limit += (int) bytesRead;

Review comment:
       limit = bytesread




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

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to