Class MacOutputStream

  • All Implemented Interfaces:
    Closeable, Flushable, AutoCloseable

    public class MacOutputStream
    extends com.helger.commons.io.stream.WrappedOutputStream
    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 message digest after your calls to one of this digest output stream's write methods.

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

    Since:
    9.1.7
    Author:
    Philip Helger
    • Constructor Detail

      • MacOutputStream

        public MacOutputStream​(@Nonnull
                               OutputStream aOS,
                               @Nonnull
                               Mac aMac)
        Creates a Mac output stream, using the specified output stream and Mac.
        Parameters:
        aOS - the output 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. May not be null.
        See Also:
        getMac()
      • write

        public void write​(int b)
                   throws IOException
        Updates the Mac (if the function is on) using the specified byte, and in any case writes the byte to the output stream. That is, if the digest function is on (see on), this method calls update on the message digest associated with this stream, passing it the byte b. This method then writes the byte to the output stream, blocking until the byte is actually written.
        Overrides:
        write in class FilterOutputStream
        Parameters:
        b - the byte to be used for updating and writing to the output stream.
        Throws:
        IOException - if an I/O error occurs.
        See Also:
        Mac.update(byte)
      • write

        public void write​(@Nonnull
                          byte[] aBuf,
                          @Nonnegative
                          int nOfs,
                          @Nonnegative
                          int nLen)
                   throws IOException
        Updates the Mac (if the function is on) using the specified subarray, and in any case writes the subarray to the output stream. That is, if the digest function is on (see on), this method calls update on the Mac associated with this stream, passing it the subarray specifications. This method then writes the subarray bytes to the output stream, blocking until the bytes are actually written.
        Overrides:
        write in class com.helger.commons.io.stream.WrappedOutputStream
        Parameters:
        aBuf - the array containing the subarray to be used for updating and writing to the output stream.
        nOfs - the offset into b of the first byte to be updated and written.
        nLen - the number of bytes of data to be updated and written from b, starting at offset off.
        Throws:
        IOException - if an I/O error occurs.
        See Also:
        Mac.update(byte[], int, int)
      • 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 write 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
      • toString

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