This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-compress.git
commit a2dd1ef097e80df78d86766106b9f1d829a49515 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Thu Dec 21 21:49:04 2023 -0500 Reimplement for reuse --- .../commons/compress/compressors/lz4/XXHash32.java | 137 +-------------------- 1 file changed, 3 insertions(+), 134 deletions(-) diff --git a/src/main/java/org/apache/commons/compress/compressors/lz4/XXHash32.java b/src/main/java/org/apache/commons/compress/compressors/lz4/XXHash32.java index c1e266281..8846e6a36 100644 --- a/src/main/java/org/apache/commons/compress/compressors/lz4/XXHash32.java +++ b/src/main/java/org/apache/commons/compress/compressors/lz4/XXHash32.java @@ -18,11 +18,6 @@ */ package org.apache.commons.compress.compressors.lz4; -import static java.lang.Integer.rotateLeft; -import static org.apache.commons.compress.utils.ByteUtils.fromLittleEndian; - -import java.util.zip.Checksum; - /** * Implementation of the xxhash32 hash algorithm. * @@ -32,37 +27,13 @@ import java.util.zip.Checksum; * @deprecated Use {@link org.apache.commons.codec.digest.XXHash32}. */ @Deprecated -public class XXHash32 implements Checksum { - - private static final int BUF_SIZE = 16; - private static final int ROTATE_BITS = 13; - - private static final int PRIME1 = (int) 2654435761L; - private static final int PRIME2 = (int) 2246822519L; - private static final int PRIME3 = (int) 3266489917L; - private static final int PRIME4 = 668265263; - private static final int PRIME5 = 374761393; - - private static int getInt(final byte[] buffer, final int idx) { - return (int) (fromLittleEndian(buffer, idx, 4) & 0xffffffffL); - } - - private final byte[] oneByte = new byte[1]; - private final int[] state = new int[4]; - // Note: the code used to use ByteBuffer but the manual method is 50% faster - // See: https://gitbox.apache.org/repos/asf/commons-compress/diff/2f56fb5c - private final byte[] buffer = new byte[BUF_SIZE]; - - private final int seed; - private int totalLen; - - private int pos; +public class XXHash32 extends org.apache.commons.codec.digest.XXHash32 { /** * Creates an XXHash32 instance with a seed of 0. */ public XXHash32() { - this(0); + super(0); } /** @@ -71,108 +42,6 @@ public class XXHash32 implements Checksum { * @param seed the seed to use */ public XXHash32(final int seed) { - this.seed = seed; - initializeState(); - } - - @Override - public long getValue() { - int hash; - if (totalLen > BUF_SIZE) { - hash = rotateLeft(state[0], 1) + rotateLeft(state[1], 7) + rotateLeft(state[2], 12) + rotateLeft(state[3], 18); - } else { - hash = state[2] + PRIME5; - } - hash += totalLen; - - int idx = 0; - final int limit = pos - 4; - for (; idx <= limit; idx += 4) { - hash = rotateLeft(hash + getInt(buffer, idx) * PRIME3, 17) * PRIME4; - } - while (idx < pos) { - hash = rotateLeft(hash + (buffer[idx++] & 0xff) * PRIME5, 11) * PRIME1; - } - - hash ^= hash >>> 15; - hash *= PRIME2; - hash ^= hash >>> 13; - hash *= PRIME3; - hash ^= hash >>> 16; - return hash & 0xffffffffL; - } - - private void initializeState() { - state[0] = seed + PRIME1 + PRIME2; - state[1] = seed + PRIME2; - state[2] = seed; - state[3] = seed - PRIME1; - } - - private void process(final byte[] b, final int offset) { - // local shadows for performance - int s0 = state[0]; - int s1 = state[1]; - int s2 = state[2]; - int s3 = state[3]; - - s0 = rotateLeft(s0 + getInt(b, offset) * PRIME2, ROTATE_BITS) * PRIME1; - s1 = rotateLeft(s1 + getInt(b, offset + 4) * PRIME2, ROTATE_BITS) * PRIME1; - s2 = rotateLeft(s2 + getInt(b, offset + 8) * PRIME2, ROTATE_BITS) * PRIME1; - s3 = rotateLeft(s3 + getInt(b, offset + 12) * PRIME2, ROTATE_BITS) * PRIME1; - - state[0] = s0; - state[1] = s1; - state[2] = s2; - state[3] = s3; - - pos = 0; - } - - @Override - public void reset() { - initializeState(); - totalLen = 0; - pos = 0; - } - - @Override - public void update(final byte[] b, int off, final int len) { - if (len <= 0) { - return; - } - totalLen += len; - - final int end = off + len; - - if (pos + len < BUF_SIZE) { - System.arraycopy(b, off, buffer, pos, len); - pos += len; - return; - } - - if (pos > 0) { - final int size = BUF_SIZE - pos; - System.arraycopy(b, off, buffer, pos, size); - process(buffer, 0); - off += size; - } - - final int limit = end - BUF_SIZE; - while (off <= limit) { - process(b, off); - off += BUF_SIZE; - } - - if (off < end) { - pos = end - off; - System.arraycopy(b, off, buffer, 0, pos); - } - } - - @Override - public void update(final int b) { - oneByte[0] = (byte) (b & 0xff); - update(oneByte, 0, 1); + super(seed); } }