Top Description Fields Constructors Methods
com.google.common.hash

pack-priv abstract Class AbstractStreamingHasher

extends AbstractHasher
Class Inheritance
Known Direct Subclasses
com.google.common.hash.Crc32cHashFunction.Crc32cHasher, com.google.common.hash.Murmur3_128HashFunction.Murmur3_128Hasher, com.google.common.hash.SipHashFunction.SipHasher
Annotations
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.base.Preconditions.checkArgument

A convenience base class for implementors of Hasher; handles accumulating data until an entire "chunk" (of implementation-dependent length) is ready to be hashed.
Authors
Kevin Bourrillion, Dimitris Andreou

Field Summary

Modifier and TypeField and Description
private final ByteBuffer
buffer

Buffer via which we pass data to the hash algorithm (the implementor)

private final int
bufferSize

Number of bytes to be filled before process() invocation(s).

private final int
chunkSize

Number of bytes processed per process() invocation.

Constructor Summary

AccessConstructor and Description
protected
AbstractStreamingHasher(int
the number of bytes available per process(ByteBuffer) invocation; must be at least 4
chunkSize
)

Constructor for use by subclasses.

protected
AbstractStreamingHasher(int
the number of bytes available per process(ByteBuffer) invocation; must be at least 4
chunkSize
,
int
the size of the internal buffer. Must be a multiple of chunkSize
bufferSize
)

Constructor for use by subclasses.

Method Summary

Modifier and TypeMethod and Description
public final HashCode
hash()

Implements com.google.common.hash.Hasher.hash.

Computes a hash code based on the data that have been provided to this hasher.
protected abstract HashCode
makeHash()

Computes a hash code based on the data that have been provided to this hasher.

private void
private void
protected abstract void
process(ByteBuffer bb)

Processes the available bytes of the buffer (at most chunk bytes).

protected void
processRemaining(ByteBuffer bb)

This is invoked for the last bytes of the input, which are not enough to fill a whole chunk.

public final Hasher
putByte(byte
a byte
b
)

Implements com.google.common.hash.Hasher.putByte.

Puts a byte into this sink.
public final Hasher
putBytes(byte[]
a byte array
bytes
,
int
the start offset in the array
off
,
int
the number of bytes to write
len
)

Overrides com.google.common.hash.AbstractHasher.putBytes.

Implements com.google.common.hash.Hasher.putBytes.

Puts a chunk of an array of bytes into this sink.
public final Hasher
putBytes(ByteBuffer
a byte buffer
readBuffer
)

Overrides com.google.common.hash.AbstractHasher.putBytes.

Implements com.google.common.hash.Hasher.putBytes.

Puts the remaining bytes of a byte buffer into this sink.
private Hasher
public final Hasher
putChar(char c)

Overrides com.google.common.hash.AbstractHasher.putChar.

Implements com.google.common.hash.Hasher.putChar.

Puts a character into this sink.
public final Hasher
putInt(int i)

Overrides com.google.common.hash.AbstractHasher.putInt.

Implements com.google.common.hash.Hasher.putInt.

Puts an int into this sink.
public final Hasher
putLong(long l)

Overrides com.google.common.hash.AbstractHasher.putLong.

Implements com.google.common.hash.Hasher.putLong.

Puts a long into this sink.
public final Hasher
putShort(short s)

Overrides com.google.common.hash.AbstractHasher.putShort.

Implements com.google.common.hash.Hasher.putShort.

Puts a short into this sink.
Inherited from com.google.common.hash.AbstractHasher:
putBooleanputBytesputDoubleputFloatputObjectputStringputUnencodedChars

Field Detail

bufferback to summary
private final ByteBuffer buffer

Buffer via which we pass data to the hash algorithm (the implementor)

bufferSizeback to summary
private final int bufferSize

Number of bytes to be filled before process() invocation(s).

chunkSizeback to summary
private final int chunkSize

Number of bytes processed per process() invocation.

Constructor Detail

AbstractStreamingHasherback to summary
protected AbstractStreamingHasher(int chunkSize)

Constructor for use by subclasses. This hasher instance will process chunks of the specified size.

Parameters
chunkSize:int

