Package java.security

Class DigestInputStream

  • All Implemented Interfaces:
    Closeable, AutoCloseable

    public class DigestInputStream
    extends FilterInputStream
    DigestInputStream is a FilterInputStream which maintains an associated message digest.
    • Field Detail

      • digest

        protected MessageDigest digest
        The message digest for this stream.
    • Constructor Detail

      • DigestInputStream

        public DigestInputStream​(InputStream stream,
                                 MessageDigest digest)
        Constructs a new instance of this DigestInputStream, using the given stream and the digest.

        Warning: passing a null source creates an invalid DigestInputStream. All operations on such a stream will fail.

        Parameters:
        stream - the input stream.
        digest - the message digest.
    • Method Detail

      • getMessageDigest

        public MessageDigest getMessageDigest()
        Returns the message digest for this stream.
        Returns:
        the message digest for this stream.
      • setMessageDigest

        public void setMessageDigest​(MessageDigest digest)
        Sets the message digest which this stream will use.
        Parameters:
        digest - the message digest which this stream will use.
      • read

        public int read()
                 throws IOException
        Reads the next byte and returns it as an int. Updates the digest for the byte if this function is on(boolean).

        This operation is blocking.

        Overrides:
        read in class FilterInputStream
        Returns:
        the byte which was read or -1 at end of stream.
        Throws:
        IOException - if reading the source stream causes an IOException.
      • read

        public int read​(byte[] buffer,
                        int byteOffset,
                        int byteCount)
                 throws IOException
        Reads up to byteCount bytes into buffer, starting at byteOffset. Updates the digest if this function is on(boolean).

        This operation is blocking.

        Returns the number of bytes actually read or -1 if the end of the filtered stream has been reached while reading.

        Overrides:
        read in class FilterInputStream
        Throws:
        IOException - if reading the source stream causes an IOException
      • on

        public void on​(boolean on)
        Enables or disables the digest function (default is on).
        Parameters:
        on - true if the digest should be computed, false otherwise.
        See Also:
        MessageDigest
      • toString

        public String toString()
        Returns a string containing a concise, human-readable description of this DigestInputStream including the digest.
        Overrides:
        toString in class Object
        Returns:
        a printable representation for this DigestInputStream.