Class HashingInputStream

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable

    @Beta
    public final class HashingInputStream
    extends java.io.FilterInputStream
    An InputStream that maintains a hash of the data read from it.
    Since:
    16.0
    Author:
    Qian Huang
    • Field Summary

      • Fields inherited from class java.io.FilterInputStream

        in
    • Constructor Summary

      Constructors 
      Constructor Description
      HashingInputStream​(HashFunction hashFunction, java.io.InputStream in)
      Creates an input stream that hashes using the given HashFunction and delegates all data read from it to the underlying InputStream.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      HashCode hash()
      Returns the HashCode based on the data read from this stream.
      void mark​(int readlimit)
      mark() is not supported for HashingInputStream
      boolean markSupported()
      mark() is not supported for HashingInputStream
      int read()
      Reads the next byte of data from the underlying input stream and updates the hasher with the byte read.
      int read​(byte[] bytes, int off, int len)
      Reads the specified bytes of data from the underlying input stream and updates the hasher with the bytes read.
      void reset()
      reset() is not supported for HashingInputStream.
      • Methods inherited from class java.io.FilterInputStream

        available, close, read, skip
      • Methods inherited from class java.io.InputStream

        nullInputStream, readAllBytes, readNBytes, readNBytes, transferTo
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • HashingInputStream

        public HashingInputStream​(HashFunction hashFunction,
                                  java.io.InputStream in)
        Creates an input stream that hashes using the given HashFunction and delegates all data read from it to the underlying InputStream.

        The InputStream should not be read from before or after the hand-off.

    • Method Detail

      • read

        @CanIgnoreReturnValue
        public int read()
                 throws java.io.IOException
        Reads the next byte of data from the underlying input stream and updates the hasher with the byte read.
        Overrides:
        read in class java.io.FilterInputStream
        Throws:
        java.io.IOException
      • read

        @CanIgnoreReturnValue
        public int read​(byte[] bytes,
                        int off,
                        int len)
                 throws java.io.IOException
        Reads the specified bytes of data from the underlying input stream and updates the hasher with the bytes read.
        Overrides:
        read in class java.io.FilterInputStream
        Throws:
        java.io.IOException
      • markSupported

        public boolean markSupported()
        mark() is not supported for HashingInputStream
        Overrides:
        markSupported in class java.io.FilterInputStream
        Returns:
        false always
      • mark

        public void mark​(int readlimit)
        mark() is not supported for HashingInputStream
        Overrides:
        mark in class java.io.FilterInputStream
      • reset

        public void reset()
                   throws java.io.IOException
        reset() is not supported for HashingInputStream.
        Overrides:
        reset in class java.io.FilterInputStream
        Throws:
        java.io.IOException - this operation is not supported
      • hash

        public HashCode hash()
        Returns the HashCode based on the data read from this stream. The result is unspecified if this method is called more than once on the same instance.