Class DigestInputStream
java.lang.Object
java.io.InputStream
java.io.FilterInputStream
java.security.DigestInputStream
- All Implemented Interfaces:
Closeable, AutoCloseable
A transparent stream that updates the associated message digest using
the bits going through the stream.
To complete the message digest computation, call one of the
digest methods on the associated message
digest after your calls to one of this digest 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 message digest. But when it is off,
the message digest is not updated. The default is for the stream
to be on.
Note that digest objects can compute only one digest (see
MessageDigest),
so that in order to compute intermediate digests, a caller should
retain a handle onto the digest object, and clone it for each
digest to be computed, leaving the original digest untouched.
- Implementation Note:
- This implementation only updates the message digest
with data actually read from the input stream when it is
turned on. This includes the various
readmethods,transferTo,readAllBytes, andreadNBytes. Please note that data bypassed by theskipmethod are ignored. On the other hand, if the underlying stream supports themarkandresetmethods, and the same data is read again afterreset, then the message digest is updated again. - Since:
- 1.2
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected MessageDigestThe message digest associated with this stream.Fields declared in class FilterInputStream
in -
Constructor Summary
ConstructorsConstructorDescriptionDigestInputStream(InputStream stream, MessageDigest digest) Creates a digest input stream, using the specified input stream and message digest. -
Method Summary
Modifier and TypeMethodDescriptionReturns the message digest associated with this stream.voidon(boolean on) Turns the digest function on or off.intread()Reads a byte, and updates the message digest (if the digest function is on).intread(byte[] b, int off, int len) Reads into a byte array, and updates the message digest (if the digest function is on).voidsetMessageDigest(MessageDigest digest) Associates the specified message digest with this stream.toString()Prints a string representation of this digest input stream and its associated message digest object.Methods declared in class FilterInputStream
available, close, mark, markSupported, read, reset, skipModifier and TypeMethodDescriptionintReturns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next caller of a method for this input stream.voidclose()Closes this input stream and releases any system resources associated with the stream.voidmark(int readlimit) Marks the current position in this input stream.booleanTests if this input stream supports themarkandresetmethods.intread(byte[] b) Reads up tob.lengthbytes of data from this input stream into an array of bytes.voidreset()Repositions this stream to the position at the time themarkmethod was last called on this input stream.longskip(long n) Skips over and discardsnbytes of data from the input stream.Methods declared in class InputStream
nullInputStream, readAllBytes, readNBytes, readNBytes, skipNBytes, transferToModifier and TypeMethodDescriptionstatic InputStreamReturns a newInputStreamthat reads no bytes.byte[]Reads all remaining bytes from the input stream.intreadNBytes(byte[] b, int off, int len) Reads the requested number of bytes from the input stream into the given byte array.byte[]readNBytes(int len) Reads up to a specified number of bytes from the input stream.voidskipNBytes(long n) Skips over and discards exactlynbytes of data from this input stream.longtransferTo(OutputStream out) Reads all bytes from this input stream and writes the bytes to the given output stream in the order that they are read.Methods declared in class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitModifier and TypeMethodDescriptionprotected Objectclone()Answers a new instance of the same class as the receiver, whose slots have been filled in with the values in the slots of the receiver.booleanCompares the argument to the receiver, and answers true if they represent the same object using a class specific comparison.protected voidfinalize()Deprecated, for removal: This API element is subject to removal in a future version.May cause performance issues, deadlocks and hangs.getClass()Answers the unique instance of java.lang.Class which represents the class of the receiver.inthashCode()Answers an integer hash code for the receiver.final voidnotify()Causes one thread which iswaiting on the receiver to be made ready to run.final voidCauses all threads which arewaiting on the receiver to be made ready to run.final voidwait()Causes the thread which sent this message to be made not ready to run pending some change in the receiver (as indicated bynotifyornotifyAll).final voidwait(long time) Causes the thread which sent this message to be made not ready to run either pending some change in the receiver (as indicated bynotifyornotifyAll) or the expiration of the timeout.final voidwait(long time, int frac) Causes the thread which sent this message to be made not ready to run either pending some change in the receiver (as indicated bynotifyornotifyAll) or the expiration of the timeout.
-
Field Details
-
digest
The message digest associated with this stream.
-
-
Constructor Details
-
DigestInputStream
Creates a digest input stream, using the specified input stream and message digest.- Parameters:
stream- the input stream.digest- the message digest to associate with this stream.
-
-
Method Details
-
getMessageDigest
Returns the message digest associated with this stream.- Returns:
- the message digest associated with this stream.
- See Also:
-
setMessageDigest
Associates the specified message digest with this stream.- Parameters:
digest- the message digest to be associated with this stream.- See Also:
-
read
Reads a byte, and updates the message digest (if the digest function is on). That is, this method reads a byte from the input stream, blocking until the byte is actually read. If the digest function is on (seeon), this method will then callupdateon the message digest 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:
-
read
Reads into a byte array, and updates the message digest (if the digest 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 digest function is on (seeon), this method will then callupdateon the message digest associated with this stream, passing it the data.- Overrides:
readin classFilterInputStream- Parameters:
b- the array into which the data is read.off- the starting offset intobof where the data should be placed.len- 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:
-
on
public void on(boolean on) Turns the digest function on or off. The default is on. When it is on, a call to one of thereadmethods results in an update on the message digest. But when it is off, the message digest is not updated.- Parameters:
on-trueto turn the digest function on,falseto turn it off.
-
toString
-