@Override public void update(final int b) { oneByte[0] = (byte) (b & 0xff); update(oneByte, 0, 1); }
private static int getInt(final byte[] buffer, final int idx) { return (int) (fromLittleEndian(buffer, idx, 4) & 0xffffffffl); }
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; }
@Test public void verifyChecksum() throws IOException { final XXHash32 h = new XXHash32(); final FileInputStream s = new FileInputStream(file); try { final byte[] b = toByteArray(s); h.update(b, 0, b.length); } finally { s.close(); } Assert.assertEquals("checksum for " + file.getName(), expectedChecksum, Long.toHexString(h.getValue())); }
@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); } }
/** * Creates an XXHash32 instance. * @param seed the seed to use */ public XXHash32(final int seed) { this.seed = seed; initializeState(); }
@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); }
/** * Creates an XXHash32 instance. * @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 static int getInt(final byte[] buffer, final int idx) { return (int) (fromLittleEndian(buffer, idx, 4) & 0xffffffffl); }
@Override public void reset() { initializeState(); totalLen = 0; pos = 0; }
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 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; }