sahilTakiar commented on a change in pull request #597: HDFS-3246: pRead 
equivalent for direct read path
URL: https://github.com/apache/hadoop/pull/597#discussion_r267932621
 
 

 ##########
 File path: 
hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/crypto/CryptoInputStream.java
 ##########
 @@ -375,7 +397,70 @@ private void decrypt(long position, byte[] buffer, int 
offset, int length)
       returnDecryptor(decryptor);
     }
   }
-  
+
+  /**
+   * Decrypt n bytes in buf starting at start. Output is also put into buf
+   * starting at current position. buf.position() and buf.limit() should be
+   * unchanged after decryption. It is thread-safe.
+   *
+   * <p>
+   *   This method decrypts the input buf chunk-by-chunk and writes the
+   *   decrypted output back into the input buf. It uses two local buffers
+   *   taken from the {@link #bufferPool} to assist in this process: one is
+   *   designated as the input buffer and it stores a single chunk of the
+   *   given buf, the other is designated as the output buffer, which stores
+   *   the output of decrypting the input buffer. Both buffers are of size
+   *   {@link #bufferSize}.
+   * </p>
+   *
+   * <p>
+   *   Decryption is done by using a {@link Decryptor} and the
+   *   {@link #decrypt(Decryptor, ByteBuffer, ByteBuffer, byte)} method. Once
+   *   the decrypted data is written into the output buffer, is is copied back
+   *   into buf. Both buffers are returned back into the pool once the entire
+   *   buf is decrypted.
+   * </p>
+   */
+  private void decrypt(long position, ByteBuffer buf, int n, int start)
+          throws IOException {
+    ByteBuffer localInBuffer = getBuffer();
+    ByteBuffer localOutBuffer = getBuffer();
+    final int pos = buf.position();
+    final int limit = buf.limit();
+    int len = 0;
+    Decryptor localDecryptor = null;
+    try {
+      localDecryptor = getDecryptor();
+      byte[] localIV = initIV.clone();
+      updateDecryptor(localDecryptor, position, localIV);
+      byte localPadding = getPadding(position);
+      // Set proper position for inputdata.
+      localInBuffer.position(localPadding);
+
+      while (len < n) {
+        buf.position(start + len);
+        buf.limit(start + len + Math.min(n - len, localInBuffer.remaining()));
+        localInBuffer.put(buf);
+        // Do decryption
+        try {
+          decrypt(localDecryptor, localInBuffer, localOutBuffer, localPadding);
+          buf.position(start + len);
+          buf.limit(limit);
 
 Review comment:
   Well the invariant you want to preserve is that `buf.put(inputBuf)` should 
only be called with the original value of `buf.limit()` so that you don't 
exceed the given limit. Using `start + len + Math.min(n - len, 
localInBuffer.remaining())` as the limit could violate this if `n + start > 
buf.limit()`.

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: common-issues-unsubscr...@hadoop.apache.org
For additional commands, e-mail: common-issues-h...@hadoop.apache.org

Reply via email to