@Override public Hasher putBytes(byte[] bytes, int off, int len) { for (Hasher hasher : hashers) { hasher.putBytes(bytes, off, len); } return this; }
@Override public Hasher putBytes(byte[] bytes) { for (Hasher hasher : hashers) { hasher.putBytes(bytes); } return this; }
public StatisticsHasher putBytes(byte[] bytes) { hasher.putBytes(bytes); return this; }
public StatisticsHasher putBytes(byte[] bytes, int off, int len) { hasher.putBytes(bytes, off, len); return this; }
@Override public void write(byte[] bytes, int off, int len) throws IOException { hasher.putBytes(bytes, off, len); out.write(bytes, off, len); }
@Override public Hasher putBytes(ByteBuffer bytes) { int pos = bytes.position(); for (Hasher hasher : hashers) { bytes.position(pos); hasher.putBytes(bytes); } return this; }
/** * Reads the specified bytes of data from the underlying input stream and updates the hasher with * the bytes read. */ @Override @CanIgnoreReturnValue public int read(byte[] bytes, int off, int len) throws IOException { int numOfBytesRead = in.read(bytes, off, len); if (numOfBytesRead != -1) { hasher.putBytes(bytes, off, numOfBytesRead); } return numOfBytesRead; }
@Override public HashCode hashBytes(ByteBuffer input) { return newHasher(input.remaining()).putBytes(input).hash(); }
@Override public HashCode hashBytes(ByteBuffer input) { return newHasher(input.remaining()).putBytes(input).hash(); }
@Override public HashCode hashBytes(byte[] input, int off, int len) { checkPositionIndexes(off, off + len, input.length); return newHasher(len).putBytes(input, off, len).hash(); }
private static void assertHashBytesEquivalence(HashFunction hashFunction, Random random) { int size = random.nextInt(2048); byte[] bytes = new byte[size]; random.nextBytes(bytes); assertEquals( hashFunction.hashBytes(bytes), hashFunction.newHasher(size).putBytes(bytes).hash()); int off = random.nextInt(size); int len = random.nextInt(size - off); assertEquals( hashFunction.hashBytes(bytes, off, len), hashFunction.newHasher(size).putBytes(bytes, off, len).hash()); }
private static void assertHashByteBufferEquivalence(HashFunction hashFunction, Random random) { int size = random.nextInt(2048); byte[] bytes = new byte[size]; random.nextBytes(bytes); assertEquals( hashFunction.hashBytes(ByteBuffer.wrap(bytes)), hashFunction.newHasher(size).putBytes(ByteBuffer.wrap(bytes)).hash()); int off = random.nextInt(size); int len = random.nextInt(size - off); assertEquals( hashFunction.hashBytes(ByteBuffer.wrap(bytes, off, len)), hashFunction.newHasher(size).putBytes(ByteBuffer.wrap(bytes, off, len)).hash()); }
static void assertHasherByteBufferPreservesByteOrder(HashFunction hashFunction) { Random rng = new Random(0L); byte[] bytes = new byte[rng.nextInt(256) + 1]; rng.nextBytes(bytes); ByteBuffer littleEndian = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN); ByteBuffer bigEndian = ByteBuffer.wrap(bytes).order(ByteOrder.BIG_ENDIAN); assertEquals( hashFunction.newHasher().putBytes(littleEndian).hash(), hashFunction.newHasher().putBytes(littleEndian).hash()); assertEquals(ByteOrder.LITTLE_ENDIAN, littleEndian.order()); assertEquals(ByteOrder.BIG_ENDIAN, littleEndian.order()); }
public void testWrite_putByteArray() throws Exception { byte[] buf = new byte[] {'y', 'a', 'm', 's'}; HashingOutputStream out = new HashingOutputStream(hashFunction, buffer); out.write(buf); verify(hashFunction).newHasher(); verify(hasher).putBytes(buf, 0, buf.length); verifyNoMoreInteractions(hashFunction, hasher); }
public void testWrite_putByteArrayAtPos() throws Exception { byte[] buf = new byte[] {'y', 'a', 'm', 's'}; HashingOutputStream out = new HashingOutputStream(hashFunction, buffer); out.write(buf, 0, 3); verify(hashFunction).newHasher(); verify(hasher).putBytes(buf, 0, 3); verifyNoMoreInteractions(hashFunction, hasher); }
public void testRead_putByteArrayAtPos() throws Exception { HashingInputStream in = new HashingInputStream(hashFunction, buffer); byte[] buf = new byte[3]; int numOfByteRead = in.read(buf, 0, 3); assertEquals(3, numOfByteRead); for (int i = 0; i < numOfByteRead; i++) { assertEquals(testBytes[i], buf[i]); } verify(hasher).putBytes(Arrays.copyOf(testBytes, 3), 0, 3); verify(hashFunction).newHasher(); verifyNoMoreInteractions(hashFunction, hasher); }
public void testRead_putByteArray() throws Exception { HashingInputStream in = new HashingInputStream(hashFunction, buffer); byte[] buf = new byte[4]; int numOfByteRead = in.read(buf, 0, buf.length); assertEquals(4, numOfByteRead); for (int i = 0; i < testBytes.length; i++) { assertEquals(testBytes[i], buf[i]); } verify(hasher).putBytes(testBytes, 0, testBytes.length); verify(hashFunction).newHasher(); verifyNoMoreInteractions(hashFunction, hasher); }
private static void assertSip(byte[] input, long expected) { assertEquals(expected, SIP_WITH_KEY.hashBytes(input).asLong()); assertEquals(expected, SIP_WITH_KEY.newHasher().putBytes(input).hash().asLong()); assertEquals(expected, SIP_WITHOUT_KEY.hashBytes(input).asLong()); assertEquals(expected, SIP_WITHOUT_KEY.newHasher().putBytes(input).hash().asLong()); } }
public void testRead_putByteArrayOutOfBound() throws Exception { byte[] buf = new byte[100]; byte[] expectedBytes = buf.clone(); System.arraycopy(testBytes, 0, expectedBytes, 0, testBytes.length); HashingInputStream in = new HashingInputStream(hashFunction, buffer); int numOfByteRead = in.read(buf, 0, 100); assertEquals(4, numOfByteRead); for (int i = 0; i < numOfByteRead; i++) { assertEquals(testBytes[i], buf[i]); } verify(hasher).putBytes(expectedBytes, 0, 4); verify(hashFunction).newHasher(); verifyNoMoreInteractions(hashFunction, hasher); }
private static void assertHash(int seed, long expected1, long expected2, String stringInput) { HashCode expected = toHashCode(expected1, expected2); byte[] input = HashTestUtils.ascii(stringInput); assertEquals(expected, murmur3_128(seed).hashBytes(input)); assertEquals(expected, murmur3_128(seed).newHasher().putBytes(input).hash()); }