Package com.helger.security.mac
Class MacInputStream
- java.lang.Object
-
- java.io.InputStream
-
- java.io.FilterInputStream
-
- com.helger.commons.io.stream.WrappedInputStream
-
- com.helger.security.mac.MacInputStream
-
- All Implemented Interfaces:
Closeable,AutoCloseable
public class MacInputStream extends com.helger.commons.io.stream.WrappedInputStreamA 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 Mac after your calls to one of this Mac input stream'sreadmethods.It is possible to turn this stream on or off (see
on). When it is on, a call to one of thereadmethods 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
-
-
Field Summary
Fields Modifier and Type Field Description static booleanDEFAULT_ON-
Fields inherited from class java.io.FilterInputStream
in
-
-
Constructor Summary
Constructors Constructor Description MacInputStream(InputStream aIS, Mac aMac)Creates a Mac input stream, using the specified input 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()intread()Reads a byte, and updates the Mac (if the function is on).intread(byte[] aBuf, int nOfs, int nLen)Reads into a byte array, and updates the Mac (if the function is on).voidsetMac(Mac aMac)Associates the specified Mac with this stream.voidsetOn(boolean bOn)Turns the function on or off.StringtoString()-
Methods inherited from class java.io.FilterInputStream
available, close, mark, markSupported, read, reset, skip
-
Methods inherited from class java.io.InputStream
nullInputStream, readAllBytes, readNBytes, readNBytes, transferTo
-
-
-
-
Field Detail
-
DEFAULT_ON
public static final boolean DEFAULT_ON
- See Also:
- Constant Field Values
-
-
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 thereadmethods 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
-
read
public int read() throws IOExceptionReads 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 (seeon), this method will then callupdateon the Mac associated with this stream, passing it the byte read.- Overrides:
readin classFilterInputStream- 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 tolenbytes from the input stream into the arrayb, starting at offsetoff. This method blocks until the data is actually read. If the function is on (seeon), this method will then callupdateon the Mac associated with this stream, passing it the data.- Overrides:
readin classFilterInputStream- Parameters:
aBuf- the array into which the data is read.nOfs- the starting offset intobof where the data should be placed.nLen- the maximum number of bytes to be read from the input stream into b, starting at offsetoff.- Returns:
- the actual number of bytes read. This is less than
lenif the end of the stream is reached prior to readinglenbytes. -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:
toStringin classcom.helger.commons.io.stream.WrappedInputStream
-
-