Top Description Fields Constructors Methods
org.bouncycastle.crypto.io

public Class CipherInputStream

extends FilterInputStream
Class Inheritance
Imports
java.io.FilterInputStream, .IOException, .InputStream, org.bouncycastle.crypto.BufferedBlockCipher, .InvalidCipherTextException, .SkippingCipher, .StreamCipher, org.bouncycastle.crypto.modes.AEADBlockCipher, org.bouncycastle.util.Arrays

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

Modifier and TypeField and Description
private AEADBlockCipher
private byte[]
private BufferedBlockCipher
private int
private boolean
private byte[]
private static final int
private byte[]
private int
private long
private int
private SkippingCipher
private StreamCipher
Inherited from java.io.FilterInputStream:
in

Constructor Summary

AccessConstructor and Description
public
CipherInputStream(InputStream is, BufferedBlockCipher cipher)

Constructs a CipherInputStream from an InputStream and a BufferedBlockCipher.

public
CipherInputStream(InputStream is, StreamCipher cipher)

Constructs a CipherInputStream from an InputStream and a StreamCipher.

public
CipherInputStream(InputStream is, AEADBlockCipher cipher)

Constructs a CipherInputStream from an InputStream and an AEADBlockCipher.

public
CipherInputStream(InputStream is, BufferedBlockCipher cipher, int bufSize)

Constructs a CipherInputStream from an InputStream, a BufferedBlockCipher, and a specified internal buffer size.

public
CipherInputStream(InputStream is, StreamCipher cipher, int bufSize)

Constructs a CipherInputStream from an InputStream, a StreamCipher, and a specified internal buffer size.

public
CipherInputStream(InputStream is, AEADBlockCipher cipher, int bufSize)

Constructs a CipherInputStream from an InputStream, an AEADBlockCipher, and a specified internal buffer size.

Method Summary

Modifier and TypeMethod and Description
public int
available()

Overrides java.io.FilterInputStream.available.

Returns 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.
public void
close()

Overrides java.io.FilterInputStream.close.

Implements java.io.Closeable.close.

Closes the underlying input stream and finalises the processing of the data by the cipher.
private void
ensureCapacity(int
the size of the pending update.
updateSize
,
boolean
true iff this the cipher is to be finalised.
finalOutput
)

Ensure the cipher text buffer has space sufficient to accept an upcoming output.

private void
public void
mark(int
the maximum read ahead required before a reset() may be called.
readlimit
)

Overrides java.io.FilterInputStream.mark.

Mark the current position.
public boolean

Returns:

true if mark(readlimit) supported, false otherwise.
markSupported
()

Overrides java.io.FilterInputStream.markSupported.

Return true if mark(readlimit) is supported.
private int

Returns:

-1 to indicate end of stream, or the number of bytes (> 0) available.
nextChunk
()

Read data from underlying stream and process with cipher until end of stream or some data is available after cipher processing.

public int
read()

Overrides java.io.FilterInputStream.read.

Reads data from the underlying stream and processes it with the cipher until the cipher outputs data, and returns the next available byte.
public int

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.
read
(byte[]
the buffer into which the data is read.
b
)

Overrides java.io.FilterInputStream.read.

Reads data from the underlying stream and processes it with the cipher until the cipher outputs data, and then returns up to b.length bytes in the provided array.
public int

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.
read
(byte[]
the buffer into which the data is read.
b
,
int
the start offset in the destination array b
off
,
int
the maximum number of bytes read.
len
)

Overrides java.io.FilterInputStream.read.

Reads data from the underlying stream and processes it with the cipher until the cipher outputs data, and then returns up to len bytes in the provided array.
public void
reset()

Overrides java.io.FilterInputStream.reset.

Reset to the last marked position, if supported.
public long
skip(long
the number of bytes to be skipped.
n
)

Overrides java.io.FilterInputStream.skip.

Skips over and discards n bytes of data from the input stream.

Field Detail

aeadBlockCipherback to summary
private AEADBlockCipher aeadBlockCipher
bufback to summary
private byte[] buf
bufferedBlockCipherback to summary
private BufferedBlockCipher bufferedBlockCipher
bufOffback to summary
private int bufOff
finalizedback to summary
private boolean finalized
inBufback to summary
private byte[] inBuf
INPUT_BUF_SIZEback to summary
private static final int INPUT_BUF_SIZE
markBufback to summary
private byte[] markBuf
markBufOffback to summary
private int markBufOff
markPositionback to summary
private long markPosition
maxBufback to summary
private int maxBuf
skippingCipherback to summary
private SkippingCipher skippingCipher
streamCipherback to summary
private StreamCipher streamCipher

