Package org.bouncycastle.crypto.io
Class CipherInputStream
java.lang.Object
java.io.InputStream
java.io.FilterInputStream
org.bouncycastle.crypto.io.CipherInputStream
- All Implemented Interfaces:
Closeable
,AutoCloseable
A CipherInputStream is composed of an InputStream and a cipher so that read() methods return data
that are read in from the underlying InputStream but have been additionally processed by the
Cipher. The cipher must be fully initialized before being used by a CipherInputStream.
For example, if the Cipher is initialized for decryption, the CipherInputStream will attempt to read in data and decrypt them, before returning the decrypted data.
-
Field Summary
Fields inherited from class java.io.FilterInputStream
in
-
Constructor Summary
ConstructorDescriptionCipherInputStream
(InputStream is, BufferedBlockCipher cipher) Constructs a CipherInputStream from an InputStream and a BufferedBlockCipher.CipherInputStream
(InputStream is, BufferedBlockCipher cipher, int bufSize) Constructs a CipherInputStream from an InputStream, a BufferedBlockCipher, and a specified internal buffer size.CipherInputStream
(InputStream is, AEADBlockCipher cipher) Constructs a CipherInputStream from an InputStream and an AEADBlockCipher.CipherInputStream
(InputStream is, AEADBlockCipher cipher, int bufSize) Constructs a CipherInputStream from an InputStream, an AEADBlockCipher, and a specified internal buffer size.CipherInputStream
(InputStream is, StreamCipher cipher) Constructs a CipherInputStream from an InputStream and a StreamCipher.CipherInputStream
(InputStream is, StreamCipher cipher, int bufSize) Constructs a CipherInputStream from an InputStream, a StreamCipher, and a specified internal buffer size. -
Method Summary
Modifier and TypeMethodDescriptionint
void
close()
Closes the underlying input stream and finalises the processing of the data by the cipher.void
mark
(int readlimit) Mark the current position.boolean
Return true if mark(readlimit) is supported.int
read()
Reads data from the underlying stream and processes it with the cipher until the cipher outputs data, and returns the next available byte.int
read
(byte[] b) Reads data from the underlying stream and processes it with the cipher until the cipher outputs data, and then returns up tob.length
bytes in the provided array.int
read
(byte[] b, int off, int len) Reads data from the underlying stream and processes it with the cipher until the cipher outputs data, and then returns up tolen
bytes in the provided array.void
reset()
Reset to the last marked position, if supported.long
skip
(long n) Methods inherited from class java.io.InputStream
nullInputStream, readAllBytes, readNBytes, readNBytes, skipNBytes, transferTo
-
Constructor Details
-
CipherInputStream
Constructs a CipherInputStream from an InputStream and a BufferedBlockCipher. -
CipherInputStream
Constructs a CipherInputStream from an InputStream and a StreamCipher. -
CipherInputStream
Constructs a CipherInputStream from an InputStream and an AEADBlockCipher. -
CipherInputStream
Constructs a CipherInputStream from an InputStream, a BufferedBlockCipher, and a specified internal buffer size. -
CipherInputStream
Constructs a CipherInputStream from an InputStream, a StreamCipher, and a specified internal buffer size. -
CipherInputStream
Constructs a CipherInputStream from an InputStream, an AEADBlockCipher, and a specified internal buffer size.
-
-
Method Details
-
read
Reads data from the underlying stream and processes it with the cipher until the cipher outputs data, and returns the next available byte.If the underlying stream is exhausted by this call, the cipher will be finalised.
- Overrides:
read
in classFilterInputStream
- Throws:
IOException
- if there was an error closing the input stream.InvalidCipherTextIOException
- if the data read from the stream was invalid ciphertext (e.g. the cipher is an AEAD cipher and the ciphertext tag check fails).
-
read
Reads data from the underlying stream and processes it with the cipher until the cipher outputs data, and then returns up tob.length
bytes in the provided array.If the underlying stream is exhausted by this call, the cipher will be finalised.
- Overrides:
read
in classFilterInputStream
- Parameters:
b
- the buffer into which the data is read.- Returns:
- the total number of bytes read into the buffer, or
-1
if there is no more data because the end of the stream has been reached. - Throws:
IOException
- if there was an error closing the input stream.InvalidCipherTextIOException
- if the data read from the stream was invalid ciphertext (e.g. the cipher is an AEAD cipher and the ciphertext tag check fails).
-
read
Reads data from the underlying stream and processes it with the cipher until the cipher outputs data, and then returns up tolen
bytes in the provided array.If the underlying stream is exhausted by this call, the cipher will be finalised.
- Overrides:
read
in classFilterInputStream
- Parameters:
b
- the buffer into which the data is read.off
- the start offset in the destination arrayb
len
- the maximum number of bytes read.- Returns:
- the total number of bytes read into the buffer, or
-1
if there is no more data because the end of the stream has been reached. - Throws:
IOException
- if there was an error closing the input stream.InvalidCipherTextIOException
- if the data read from the stream was invalid ciphertext (e.g. the cipher is an AEAD cipher and the ciphertext tag check fails).
-
skip
- Overrides:
skip
in classFilterInputStream
- Throws:
IOException
-
available
- Overrides:
available
in classFilterInputStream
- Throws:
IOException
-
close
Closes the underlying input stream and finalises the processing of the data by the cipher.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classFilterInputStream
- Throws:
IOException
- if there was an error closing the input stream.InvalidCipherTextIOException
- if the data read from the stream was invalid ciphertext (e.g. the cipher is an AEAD cipher and the ciphertext tag check fails).
-
mark
public void mark(int readlimit) Mark the current position.This method only works if markSupported() returns true - which means the underlying stream supports marking, and the cipher passed in to this stream's constructor is a SkippingCipher (so capable of being reset to an arbitrary point easily).
- Overrides:
mark
in classFilterInputStream
- Parameters:
readlimit
- the maximum read ahead required before a reset() may be called.
-
reset
Reset to the last marked position, if supported.- Overrides:
reset
in classFilterInputStream
- Throws:
IOException
- if marking not supported by the cipher used, or the underlying stream.
-
markSupported
public boolean markSupported()Return true if mark(readlimit) is supported. This will be true if the underlying stream supports marking and the cipher used is a SkippingCipher,- Overrides:
markSupported
in classFilterInputStream
- Returns:
- true if mark(readlimit) supported, false otherwise.
-