Package org.apache.commons.crypto.stream
Class CryptoInputStream
java.lang.Object
java.io.InputStream
org.apache.commons.crypto.stream.CryptoInputStream
- All Implemented Interfaces:
Closeable,AutoCloseable,Channel,ReadableByteChannel
- Direct Known Subclasses:
CtrCryptoInputStream
CryptoInputStream reads input data and decrypts data in stream manner. It
supports any mode of operations such as AES CBC/CTR/GCM mode in concept.It is
not thread-safe.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final intThe buffer size.(package private) final CryptoCipherThe CryptoCipher instance.private booleanFlag to mark whether the input stream is closed.static final intThe index value when the end of the stream has been reached-1.private booleanFlag to mark whether do final of the cipher to end the decrypting stream.(package private) ByteBufferInput data buffer.(package private) InputThe input data.(package private) final KeyCrypto key for the cipher.private static final intprivate final byte[](package private) ByteBufferThe decrypted data buffer.private final AlgorithmParameterSpecthe algorithm parametersprivate static final intThe default value of the buffer size for stream.static final StringThe configuration key of the buffer size for stream. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedCryptoInputStream(InputStream inputStream, CryptoCipher cipher, int bufferSize, Key key, AlgorithmParameterSpec params) Constructs aCryptoInputStream.CryptoInputStream(String transformation, Properties properties, InputStream inputStream, Key key, AlgorithmParameterSpec params) Constructs aCryptoInputStream.CryptoInputStream(String transformation, Properties properties, ReadableByteChannel channel, Key key, AlgorithmParameterSpec params) Constructs aCryptoInputStream.protectedCryptoInputStream(ReadableByteChannel channel, CryptoCipher cipher, int bufferSize, Key key, AlgorithmParameterSpec params) Constructs aCryptoInputStream.protectedCryptoInputStream(Input input, CryptoCipher cipher, int bufferSize, Key key, AlgorithmParameterSpec params) Constructs aCryptoInputStream. -
Method Summary
Modifier and TypeMethodDescriptionintOverrides theInputStream.available().(package private) static intcheckBufferSize(CryptoCipher cipher, int bufferSize) Checks and floors buffer size.protected voidChecks whether the stream is closed.(package private) static voidcheckStreamCipher(CryptoCipher cipher) Checks whether the cipher is supported streaming.voidclose()Overrides theInputStream.close().protected voiddecrypt()Does the decryption using inBuffer as input and outBuffer as output.protected voidDoes final of the cipher to end the decrypting stream.protected intDecrypts more data by reading the under layer stream.protected voidForcibly free the direct buffers.(package private) static voidfreeDirectBuffer(ByteBuffer buffer) Forcibly free the direct buffer.protected intGets the buffer size.(package private) static intgetBufferSize(Properties props) Reads crypto buffer size.protected CryptoCipherGets the internal CryptoCipher.protected InputgetInput()Gets the input.protected KeygetKey()Gets the key.protected AlgorithmParameterSpecGets the specification of cryptographic parameters.protected voidInitializes the cipher.booleanisOpen()Overrides theChannel.isOpen().booleanOverrides theInputStream.markSupported().intread()Overrides theInputStream.read().intread(byte[] array, int off, int len) Overrides theInputStream.read(byte[], int, int).intread(ByteBuffer dst) Overrides theReadableByteChannel.read(ByteBuffer).longskip(long n) Overrides theInputStream.skip(long).Methods inherited from class java.io.InputStream
mark, read, reset
-
Field Details
-
STREAM_BUFFER_SIZE_KEY
The configuration key of the buffer size for stream.- See Also:
-
STREAM_BUFFER_SIZE_DEFAULT
private static final int STREAM_BUFFER_SIZE_DEFAULTThe default value of the buffer size for stream.- See Also:
-
MIN_BUFFER_SIZE
private static final int MIN_BUFFER_SIZE- See Also:
-
EOS
public static final int EOSThe index value when the end of the stream has been reached-1.- Since:
- 1.1
- See Also:
-
oneByteBuf
private final byte[] oneByteBuf -
cipher
The CryptoCipher instance. -
bufferSize
private final int bufferSizeThe buffer size. -
key
Crypto key for the cipher. -
params
the algorithm parameters -
closed
private boolean closedFlag to mark whether the input stream is closed. -
finalDone
private boolean finalDoneFlag to mark whether do final of the cipher to end the decrypting stream. -
input
Input inputThe input data. -
inBuffer
ByteBuffer inBufferInput data buffer. The data starts at inBuffer.position() and ends at to inBuffer.limit(). -
outBuffer
ByteBuffer outBufferThe decrypted data buffer. The data starts at outBuffer.position() and ends at outBuffer.limit().
-
-
Constructor Details
-
CryptoInputStream
protected CryptoInputStream(Input input, CryptoCipher cipher, int bufferSize, Key key, AlgorithmParameterSpec params) throws IOException Constructs aCryptoInputStream.- Parameters:
input- the input data.cipher- the cipher instance.bufferSize- the bufferSize.key- crypto key for the cipher.params- the algorithm parameters.- Throws:
IOException- if an I/O error occurs.
-
CryptoInputStream
protected CryptoInputStream(InputStream inputStream, CryptoCipher cipher, int bufferSize, Key key, AlgorithmParameterSpec params) throws IOException Constructs aCryptoInputStream.- Parameters:
inputStream- the input stream.cipher- the cipher instance.bufferSize- the bufferSize.key- crypto key for the cipher.params- the algorithm parameters.- Throws:
IOException- if an I/O error occurs.
-
CryptoInputStream
protected CryptoInputStream(ReadableByteChannel channel, CryptoCipher cipher, int bufferSize, Key key, AlgorithmParameterSpec params) throws IOException Constructs aCryptoInputStream.- Parameters:
channel- the ReadableByteChannel instance.cipher- the cipher instance.bufferSize- the bufferSize.key- crypto key for the cipher.params- the algorithm parameters.- Throws:
IOException- if an I/O error occurs.
-
CryptoInputStream
public CryptoInputStream(String transformation, Properties properties, InputStream inputStream, Key key, AlgorithmParameterSpec params) throws IOException Constructs aCryptoInputStream.- Parameters:
transformation- the name of the transformation, e.g., AES/CBC/PKCS5Padding. See the Java Cryptography Architecture Standard Algorithm Name Documentation for information about standard transformation names.properties- ThePropertiesclass represents a set of properties.inputStream- the input stream.key- crypto key for the cipher.params- the algorithm parameters.- Throws:
IOException- if an I/O error occurs.
-
CryptoInputStream
public CryptoInputStream(String transformation, Properties properties, ReadableByteChannel channel, Key key, AlgorithmParameterSpec params) throws IOException Constructs aCryptoInputStream.- Parameters:
transformation- the name of the transformation, e.g., AES/CBC/PKCS5Padding. See the Java Cryptography Architecture Standard Algorithm Name Documentation for information about standard transformation names.properties- ThePropertiesclass represents a set of properties.channel- the ReadableByteChannel object.key- crypto key for the cipher.params- the algorithm parameters.- Throws:
IOException- if an I/O error occurs.
-
-
Method Details
-
checkBufferSize
Checks and floors buffer size.- Parameters:
cipher- theCryptoCipherinstance.bufferSize- the buffer size.- Returns:
- the remaining buffer size.
-
checkStreamCipher
Checks whether the cipher is supported streaming.- Parameters:
cipher- theCryptoCipherinstance.- Throws:
IOException- if an I/O error occurs.
-
freeDirectBuffer
Forcibly free the direct buffer.- Parameters:
buffer- the bytebuffer to be freed.
-
getBufferSize
Reads crypto buffer size.- Parameters:
props- ThePropertiesclass represents a set of properties.- Returns:
- the buffer size.
-
available
Overrides theInputStream.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 invocation of a method for this input stream.- Overrides:
availablein classInputStream- Returns:
- an estimate of the number of bytes that can be read (or skipped
over) from this input stream without blocking or
0when it reaches the end of the input stream. - Throws:
IOException- if an I/O error occurs.
-
checkStream
Checks whether the stream is closed.- Throws:
IOException- if an I/O error occurs.
-
close
Overrides theInputStream.close(). Closes this input stream and releases any system resources associated with the stream.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceChannel- Specified by:
closein interfaceCloseable- Overrides:
closein classInputStream- Throws:
IOException- if an I/O error occurs.
-
decrypt
Does the decryption using inBuffer as input and outBuffer as output. Upon return, inBuffer is cleared; the decrypted data starts at outBuffer.position() and ends at outBuffer.limit().- Throws:
IOException- if an I/O error occurs.
-
decryptFinal
Does final of the cipher to end the decrypting stream.- Throws:
IOException- if an I/O error occurs.
-
decryptMore
Decrypts more data by reading the under layer stream. The decrypted data will be put in the output buffer. If the end of the under stream reached, we will do final of the cipher to finish all the decrypting of data.- Returns:
- The number of decrypted data. return -1 (if end of the decrypted stream) return 0 (no data now, but could have more later)
- Throws:
IOException- if an I/O error occurs.
-
freeBuffers
protected void freeBuffers()Forcibly free the direct buffers. -
getBufferSize
protected int getBufferSize()Gets the buffer size.- Returns:
- the bufferSize.
-
getCipher
Gets the internal CryptoCipher.- Returns:
- the cipher instance.
-
getInput
Gets the input.- Returns:
- the input.
-
getKey
Gets the key.- Returns:
- the key.
-
getParams
Gets the specification of cryptographic parameters.- Returns:
- the params.
-
initCipher
Initializes the cipher.- Throws:
IOException- if an I/O error occurs.
-
isOpen
public boolean isOpen()Overrides theChannel.isOpen(). -
markSupported
public boolean markSupported()Overrides theInputStream.markSupported().- Overrides:
markSupportedin classInputStream- Returns:
- false,the
CtrCryptoInputStreamdon't support the mark method.
-
read
Overrides theInputStream.read(). Reads the next byte of data from the input stream.- Specified by:
readin classInputStream- Returns:
- the next byte of data, or
EOS (-1)if the end of the stream is reached. - Throws:
IOException- if an I/O error occurs.
-
read
Overrides theInputStream.read(byte[], int, int). Decryption is buffer based. If there is data inoutBuffer, then read it out of this buffer. If there is no data inoutBuffer, then read more from the underlying stream and do the decryption.- Overrides:
readin classInputStream- Parameters:
array- the buffer into which the decrypted data is read.off- the buffer offset.len- the maximum number of decrypted data bytes to read.- Returns:
- int the total number of decrypted data bytes read into the buffer.
- Throws:
IOException- if an I/O error occurs.
-
read
Overrides theReadableByteChannel.read(ByteBuffer). Reads a sequence of bytes from this channel into the given buffer.- Specified by:
readin interfaceReadableByteChannel- Parameters:
dst- The buffer into which bytes are to be transferred.- Returns:
- The number of bytes read, possibly zero, or
EOS (-1)if the channel has reached end-of-stream. - Throws:
IOException- if an I/O error occurs.
-
skip
Overrides theInputStream.skip(long). Skips over and discardsnbytes of data from this input stream.- Overrides:
skipin classInputStream- Parameters:
n- the number of bytes to be skipped.- Returns:
- the actual number of bytes skipped.
- Throws:
IOException- if an I/O error occurs.
-