Package com.google.common.hash
Class AbstractStreamingHasher
java.lang.Object
com.google.common.hash.AbstractHasher
com.google.common.hash.AbstractStreamingHasher
- All Implemented Interfaces:
Hasher,PrimitiveSink
- Direct Known Subclasses:
Crc32cHashFunction.Crc32cHasher,Murmur3_128HashFunction.Murmur3_128Hasher,SipHashFunction.SipHasher
A convenience base class for implementors of
Hasher; handles accumulating data until an
entire "chunk" (of implementation-dependent length) is ready to be hashed.-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final ByteBufferBuffer via which we pass data to the hash algorithm (the implementor)private final intNumber of bytes to be filled before process() invocation(s).private final intNumber of bytes processed per process() invocation. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAbstractStreamingHasher(int chunkSize) Constructor for use by subclasses.protectedAbstractStreamingHasher(int chunkSize, int bufferSize) Constructor for use by subclasses. -
Method Summary
Modifier and TypeMethodDescriptionfinal HashCodehash()Computes a hash code based on the data that have been provided to this hasher.protected abstract HashCodemakeHash()Computes a hash code based on the data that have been provided to this hasher.private voidmunch()private voidprotected abstract voidprocess(ByteBuffer bb) Processes the available bytes of the buffer (at mostchunkbytes).protected voidThis is invoked for the last bytes of the input, which are not enough to fill a whole chunk.final HasherputByte(byte b) Puts a byte into this sink.final HasherputBytes(byte[] bytes, int off, int len) Puts a chunk of an array of bytes into this sink.final HasherputBytes(ByteBuffer readBuffer) Puts the remaining bytes of a byte buffer into this sink.private HasherputBytesInternal(ByteBuffer readBuffer) final HasherputChar(char c) Puts a character into this sink.final HasherputInt(int i) Puts an int into this sink.final HasherputLong(long l) Puts a long into this sink.final HasherputShort(short s) Puts a short into this sink.Methods inherited from class com.google.common.hash.AbstractHasher
putBoolean, putBytes, putDouble, putFloat, putObject, putString, putUnencodedChars
-
Field Details
-
buffer
Buffer via which we pass data to the hash algorithm (the implementor) -
bufferSize
private final int bufferSizeNumber of bytes to be filled before process() invocation(s). -
chunkSize
private final int chunkSizeNumber of bytes processed per process() invocation.
-
-
Constructor Details
-
AbstractStreamingHasher
protected AbstractStreamingHasher(int chunkSize) Constructor for use by subclasses. This hasher instance will process chunks of the specified size.- Parameters:
chunkSize- the number of bytes available perprocess(ByteBuffer)invocation; must be at least 4
-
AbstractStreamingHasher
protected AbstractStreamingHasher(int chunkSize, int bufferSize) Constructor for use by subclasses. This hasher instance will process chunks of the specified size, using an internal buffer ofbufferSizesize, which must be a multiple ofchunkSize.- Parameters:
chunkSize- the number of bytes available perprocess(ByteBuffer)invocation; must be at least 4bufferSize- the size of the internal buffer. Must be a multiple of chunkSize
-
-
Method Details
-
process
Processes the available bytes of the buffer (at mostchunkbytes). -
processRemaining
This is invoked for the last bytes of the input, which are not enough to fill a whole chunk. The passedByteBufferis guaranteed to be non-empty.This implementation simply pads with zeros and delegates to
process(ByteBuffer). -
putBytes
Description copied from interface:PrimitiveSinkPuts a chunk of an array of bytes into this sink.bytes[off]is the first byte written,bytes[off + len - 1]is the last.- Specified by:
putBytesin interfaceHasher- Specified by:
putBytesin interfacePrimitiveSink- Overrides:
putBytesin classAbstractHasher- Parameters:
bytes- a byte arrayoff- the start offset in the arraylen- the number of bytes to write- Returns:
- this instance
-
putBytes
Description copied from interface:PrimitiveSinkPuts the remaining bytes of a byte buffer into this sink.bytes.position()is the first byte written,bytes.limit() - 1is the last. The position of the buffer will be equal to the limit when this method returns.- Specified by:
putBytesin interfaceHasher- Specified by:
putBytesin interfacePrimitiveSink- Overrides:
putBytesin classAbstractHasher- Parameters:
readBuffer- a byte buffer- Returns:
- this instance
-
putBytesInternal
-
putByte
Description copied from interface:PrimitiveSinkPuts a byte into this sink.- Parameters:
b- a byte- Returns:
- this instance
-
putShort
Description copied from interface:PrimitiveSinkPuts a short into this sink.- Specified by:
putShortin interfaceHasher- Specified by:
putShortin interfacePrimitiveSink- Overrides:
putShortin classAbstractHasher
-
putChar
Description copied from interface:PrimitiveSinkPuts a character into this sink.- Specified by:
putCharin interfaceHasher- Specified by:
putCharin interfacePrimitiveSink- Overrides:
putCharin classAbstractHasher
-
putInt
Description copied from interface:PrimitiveSinkPuts an int into this sink.- Specified by:
putIntin interfaceHasher- Specified by:
putIntin interfacePrimitiveSink- Overrides:
putIntin classAbstractHasher
-
putLong
Description copied from interface:PrimitiveSinkPuts a long into this sink.- Specified by:
putLongin interfaceHasher- Specified by:
putLongin interfacePrimitiveSink- Overrides:
putLongin classAbstractHasher
-
hash
Description copied from interface:HasherComputes a hash code based on the data that have been provided to this hasher. The result is unspecified if this method is called more than once on the same instance. -
makeHash
Computes a hash code based on the data that have been provided to this hasher. This is called after all chunks are handled withprocess(java.nio.ByteBuffer)and any leftover bytes that did not make a complete chunk are handled withprocessRemaining(java.nio.ByteBuffer). -
munchIfFull
private void munchIfFull() -
munch
private void munch()
-