abstract class AbstractStreamingHasher extends AbstractHasher
Hasher
; handles accumulating data until an
entire "chunk" (of implementation-dependent length) is ready to be hashed.Modifier and Type | Field and 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.
|
Modifier | Constructor and Description |
---|---|
protected |
AbstractStreamingHasher(int chunkSize)
Constructor for use by subclasses.
|
protected |
AbstractStreamingHasher(int chunkSize,
int bufferSize)
Constructor for use by subclasses.
|
Modifier and Type | Method and Description |
---|---|
HashCode |
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 |
munch() |
private void |
munchIfFull() |
protected abstract void |
process(java.nio.ByteBuffer bb)
Processes the available bytes of the buffer (at most
chunk 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,
int off,
int len)
Puts a chunk of an array of bytes into this sink.
|
Hasher |
putBytes(java.nio.ByteBuffer readBuffer)
Puts the remaining bytes of a byte buffer into this sink.
|
private Hasher |
putBytesInternal(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.
|
Hasher |
putShort(short s)
Puts a short into this sink.
|
putBoolean, putBytes, putDouble, putFloat, putObject, putString, putUnencodedChars
private final java.nio.ByteBuffer buffer
private final int bufferSize
private final int chunkSize
protected AbstractStreamingHasher(int chunkSize)
chunkSize
- the number of bytes available per process(ByteBuffer)
invocation;
must be at least 4protected AbstractStreamingHasher(int chunkSize, int bufferSize)
bufferSize
size, which must be a multiple of chunkSize
.chunkSize
- the number of bytes available per process(ByteBuffer)
invocation;
must be at least 4bufferSize
- the size of the internal buffer. Must be a multiple of chunkSizeprotected abstract void process(java.nio.ByteBuffer bb)
chunk
bytes).protected void processRemaining(java.nio.ByteBuffer bb)
ByteBuffer
is guaranteed to be non-empty.
This implementation simply pads with zeros and delegates to process(ByteBuffer)
.
public final Hasher putBytes(byte[] bytes, int off, int len)
PrimitiveSink
bytes[off]
is the first byte written,
bytes[off + len - 1]
is the last.putBytes
in interface Hasher
putBytes
in interface PrimitiveSink
putBytes
in class AbstractHasher
bytes
- a byte arrayoff
- the start offset in the arraylen
- the number of bytes to writepublic final Hasher putBytes(java.nio.ByteBuffer readBuffer)
PrimitiveSink
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.putBytes
in interface Hasher
putBytes
in interface PrimitiveSink
putBytes
in class AbstractHasher
readBuffer
- a byte bufferprivate Hasher putBytesInternal(java.nio.ByteBuffer readBuffer)
public final Hasher putByte(byte b)
PrimitiveSink
b
- a bytepublic final Hasher putShort(short s)
PrimitiveSink
putShort
in interface Hasher
putShort
in interface PrimitiveSink
putShort
in class AbstractHasher
public final Hasher putChar(char c)
PrimitiveSink
putChar
in interface Hasher
putChar
in interface PrimitiveSink
putChar
in class AbstractHasher
public final Hasher putInt(int i)
PrimitiveSink
putInt
in interface Hasher
putInt
in interface PrimitiveSink
putInt
in class AbstractHasher
public final Hasher putLong(long l)
PrimitiveSink
putLong
in interface Hasher
putLong
in interface PrimitiveSink
putLong
in class AbstractHasher
public final HashCode hash()
Hasher
protected abstract HashCode makeHash()
process(java.nio.ByteBuffer)
and any leftover bytes that did not make a
complete chunk are handled with processRemaining(java.nio.ByteBuffer)
.private void munchIfFull()
private void munch()