You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ah...@apache.org on 2019/12/30 22:17:29 UTC

[commons-codec] 02/05: Update the method to get the little-endian int.

This is an automated email from the ASF dual-hosted git repository.

aherbert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-codec.git

commit bc82d82d2a70200f5c7a084908f3587fbe8cc204
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Mon Dec 30 21:43:31 2019 +0000

    Update the method to get the little-endian int.
    
    The previous version used a generic method for variable length bytes
    always with a fixed length of 4.
---
 .../org/apache/commons/codec/digest/XXHash32.java  | 31 ++++++++--------------
 1 file changed, 11 insertions(+), 20 deletions(-)

diff --git a/src/main/java/org/apache/commons/codec/digest/XXHash32.java b/src/main/java/org/apache/commons/codec/digest/XXHash32.java
index 0fce378..140d08b 100644
--- a/src/main/java/org/apache/commons/codec/digest/XXHash32.java
+++ b/src/main/java/org/apache/commons/codec/digest/XXHash32.java
@@ -162,8 +162,18 @@ public class XXHash32 implements Checksum {
         return hash & 0xffffffffl;
     }
 
+    /**
+     * Gets the little-endian int from 4 bytes starting at the specified index.
+     *
+     * @param buffer The data
+     * @param idx The index
+     * @return The little-endian int
+     */
     private static int getInt(final byte[] buffer, final int idx) {
-        return (int) (fromLittleEndian(buffer, idx, 4) & 0xffffffffl);
+        return ((buffer[idx    ] & 0xff)      ) |
+               ((buffer[idx + 1] & 0xff) <<  8) |
+               ((buffer[idx + 2] & 0xff) << 16) |
+               ((buffer[idx + 3] & 0xff) << 24);
     }
 
     private void initializeState() {
@@ -192,23 +202,4 @@ public class XXHash32 implements Checksum {
 
         stateUpdated = true;
     }
-
-    /**
-     * Reads the given byte array as a little endian long.
-     * @param bytes the byte array to convert
-     * @param off the offset into the array that starts the value
-     * @param length the number of bytes representing the value
-     * @return the number read
-     * @throws IllegalArgumentException if len is bigger than eight
-     */
-    private static long fromLittleEndian(final byte[] bytes, final int off, final int length) {
-        if (length > 8) {
-            throw new IllegalArgumentException("can't read more than eight bytes into a long value");
-        }
-        long l = 0;
-        for (int i = 0; i < length; i++) {
-            l |= (bytes[off + i] & 0xffl) << (8 * i);
-        }
-        return l;
-    }
 }