Class MacInputStream

  • All Implemented Interfaces:
    Closeable, AutoCloseable

    public class MacInputStream
    extends com.helger.commons.io.stream.WrappedInputStream
    A transparent stream that updates the associated Mac using the bits going through the stream.

    To complete the Mac computation, call one of the doFinal methods on the associated Mac after your calls to one of this Mac input stream's read methods.

    It is possible to turn this stream on or off (see on). When it is on, a call to one of the read methods results in an update on the Mac. But when it is off, the Mac is not updated. The default is for the stream to be on.

    Note that Mac objects can compute only one digest (see Mac), so that in order to compute intermediate digests, a caller should retain a handle onto the Mac object, and clone it for each digest to be computed, leaving the original digest untouched.

    Since:
    9.1.7
    Author:
    Philip Helger
    • Constructor Detail

      • MacInputStream

        public MacInputStream​(@Nonnull
                              InputStream aIS,
                              @Nonnull
                              Mac aMac)
        Creates a Mac input stream, using the specified input stream and Mac.
        Parameters:
        aIS - the input stream.
        aMac - the Mac to associate with this stream.
    • Method Detail

      • getMac

        @Nonnull
        public final Mac getMac()
        Returns the Mac associated with this stream.
        Returns:
        the Mac associated with this stream.
        See Also:
        setMac(Mac)
      • setMac

        public final void setMac​(@Nonnull
                                 Mac aMac)
        Associates the specified Mac with this stream.
        Parameters:
        aMac - the Mac to be associated with this stream.
        See Also:
        getMac()
      • setOn

        public final void setOn​(boolean bOn)
        Turns the function on or off. The default is on. When it is on, a call to one of the read methods results in an update on the Mac. But when it is off, the Mac is not updated.
        Parameters:
        bOn - true to turn the function on, false to turn it off.
      • isOn

        public final boolean isOn()
        Returns:
        true if Mac processing is on, false if it is off
      • read

        public int read()
                 throws IOException
        Reads a byte, and updates the Mac (if the function is on). That is, this method reads a byte from the input stream, blocking until the byte is actually read. If the function is on (see on), this method will then call update on the Mac associated with this stream, passing it the byte read.
        Overrides:
        read in class FilterInputStream
        Returns:
        the byte read.
        Throws:
        IOException - if an I/O error occurs.
        See Also:
        Mac.update(byte)
      • read

        public int read​(@Nonnull
                        byte[] aBuf,
                        @Nonnegative
                        int nOfs,
                        @Nonnegative
                        int nLen)
                 throws IOException
        Reads into a byte array, and updates the Mac (if the function is on). That is, this method reads up to len bytes from the input stream into the array b, starting at offset off. This method blocks until the data is actually read. If the function is on (see on), this method will then call update on the Mac associated with this stream, passing it the data.
        Overrides:
        read in class FilterInputStream
        Parameters:
        aBuf - the array into which the data is read.
        nOfs - the starting offset into b of where the data should be placed.
        nLen - the maximum number of bytes to be read from the input stream into b, starting at offset off.
        Returns:
        the actual number of bytes read. This is less than len if the end of the stream is reached prior to reading len bytes. -1 is returned if no bytes were read because the end of the stream had already been reached when the call was made.
        Throws:
        IOException - if an I/O error occurs.
        See Also:
        Mac.update(byte[], int, int)
      • toString

        public String toString()
        Overrides:
        toString in class com.helger.commons.io.stream.WrappedInputStream