Package org.apache.commons.io.input
Class ChecksumInputStream
- java.lang.Object
-
- java.io.InputStream
-
- java.io.FilterInputStream
-
- org.apache.commons.io.input.ProxyInputStream
-
- org.apache.commons.io.input.CountingInputStream
-
- org.apache.commons.io.input.ChecksumInputStream
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
public final class ChecksumInputStream extends CountingInputStream
Automatically verifies aChecksumvalue once the stream is exhausted or the count threshold is reached.If the
Checksumdoes not meet the expected value when exhausted, then the input stream throws anIOException.If you do not need the verification or threshold feature, then use a plain
CheckedInputStream.To build an instance, use
ChecksumInputStream.Builder.- Since:
- 2.16.0
- See Also:
ChecksumInputStream.Builder
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classChecksumInputStream.BuilderBuilds a newChecksumInputStream.
-
Field Summary
Fields Modifier and Type Field Description private longcountThresholdThe count threshold to limit how much input is consumed to update theChecksumbefore the input stream validates its value.private longexpectedChecksumValueThe expected checksum.
-
Constructor Summary
Constructors Modifier Constructor Description privateChecksumInputStream(java.io.InputStream in, java.util.zip.Checksum checksum, long expectedChecksumValue, long countThreshold)Constructs a new instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidafterRead(int n)Adds the number of read bytes to the count.static ChecksumInputStream.Builderbuilder()Constructs a newChecksumInputStream.Builder.private java.util.zip.ChecksumgetChecksum()Gets the current checksum value.longgetRemaining()Gets the byte count remaining to read.-
Methods inherited from class org.apache.commons.io.input.CountingInputStream
getByteCount, getCount, resetByteCount, resetCount, skip
-
Methods inherited from class org.apache.commons.io.input.ProxyInputStream
available, beforeRead, close, handleIOException, mark, markSupported, read, read, read, reset, unwrap
-
-
-
-
Field Detail
-
expectedChecksumValue
private final long expectedChecksumValue
The expected checksum.
-
countThreshold
private final long countThreshold
The count threshold to limit how much input is consumed to update theChecksumbefore the input stream validates its value.By default, all input updates the
Checksum.
-
-
Constructor Detail
-
ChecksumInputStream
private ChecksumInputStream(java.io.InputStream in, java.util.zip.Checksum checksum, long expectedChecksumValue, long countThreshold)Constructs a new instance.- Parameters:
in- the stream to wrap.checksum- a Checksum implementation.expectedChecksumValue- the expected checksum.countThreshold- the count threshold to limit how much input is consumed, a negative number means the threshold is unbound.
-
-
Method Detail
-
builder
public static ChecksumInputStream.Builder builder()
Constructs a newChecksumInputStream.Builder.- Returns:
- a new
ChecksumInputStream.Builder.
-
afterRead
protected void afterRead(int n) throws java.io.IOExceptionDescription copied from class:CountingInputStreamAdds the number of read bytes to the count.- Overrides:
afterReadin classCountingInputStream- Parameters:
n- number of bytes read, or -1 if no more bytes are available- Throws:
java.io.IOException- Not thrown here but subclasses may throw.
-
getChecksum
private java.util.zip.Checksum getChecksum()
Gets the current checksum value.- Returns:
- the current checksum value.
-
getRemaining
public long getRemaining()
Gets the byte count remaining to read.- Returns:
- bytes remaining to read, a negative number means the threshold is unbound.
-
-