- java.lang.Object
-
- java.io.InputStream
-
- java.io.FilterInputStream
-
- javax.crypto.CipherInputStream
-
- All Implemented Interfaces:
-
Closeable,AutoCloseable
public class CipherInputStream extends FilterInputStream
CipherInputStream由InputStream和Cipher组成,使得read()方法返回从底层InputStream读入但是由Cipher额外处理的数据。 密码必须在被CipherInputStream使用之前完全初始化。例如,如果将密码初始化为解密,则CipherInputStream将在返回解密的数据之前尝试读取数据并将其解密。
该类严格遵守其祖先类java.io.FilterInputStream和java.io.InputStream的语义,特别是失败语义。 这个类只有在其祖先类中指定的方法,并覆盖它们全部。 此外,此类捕获其祖先类不抛出的所有异常。 特别地,
skip方法跳过,而available方法仅计算已封装密码处理的数据。对于使用此类的程序员,不要使用在此类中未定义或覆盖的方法(例如后来添加到其中一个超类的新方法或构造函数),因为这些方法的设计和实现不太可能考虑到对CipherInputStream的安全性影响。
- 从以下版本开始:
- 1.4
- 另请参见:
-
InputStream,FilterInputStream,Cipher,CipherOutputStream
-
-
Field Summary
-
Fields inherited from class java.io.FilterInputStream
in
-
-
构造方法摘要
构造方法 Modifier Constructor 描述 protectedCipherInputStream(InputStream is)从InputStream构造CipherInputStream,而不指定密码。CipherInputStream(InputStream is, Cipher c)从InputStream和Cipher构造一个CipherInputStream。
-
方法摘要
所有方法 接口方法 具体的方法 Modifier and Type 方法 描述 intavailable()返回可以从该输入流读取而不阻塞的字节数。voidclose()关闭此输入流并释放与流相关联的任何系统资源。booleanmarkSupported()测试这个输入流是否支持mark和reset方法,而不是。intread()从该输入流读取下一个数据字节。intread(byte[] b)从该输入流读取最多b.length字节的数据到字节数组。intread(byte[] b, int off, int len)从该输入流读取最多len个字节的数据为字节数组。longskip(long n)跳过n字节的字节,可从该输入流中读取而不阻塞。-
Methods inherited from class java.io.FilterInputStream
mark, reset
-
Methods inherited from class java.io.InputStream
readAllBytes, readNBytes, transferTo
-
-
-
-
构造方法详细信息
-
CipherInputStream
public CipherInputStream(InputStream is, Cipher c)
从InputStream和Cipher构造一个CipherInputStream。
注意:如果指定的输入流或密码为空,则在使用NullPointerException时可能会稍后抛出。- 参数
-
is- 要处理的输入流 -
c- 一个初始化的Cipher对象
-
CipherInputStream
protected CipherInputStream(InputStream is)
从InputStream构造CipherInputStream,而不指定密码。 这具有使用NullCipher构造CipherInputStream的效果。
注意:如果指定的输入流为空,则在使用NullPointerException时可能会稍后抛出。- 参数
-
is- 要处理的输入流
-
-
方法详细信息
-
read
public int read() throws IOException从该输入流读取下一个数据字节。 值字节作为int返回,范围为0至255。 如果没有字节可用,因为流已经到达,则返回值-1。 该方法阻塞直到输入数据可用,检测到流的结尾,或抛出异常。- 重写:
-
read在FilterInputStream - 结果
-
数据的下一个字节,如果达到流的末尾,
-1。 - 异常
-
IOException- 如果发生I / O错误。 - 另请参见:
-
FilterInputStream.in
-
read
public int read(byte[] b) throws IOException从该输入流读取最多b.length个字节的数据为字节数组。该
read的方法InputStream调用read的三个参数方法与参数b,0,并b.length。- 重写:
-
read在FilterInputStream - 参数
-
b- 读取数据的缓冲区。 - 结果
-
读入缓冲区的总字节数,或
-1是没有更多的数据,因为已经到达流的结束。 - 异常
-
IOException- 如果发生I / O错误。 - 另请参见:
-
InputStream.read(byte[], int, int)
-
read
public int read(byte[] b, int off, int len) throws IOException从该输入流读取最多len个字节的数据为字节数组。 此方法将阻塞,直到某些输入可用。 如果第一个参数是null,最多len个字节被读取并丢弃。- 重写:
-
read在FilterInputStream - 参数
-
b- 读取数据的缓冲区。 -
off- 目标阵列中的起始偏移量buf -
len- 读取的最大字节数。 - 结果
-
读入缓冲区的总字节数,如果没有更多的数据,因为已经到达流的末尾,那么
-1。 - 异常
-
IOException- 如果发生I / O错误。 - 另请参见:
-
InputStream.read()
-
skip
public long skip(long n) throws IOException从可从该输入流读取的字节中跳过n字节的字节,无阻塞。可能会跳过比请求的字节少的字节。 跳过的实际字节数等于
n或调用available的结果,以较小者为准。 如果n小于0,则不会跳过任何字节。返回实际跳过的字节数。
- 重写:
-
skip在FilterInputStream - 参数
-
n- 要跳过的字节数。 - 结果
- 实际跳过的字节数。
- 异常
-
IOException- 如果发生I / O错误。
-
available
public int available() throws IOException返回可以从该输入流读取而不阻塞的字节数。 该available方法InputStream回报0。 这个方法应该被子类覆盖。- 重写:
-
available在FilterInputStream - 结果
- 可以从此输入流中读取而不阻塞的字节数。
- 异常
-
IOException- 如果发生I / O错误。
-
close
public void close() throws IOException关闭此输入流并释放与流相关联的任何系统资源。该
close的方法CipherInputStream调用close其底层输入流的方法。- Specified by:
-
close在接口AutoCloseable - Specified by:
-
close在接口Closeable - 重写:
-
close在FilterInputStream - 异常
-
IOException- 如果发生I / O错误。 - 另请参见:
-
FilterInputStream.in
-
markSupported
public boolean markSupported()
测试这个输入流是否支持mark和reset方法,而不是。- 重写:
-
markSupported在FilterInputStream - 结果
-
false,因为这个类不支持mark和reset方法。 - 另请参见:
-
InputStream.mark(int),InputStream.reset()
-
-