public class DigestInputStream
extends FilterInputStream
| java.lang.Object | |||
| java.io.InputStream | |||
| java.io.FilterInputStream | |||
| java.security.DigestInputStream | |||
一个透明的流,使用通过流的位来更新关联的消息摘要。
要完成消息摘要计算,则需要调用一个 digest方法对相关消息的调用此摘要输入流的一个消化后 read方法。
可以打开或关闭此流(请参阅on )。 打开时,调用其中一个read方法会导致消息摘要更新。 但是当它关闭时,消息摘要不会更新。 默认值是打开流。
请注意,摘要对象只能计算一个摘要(请参阅 MessageDigest ),因此为了计算中间摘要,调用方应该在摘要对象上保留一个句柄,并将其克隆为要计算的每个摘要,从而保持原始摘要不变。
也可以看看:
Fields |
|
|---|---|
protected MessageDigest |
digest 与此流关联的消息摘要。 |
Inherited fields |
|---|
java.io.FilterInputStream
|
Public constructors |
|
|---|---|
DigestInputStream(InputStream stream, MessageDigest digest) 使用指定的输入流和消息摘要创建摘要输入流。 |
|
公共方法(Public methods) |
|
|---|---|
MessageDigest |
getMessageDigest() 返回与此流关联的消息摘要。 |
void |
on(boolean on) 打开或关闭摘要功能。 |
int |
read() 读取一个字节,并更新消息摘要(如果摘要功能打开)。 |
int |
read(byte[] b, int off, int len) 读入一个字节数组,并更新消息摘要(如果摘要功能打开)。 |
void |
setMessageDigest(MessageDigest digest) 将指定的消息摘要与此流关联。 |
String |
toString() 打印此摘要输入流及其关联的消息摘要对象的字符串表示形式。 |
继承方法(Inherited methods) |
|
|---|---|
java.io.FilterInputStream
|
|
java.io.InputStream
|
|
java.lang.Object
|
|
java.io.Closeable
|
|
java.lang.AutoCloseable
|
|
DigestInputStream (InputStream stream, MessageDigest digest)
使用指定的输入流和消息摘要创建摘要输入流。
| 参数(Parameters) | |
|---|---|
stream |
InputStream: the input stream. |
digest |
MessageDigest: the message digest to associate with this stream. |
MessageDigest getMessageDigest ()
返回与此流关联的消息摘要。
| 返回(Returns) | |
|---|---|
MessageDigest |
the message digest associated with this stream. |
void on (boolean on)
打开或关闭摘要功能。 默认打开。 打开时,调用其中一个read方法会导致消息摘要更新。 但是当它关闭时,消息摘要不会更新。
| 参数(Parameters) | |
|---|---|
on |
boolean: true to turn the digest function on, false to turn it off. |
int read ()
读取一个字节,并更新消息摘要(如果摘要功能打开)。 也就是说,这个方法从输入流中读取一个字节,直到字节被实际读取。 如果摘要功能处于打开状态(请参阅on ),则此方法将在与此流关联的消息摘要上调用update ,并传递它读取的字节。
| 返回(Returns) | |
|---|---|
int |
the byte read. |
| 抛出异常(Throws) | |
|---|---|
IOException |
if an I/O error occurs. |
也可以看看:
int read (byte[] b,
int off,
int len)
读入一个字节数组,并更新消息摘要(如果摘要功能打开)。 也就是说,这种方法从输入流读取多达len个字节到数组b ,从偏移量off开始。 这种方法阻塞直到数据被实际读取。 如果摘要功能开启(请参阅on ),则此方法将在与此流关联的消息摘要上调用update ,并将其传递给数据。
| 参数(Parameters) | |
|---|---|
b |
byte: the array into which the data is read. |
off |
int: the starting offset into b of where the data should be placed. |
len |
int: the maximum number of bytes to be read from the input stream into b, starting at offset off. |
| 返回(Returns) | |
|---|---|
int |
the actual number of bytes read. This is less than len if the end of the stream is reached prior to reading len bytes. -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. |
也可以看看:
void setMessageDigest (MessageDigest digest)
将指定的消息摘要与此流关联。
| 参数(Parameters) | |
|---|---|
digest |
MessageDigest: the message digest to be associated with this stream. |
也可以看看:
String toString ()
打印此摘要输入流及其关联的消息摘要对象的字符串表示形式。
| 返回(Returns) | |
|---|---|
String |
a string representation of the object. |