poorbarcode commented on code in PR #4140:
URL: https://github.com/apache/bookkeeper/pull/4140#discussion_r1413034614


##########
circe-checksum/src/main/java/com/scurrilous/circe/checksum/Java9IntHash.java:
##########
@@ -106,14 +107,23 @@ public int resume(int current, ByteBuf buffer, int 
offset, int len) {
         } else if (buffer.hasArray()) {
             int arrayOffset = buffer.arrayOffset() + offset;
             negCrc = resume(negCrc, buffer.array(), arrayOffset, len);
+        } else if (buffer instanceof CompositeByteBuf) {
+           CompositeByteBuf compositeByteBuf = (CompositeByteBuf) buffer;
+           int loopedCurrent = current;
+           for (int i = 0; i < compositeByteBuf.numComponents(); i ++) {
+               loopedCurrent = resume(loopedCurrent, 
compositeByteBuf.component(i));
+           }
+           return loopedCurrent;
         } else {
             byte[] b = TL_BUFFER.get();
             int toRead = len;
+            int loopOffset = offset;
             while (toRead > 0) {
                 int length = Math.min(toRead, b.length);
-                buffer.slice(offset, len).readBytes(b, 0, length);
+                buffer.slice(loopOffset, length).readBytes(b, 0, length);
                 negCrc = resume(negCrc, b, 0, length);
                 toRead -= length;
+                loopOffset += length;
             }
         }
 

Review Comment:
   Moving `return` to every logic branch would make `~` everywhere(just like 
below). So I think current implementation is better to read: 
   
   ```java
   public int resume(int current, ByteBuf buffer, int offset, int len) {
       if (buffer.hasMemoryAddress()) {
           return ~resume(~current, buffer.memoryAddress(), offset, len);
       }
       if (buffer.hasArray()) {
           int arrayOffset = buffer.arrayOffset() + offset;
           return ~resume(~current, buffer.array(), arrayOffset, len);
       }
       if (buffer instanceof CompositeByteBuf) {
          CompositeByteBuf compositeByteBuf = (CompositeByteBuf) buffer;
          int loopedCurrent = current;
          for (int i = 0; i < compositeByteBuf.numComponents(); i ++) {
              loopedCurrent = resume(loopedCurrent, 
compositeByteBuf.component(i));
          }
          return loopedCurrent;
       }
       int negCrc = ~current;
       byte[] b = TL_BUFFER.get();
       int toRead = len;
       int loopOffset = offset;
       while (toRead > 0) {
           int length = Math.min(toRead, b.length);
           buffer.slice(loopOffset, length).readBytes(b, 0, length);
           negCrc = resume(negCrc, b, 0, length);
           toRead -= length;
           loopOffset += length;
       }
       return ~negCrc;
   }
   
   ```



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