Constructor Detail

CipherInputStreamback to summary
public CipherInputStream(InputStream is, BufferedBlockCipher cipher)

Constructs a CipherInputStream from an InputStream and a BufferedBlockCipher.

CipherInputStreamback to summary
public CipherInputStream(InputStream is, StreamCipher cipher)

Constructs a CipherInputStream from an InputStream and a StreamCipher.

CipherInputStreamback to summary
public CipherInputStream(InputStream is, AEADBlockCipher cipher)

Constructs a CipherInputStream from an InputStream and an AEADBlockCipher.

CipherInputStreamback to summary
public CipherInputStream(InputStream is, BufferedBlockCipher cipher, int bufSize)

Constructs a CipherInputStream from an InputStream, a BufferedBlockCipher, and a specified internal buffer size.

CipherInputStreamback to summary
public CipherInputStream(InputStream is, StreamCipher cipher, int bufSize)

Constructs a CipherInputStream from an InputStream, a StreamCipher, and a specified internal buffer size.

CipherInputStreamback to summary
public CipherInputStream(InputStream is, AEADBlockCipher cipher, int bufSize)

Constructs a CipherInputStream from an InputStream, an AEADBlockCipher, and a specified internal buffer size.

Method Detail

availableback to summary
public int available() throws IOException

Overrides java.io.FilterInputStream.available.

Doc from java.io.FilterInputStream.available.

Returns 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. The next caller might be the same thread or another thread. A single read or skip of this many bytes will not block, but may read or skip fewer bytes.

Returns:int

an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking.

Exceptions
IOException:
if an I/O error occurs.
closeback to summary
public void close() throws IOException

Overrides java.io.FilterInputStream.close.

Implements java.io.Closeable.close.

Closes the underlying input stream and finalises the processing of the data by the cipher.

Exceptions
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).
ensureCapacityback to summary
private void ensureCapacity(int updateSize, boolean finalOutput)

Ensure the cipher text buffer has space sufficient to accept an upcoming output.

Parameters
updateSize:int

the size of the pending update.

finalOutput:boolean

true iff this the cipher is to be finalised.

finaliseCipherback to summary
private void finaliseCipher() throws IOException
markback to summary
public void mark(int readlimit)

Overrides java.io.FilterInputStream.mark.

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).

Parameters
readlimit:int

the maximum read ahead required before a reset() may be called.

markSupportedback to summary
public boolean markSupported()

Overrides java.io.FilterInputStream.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,

Returns:boolean

true if mark(readlimit) supported, false otherwise.

nextChunkback to summary
private int nextChunk() throws IOException

Read data from underlying stream and process with cipher until end of stream or some data is available after cipher processing.

Returns:int

-1 to indicate end of stream, or the number of bytes (> 0) available.

readback to summary
public int read() throws IOException

Overrides java.io.FilterInputStream.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.

Returns:int

Doc from java.io.FilterInputStream.read.

the next byte of data, or -1 if the end of the stream is reached.

Exceptions
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).
readback to summary
public int read(byte[] b) throws IOException

Overrides java.io.FilterInputStream.read.

Reads data from the underlying stream and processes it with the cipher until the cipher outputs data, and then returns up to b.length bytes in the provided array.

If the underlying stream is exhausted by this call, the cipher will be finalised.

Parameters
b:byte[]

the buffer into which the data is read.

Returns:int

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.

Exceptions
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).
readback to summary
public int read(byte[] b, int off, int len) throws IOException

Overrides java.io.FilterInputStream.read.

Reads data from the underlying stream and processes it with the cipher until the cipher outputs data, and then returns up to len bytes in the provided array.

If the underlying stream is exhausted by this call, the cipher will be finalised.

Parameters
b:byte[]

the buffer into which the data is read.

off:int

the start offset in the destination array b

len:int

the maximum number of bytes read.

Returns:int

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.

Exceptions
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).
resetback to summary
public void reset() throws IOException

Overrides java.io.FilterInputStream.reset.

Reset to the last marked position, if supported.

Exceptions
IOException:
if marking not supported by the cipher used, or the underlying stream.
skipback to summary
public long skip(long n) throws IOException

Overrides java.io.FilterInputStream.skip.

Doc from java.io.FilterInputStream.skip.

Skips over and discards n bytes of data from the input stream. The skip method may, for a variety of reasons, end up skipping over some smaller number of bytes, possibly 0. The actual number of bytes skipped is returned.

Parameters
n:long

the number of bytes to be skipped.

Returns:long

the actual number of bytes skipped.

Exceptions
IOException:
if in.skip(n) throws an IOException.