Class MessageDigestCalculatingInputStream

All Implemented Interfaces:
Closeable, AutoCloseable

public class MessageDigestCalculatingInputStream extends ObservableInputStream
This class is an example for using an ObservableInputStream. It creates its own ObservableInputStream.Observer, which calculates a checksum using a MessageDigest, for example an MD5 sum. Note: Neither ObservableInputStream, nor MessageDigest, are thread safe. So is MessageDigestCalculatingInputStream.
  • Field Details

  • Constructor Details

    • MessageDigestCalculatingInputStream

      public MessageDigestCalculatingInputStream(InputStream inputStream, MessageDigest messageDigest)
      Creates a new instance, which calculates a signature on the given stream, using the given MessageDigest.
      Parameters:
      inputStream - the stream to calculate the message digest for
      messageDigest - the message digest to use
    • MessageDigestCalculatingInputStream

      public MessageDigestCalculatingInputStream(InputStream inputStream, String algorithm) throws NoSuchAlgorithmException
      Creates a new instance, which calculates a signature on the given stream, using a MessageDigest with the given algorithm.
      Parameters:
      inputStream - the stream to calculate the message digest for
      algorithm - the name of the algorithm to use
      Throws:
      NoSuchAlgorithmException - if no Provider supports a MessageDigestSpi implementation for the specified algorithm.
    • MessageDigestCalculatingInputStream

      public MessageDigestCalculatingInputStream(InputStream inputStream) throws NoSuchAlgorithmException
      Creates a new instance, which calculates a signature on the given stream, using a MessageDigest with the "MD5" algorithm.
      Parameters:
      inputStream - the stream to calculate the message digest for
      Throws:
      NoSuchAlgorithmException - if no Provider supports a MessageDigestSpi implementation for the specified algorithm.
  • Method Details

    • getMessageDigest

      public MessageDigest getMessageDigest()
      Returns the MessageDigest, which is being used for generating the checksum. Note: The checksum will only reflect the data, which has been read so far. This is probably not, what you expect. Make sure, that the complete data has been read, if that is what you want. The easiest way to do so is by invoking ObservableInputStream.consume().
      Returns:
      the message digest used