org.apache.commons.io.input

## Class MessageDigestCalculatingInputStream

- java.lang.Object
- - java.io.InputStream
  - - java.io.FilterInputStream
    - - org.apache.commons.io.input.ProxyInputStream
      - - org.apache.commons.io.input.ObservableInputStream
        - - org.apache.commons.io.input.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.

  To build an instance, see `MessageDigestCalculatingInputStream.Builder`.

  See the MessageDigest section in the Java Cryptography Architecture Standard Algorithm Name Documentation for information about standard algorithm names.

  Note: Neither `ObservableInputStream`, nor `MessageDigest`, are thread safe. So is `MessageDigestCalculatingInputStream`.

  TODO Rename to MessageDigestInputStream in 3.0.

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `static class` | `MessageDigestCalculatingInputStream.Builder` Builds a new `MessageDigestCalculatingInputStream` instance.
    `static class` | `MessageDigestCalculatingInputStream.MessageDigestMaintainingObserver` Maintains the message digest.

    - ### Nested classes/interfaces inherited from class org.apache.commons.io.input.ObservableInputStream

      `ObservableInputStream.Observer`

  - ### Field Summary

    - ### Fields inherited from class java.io.FilterInputStream

      `in`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `MessageDigestCalculatingInputStream(InputStream inputStream)` Deprecated. Use `builder()`, `MessageDigestCalculatingInputStream.Builder`, and `MessageDigestCalculatingInputStream.Builder.get()`.
    `MessageDigestCalculatingInputStream(InputStream inputStream, MessageDigest messageDigest)` Deprecated. Use `builder()`, `MessageDigestCalculatingInputStream.Builder`, and `MessageDigestCalculatingInputStream.Builder.get()`.
    `MessageDigestCalculatingInputStream(InputStream inputStream, String algorithm)` Deprecated. Use `builder()`, `MessageDigestCalculatingInputStream.Builder`, and `MessageDigestCalculatingInputStream.Builder.get()`.

  - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `static MessageDigestCalculatingInputStream.Builder` | `builder()` Constructs a new `MessageDigestCalculatingInputStream.Builder`.
    `MessageDigest` | `getMessageDigest()` Gets the `MessageDigest`, which is being used for generating the checksum.

    - ### Methods inherited from class org.apache.commons.io.input.ObservableInputStream

      `add, close, consume, getObservers, noteClosed, noteDataByte, noteDataBytes, noteError, noteFinished, read, read, read, remove, removeAllObservers`

    - ### Methods inherited from class org.apache.commons.io.input.ProxyInputStream

      `afterRead, available, beforeRead, handleIOException, mark, markSupported, reset, skip`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### MessageDigestCalculatingInputStream

          @Deprecated
          public MessageDigestCalculatingInputStream(InputStream inputStream)
                                                          throws NoSuchAlgorithmException

      Deprecated. Use `builder()`, `MessageDigestCalculatingInputStream.Builder`, and `MessageDigestCalculatingInputStream.Builder.get()`.
      Creates a new instance, which calculates a signature on the given stream, using a `MessageDigest` with the "MD5" algorithm.

      The MD5 algorithm is weak and should not be used.

      Parameters:
        `inputStream` - the stream to calculate the message digest for
      Throws:
        `NoSuchAlgorithmException` - if no Provider supports a MessageDigestSpi implementation for the specified algorithm.

    - #### MessageDigestCalculatingInputStream

          @Deprecated
          public MessageDigestCalculatingInputStream(InputStream inputStream,
                                                                 MessageDigest messageDigest)

      Deprecated. Use `builder()`, `MessageDigestCalculatingInputStream.Builder`, and `MessageDigestCalculatingInputStream.Builder.get()`.
      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

          @Deprecated
          public MessageDigestCalculatingInputStream(InputStream inputStream,
                                                                 String algorithm)
                                                          throws NoSuchAlgorithmException

      Deprecated. Use `builder()`, `MessageDigestCalculatingInputStream.Builder`, and `MessageDigestCalculatingInputStream.Builder.get()`.
      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 requested. See the MessageDigest section in the Java Cryptography Architecture Standard Algorithm Name Documentation for information about standard algorithm names.
      Throws:
        `NoSuchAlgorithmException` - if no Provider supports a MessageDigestSpi implementation for the specified algorithm.

  - ### Method Detail

    - #### builder

          public static MessageDigestCalculatingInputStream.Builder builder()

      Constructs a new `MessageDigestCalculatingInputStream.Builder`.
      Returns:
        a new `MessageDigestCalculatingInputStream.Builder`.
      Since:
        2.12.0

    - #### getMessageDigest

          public MessageDigest getMessageDigest()

      Gets 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

Copyright © 2002–2023 The Apache Software Foundation. All rights reserved.