the number of bytes available per process(ByteBuffer) invocation; must be at least 4

AbstractStreamingHasherback to summary
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 of bufferSize size, which must be a multiple of chunkSize.

Parameters
chunkSize:int

the number of bytes available per process(ByteBuffer) invocation; must be at least 4

bufferSize:int

the size of the internal buffer. Must be a multiple of chunkSize

Method Detail

hashback to summary
public final HashCode hash()

Implements com.google.common.hash.Hasher.hash.

Doc from com.google.common.hash.Hasher.hash.

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.

Annotations
@Override
makeHashback to summary
protected abstract HashCode makeHash()

Computes a hash code based on the data that have been provided to this hasher. This is called after all chunks are handled with process and any leftover bytes that did not make a complete chunk are handled with processRemaining.

munchback to summary
private void munch()
munchIfFullback to summary
private void munchIfFull()
processback to summary
protected abstract void process(ByteBuffer bb)

Processes the available bytes of the buffer (at most chunk bytes).

processRemainingback to summary
protected void processRemaining(ByteBuffer bb)

This is invoked for the last bytes of the input, which are not enough to fill a whole chunk. The passed ByteBuffer is guaranteed to be non-empty.

This implementation simply pads with zeros and delegates to process(ByteBuffer).

putByteback to summary
public final Hasher putByte(byte b)

Implements com.google.common.hash.Hasher.putByte.

Doc from com.google.common.hash.PrimitiveSink.putByte.

Puts a byte into this sink.

Parameters
b:byte

a byte

Returns:Hasher

this instance

Annotations
@Override
@CanIgnoreReturnValue
putBytesback to summary
public final Hasher putBytes(byte[] bytes, int off, int len)

Overrides com.google.common.hash.AbstractHasher.putBytes.

Implements com.google.common.hash.Hasher.putBytes.

Doc from com.google.common.hash.PrimitiveSink.putBytes.

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:byte[]

a byte array

off:int

the start offset in the array

len:int

the number of bytes to write

Returns:Hasher

this instance

Annotations
@Override
@CanIgnoreReturnValue
putBytesback to summary
public final Hasher putBytes(ByteBuffer readBuffer)

Overrides com.google.common.hash.AbstractHasher.putBytes.

Implements com.google.common.hash.Hasher.putBytes.

Doc from com.google.common.hash.PrimitiveSink.putBytes.

Puts the remaining bytes of a byte buffer into this sink. bytes.position() is the first byte written, bytes.limit() - 1 is the last. The position of the buffer will be equal to the limit when this method returns.

Parameters
readBuffer:ByteBuffer

a byte buffer

Returns:Hasher

this instance

Annotations
@Override
@CanIgnoreReturnValue
putBytesInternalback to summary
private Hasher putBytesInternal(ByteBuffer readBuffer)
Annotations
@CanIgnoreReturnValue
putCharback to summary
public final Hasher putChar(char c)

Overrides com.google.common.hash.AbstractHasher.putChar.

Implements com.google.common.hash.Hasher.putChar.

Doc from com.google.common.hash.PrimitiveSink.putChar.

Puts a character into this sink.

Annotations
@Override
@CanIgnoreReturnValue
putIntback to summary
public final Hasher putInt(int i)

Overrides com.google.common.hash.AbstractHasher.putInt.

Implements com.google.common.hash.Hasher.putInt.

Doc from com.google.common.hash.PrimitiveSink.putInt.

Puts an int into this sink.

Annotations
@Override
@CanIgnoreReturnValue
putLongback to summary
public final Hasher putLong(long l)

Overrides com.google.common.hash.AbstractHasher.putLong.

Implements com.google.common.hash.Hasher.putLong.

Doc from com.google.common.hash.PrimitiveSink.putLong.

Puts a long into this sink.

Annotations
@Override
@CanIgnoreReturnValue
putShortback to summary
public final Hasher putShort(short s)

Overrides com.google.common.hash.AbstractHasher.putShort.

Implements com.google.common.hash.Hasher.putShort.

Doc from com.google.common.hash.PrimitiveSink.putShort.

Puts a short into this sink.

Annotations
@Override
@CanIgnoreReturnValue