Skip to content

Commit bc82d82

Browse files
committed
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.
1 parent 33491ff commit bc82d82

1 file changed

Lines changed: 11 additions & 20 deletions

File tree

src/main/java/org/apache/commons/codec/digest/XXHash32.java

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,18 @@ public long getValue() {
162162
return hash & 0xffffffffl;
163163
}
164164

165+
/**
166+
* Gets the little-endian int from 4 bytes starting at the specified index.
167+
*
168+
* @param buffer The data
169+
* @param idx The index
170+
* @return The little-endian int
171+
*/
165172
private static int getInt(final byte[] buffer, final int idx) {
166-
return (int) (fromLittleEndian(buffer, idx, 4) & 0xffffffffl);
173+
return ((buffer[idx ] & 0xff) ) |
174+
((buffer[idx + 1] & 0xff) << 8) |
175+
((buffer[idx + 2] & 0xff) << 16) |
176+
((buffer[idx + 3] & 0xff) << 24);
167177
}
168178

169179
private void initializeState() {
@@ -192,23 +202,4 @@ private void process(final byte[] b, final int offset) {
192202

193203
stateUpdated = true;
194204
}
195-
196-
/**
197-
* Reads the given byte array as a little endian long.
198-
* @param bytes the byte array to convert
199-
* @param off the offset into the array that starts the value
200-
* @param length the number of bytes representing the value
201-
* @return the number read
202-
* @throws IllegalArgumentException if len is bigger than eight
203-
*/
204-
private static long fromLittleEndian(final byte[] bytes, final int off, final int length) {
205-
if (length > 8) {
206-
throw new IllegalArgumentException("can't read more than eight bytes into a long value");
207-
}
208-
long l = 0;
209-
for (int i = 0; i < length; i++) {
210-
l |= (bytes[off + i] & 0xffl) << (8 * i);
211-
}
212-
return l;
213-
}
214205
}

0 commit comments

Comments
 (0)