Package com.google.common.hash
Class AbstractStreamingHashFunction.AbstractStreamingHasher
- java.lang.Object
-
- com.google.common.hash.AbstractHasher
-
- com.google.common.hash.AbstractStreamingHashFunction.AbstractStreamingHasher
-
- All Implemented Interfaces:
Hasher
,PrimitiveSink
- Direct Known Subclasses:
Murmur3_128HashFunction.Murmur3_128Hasher
,Murmur3_32HashFunction.Murmur3_32Hasher
,SipHashFunction.SipHasher
- Enclosing class:
- AbstractStreamingHashFunction
protected abstract static class AbstractStreamingHashFunction.AbstractStreamingHasher extends AbstractHasher
A convenience base class for implementors ofHasher
; handles accumulating data until an entire "chunk" (of implementation-dependent length) is ready to be hashed.
-
-
Field Summary
Fields Modifier and Type Field Description private java.nio.ByteBuffer
buffer
Buffer via which we pass data to the hash algorithm (the implementor)private int
bufferSize
Number of bytes to be filled before process() invocation(s).private int
chunkSize
Number of bytes processed per process() invocation.
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractStreamingHasher(int chunkSize)
Constructor for use by subclasses.protected
AbstractStreamingHasher(int chunkSize, int bufferSize)
Constructor for use by subclasses.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description HashCode
hash()
Computes a hash code based on the data that have been provided to this hasher.(package private) abstract HashCode
makeHash()
private void
munch()
private void
munchIfFull()
protected abstract void
process(java.nio.ByteBuffer bb)
Processes the available bytes of the buffer (at mostchunk
bytes).protected void
processRemaining(java.nio.ByteBuffer bb)
This is invoked for the last bytes of the input, which are not enough to fill a whole chunk.Hasher
putByte(byte b)
Puts a byte into this sink.Hasher
putBytes(byte[] bytes)
Puts an array of bytes into this sink.Hasher
putBytes(byte[] bytes, int off, int len)
Puts a chunk of an array of bytes into this sink.private Hasher
putBytes(java.nio.ByteBuffer readBuffer)
Hasher
putChar(char c)
Puts a character into this sink.Hasher
putInt(int i)
Puts an int into this sink.Hasher
putLong(long l)
Puts a long into this sink.<T> Hasher
putObject(T instance, Funnel<? super T> funnel)
A simple convenience forfunnel.funnel(object, this)
.Hasher
putShort(short s)
Puts a short into this sink.Hasher
putUnencodedChars(java.lang.CharSequence charSequence)
Equivalent to processing eachchar
value in theCharSequence
, in order.-
Methods inherited from class com.google.common.hash.AbstractHasher
putBoolean, putDouble, putFloat, putString
-
-
-
-
Field Detail
-
buffer
private final java.nio.ByteBuffer buffer
Buffer via which we pass data to the hash algorithm (the implementor)
-
bufferSize
private final int bufferSize
Number of bytes to be filled before process() invocation(s).
-
chunkSize
private final int chunkSize
Number of bytes processed per process() invocation.
-
-
Constructor Detail
-
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 ofbufferSize
size, 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 Detail
-
process
protected abstract void process(java.nio.ByteBuffer bb)
Processes the available bytes of the buffer (at mostchunk
bytes).
-
processRemaining
protected void processRemaining(java.nio.ByteBuffer bb)
This is invoked for the last bytes of the input, which are not enough to fill a whole chunk. The passedByteBuffer
is guaranteed to be non-empty.This implementation simply pads with zeros and delegates to
process(ByteBuffer)
.
-
putBytes
public final Hasher putBytes(byte[] bytes)
Description copied from interface:PrimitiveSink
Puts an array of bytes into this sink.- Parameters:
bytes
- a byte array- Returns:
- this instance
-
putBytes
public final Hasher putBytes(byte[] bytes, int off, int len)
Description copied from interface:PrimitiveSink
Puts a chunk of an array of bytes into this sink.bytes[off]
is the first byte written,bytes[off + len - 1]
is the last.- Parameters:
bytes
- a byte arrayoff
- the start offset in the arraylen
- the number of bytes to write- Returns:
- this instance
-
putBytes
private Hasher putBytes(java.nio.ByteBuffer readBuffer)
-
putUnencodedChars
public final Hasher putUnencodedChars(java.lang.CharSequence charSequence)
Description copied from interface:Hasher
Equivalent to processing eachchar
value in theCharSequence
, in order. In other words, no character encoding is performed; the low byte and high byte of eachchar
are hashed directly (in that order). The input must not be updated while this method is in progress.Warning: This method will produce different output than most other languages do when running the same hash function on the equivalent input. For cross-language compatibility, use
Hasher.putString(java.lang.CharSequence, java.nio.charset.Charset)
, usually with a charset of UTF-8. For other use cases, useputUnencodedChars
.- Specified by:
putUnencodedChars
in interfaceHasher
- Specified by:
putUnencodedChars
in interfacePrimitiveSink
- Overrides:
putUnencodedChars
in classAbstractHasher
-
putByte
public final Hasher putByte(byte b)
Description copied from interface:PrimitiveSink
Puts a byte into this sink.- Parameters:
b
- a byte- Returns:
- this instance
-
putShort
public final Hasher putShort(short s)
Description copied from interface:PrimitiveSink
Puts a short into this sink.
-
putChar
public final Hasher putChar(char c)
Description copied from interface:PrimitiveSink
Puts a character into this sink.
-
putInt
public final Hasher putInt(int i)
Description copied from interface:PrimitiveSink
Puts an int into this sink.
-
putLong
public final Hasher putLong(long l)
Description copied from interface:PrimitiveSink
Puts a long into this sink.
-
putObject
public final <T> Hasher putObject(T instance, Funnel<? super T> funnel)
Description copied from interface:Hasher
A simple convenience forfunnel.funnel(object, this)
.
-
hash
public final HashCode hash()
Description copied from interface:Hasher
Computes 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
abstract HashCode makeHash()
-
munchIfFull
private void munchIfFull()
-
munch
private void munch()
-
-