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

abstract class AbstractStreamingHasher extends AbstractHasher
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 Details

    • buffer

      private final 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 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 per process(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 of bufferSize size, which must be a multiple of chunkSize.
      Parameters:
      chunkSize - the number of bytes available per process(ByteBuffer) invocation; must be at least 4
      bufferSize - the size of the internal buffer. Must be a multiple of chunkSize
  • Method Details

    • process

      protected abstract void process(ByteBuffer bb)
      Processes the available bytes of the buffer (at most chunk bytes).
    • processRemaining

      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).

    • 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.
      Specified by:
      putBytes in interface Hasher
      Specified by:
      putBytes in interface PrimitiveSink
      Overrides:
      putBytes in class AbstractHasher
      Parameters:
      bytes - a byte array
      off - the start offset in the array
      len - the number of bytes to write
      Returns:
      this instance
    • putBytes

      public final Hasher putBytes(ByteBuffer readBuffer)
      Description copied from interface: PrimitiveSink
      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.
      Specified by:
      putBytes in interface Hasher
      Specified by:
      putBytes in interface PrimitiveSink
      Overrides:
      putBytes in class AbstractHasher
      Parameters:
      readBuffer - a byte buffer
      Returns:
      this instance
    • putBytesInternal

      private Hasher putBytesInternal(ByteBuffer readBuffer)
    • 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.
      Specified by:
      putShort in interface Hasher
      Specified by:
      putShort in interface PrimitiveSink
      Overrides:
      putShort in class AbstractHasher
    • putChar

      public final Hasher putChar(char c)
      Description copied from interface: PrimitiveSink
      Puts a character into this sink.
      Specified by:
      putChar in interface Hasher
      Specified by:
      putChar in interface PrimitiveSink
      Overrides:
      putChar in class AbstractHasher
    • putInt

      public final Hasher putInt(int i)
      Description copied from interface: PrimitiveSink
      Puts an int into this sink.
      Specified by:
      putInt in interface Hasher
      Specified by:
      putInt in interface PrimitiveSink
      Overrides:
      putInt in class AbstractHasher
    • putLong

      public final Hasher putLong(long l)
      Description copied from interface: PrimitiveSink
      Puts a long into this sink.
      Specified by:
      putLong in interface Hasher
      Specified by:
      putLong in interface PrimitiveSink
      Overrides:
      putLong in class AbstractHasher
    • 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

      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(java.nio.ByteBuffer) and any leftover bytes that did not make a complete chunk are handled with processRemaining(java.nio.ByteBuffer).
    • munchIfFull

      private void munchIfFull()
    • munch

      private void munch()