Package com.helger.security.mac
Class MacOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- java.io.FilterOutputStream
-
- com.helger.commons.io.stream.WrappedOutputStream
-
- com.helger.security.mac.MacOutputStream
-
- All Implemented Interfaces:
Closeable,Flushable,AutoCloseable
public class MacOutputStream extends com.helger.commons.io.stream.WrappedOutputStreamA transparent stream that updates the associated Mac using the bits going through the stream.To complete the Mac computation, call one of the
doFinalmethods on the associated message digest after your calls to one of this digest output stream'swritemethods.It is possible to turn this stream on or off (see
on). When it is on, a call to one of thewritemethods 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
-
-
Field Summary
Fields Modifier and Type Field Description static booleanDEFAULT_ON-
Fields inherited from class java.io.FilterOutputStream
out
-
-
Constructor Summary
Constructors Constructor Description MacOutputStream(OutputStream aOS, Mac aMac)Creates a Mac output stream, using the specified output stream and Mac.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description MacgetMac()Returns the Mac associated with this stream.booleanisOn()voidsetMac(Mac aMac)Associates the specified Mac with this stream.voidsetOn(boolean bOn)Turns the function on or off.StringtoString()voidwrite(byte[] aBuf, int nOfs, int nLen)Updates the Mac (if the function is on) using the specified subarray, and in any case writes the subarray to the output stream.voidwrite(int b)Updates the Mac (if the function is on) using the specified byte, and in any case writes the byte to the output stream.-
Methods inherited from class com.helger.commons.io.stream.WrappedOutputStream
getWrappedOutputStream
-
Methods inherited from class java.io.FilterOutputStream
close, flush, write
-
Methods inherited from class java.io.OutputStream
nullOutputStream
-
-
-
-
Field Detail
-
DEFAULT_ON
public static final boolean DEFAULT_ON
- See Also:
- Constant Field Values
-
-
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 benull.- See Also:
getMac()
-
write
public void write(int b) throws IOExceptionUpdates 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 (seeon), this method callsupdateon the message digest associated with this stream, passing it the byteb. This method then writes the byte to the output stream, blocking until the byte is actually written.- Overrides:
writein classFilterOutputStream- 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 (seeon), this method callsupdateon 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:
writein classcom.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 intobof the first byte to be updated and written.nLen- the number of bytes of data to be updated and written fromb, starting at offsetoff.- 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 thewritemethods results in an update on the Mac. But when it is off, the Mac is not updated.- Parameters:
bOn-trueto turn the function on,falseto turn it off.
-
isOn
public final boolean isOn()
- Returns:
trueif Mac processing is on,falseif it is off
-
toString
public String toString()
- Overrides:
toStringin classcom.helger.commons.io.stream.WrappedOutputStream
-
-