Package org.apache.commons.crypto.stream
Class PositionedCryptoInputStream
java.lang.Object
java.io.InputStream
org.apache.commons.crypto.stream.CryptoInputStream
org.apache.commons.crypto.stream.CtrCryptoInputStream
org.apache.commons.crypto.stream.PositionedCryptoInputStream
- All Implemented Interfaces:
Closeable
,AutoCloseable
,Channel
,ReadableByteChannel
PositionedCryptoInputStream provides the capability to decrypt the stream
starting at random position as well as provides the foundation for positioned
read for decrypting. This needs a stream cipher mode such as AES CTR mode.
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Queue
<ByteBuffer> DirectBuffer poolprivate final Queue
<PositionedCryptoInputStream.CipherState> CryptoCipher poolprivate final Properties
properties for constructing a CryptoCipherFields inherited from class org.apache.commons.crypto.stream.CryptoInputStream
cipher, EOS, inBuffer, input, key, outBuffer, STREAM_BUFFER_SIZE_KEY
-
Constructor Summary
ConstructorsModifierConstructorDescriptionPositionedCryptoInputStream
(Properties properties, Input in, byte[] key, byte[] iv, long streamOffset) Constructs aPositionedCryptoInputStream
.protected
PositionedCryptoInputStream
(Properties properties, Input input, CryptoCipher cipher, int bufferSize, byte[] key, byte[] iv, long streamOffset) Constructs aPositionedCryptoInputStream
. -
Method Summary
Modifier and TypeMethodDescriptionprivate void
Cleans direct buffer poolprivate void
Cleans direct buffer poolvoid
close()
Overrides theCryptoInputStream.close()
.protected void
decrypt
(long position, byte[] buffer, int offset, int length) Decrypts length bytes in buffer starting at offset.private void
decrypt
(PositionedCryptoInputStream.CipherState state, ByteBuffer inByteBuffer, ByteBuffer outByteBuffer, byte padding) Does the decryption using inBuffer as input and outBuffer as output.private void
decryptBuffer
(PositionedCryptoInputStream.CipherState state, ByteBuffer inByteBuffer, ByteBuffer outByteBuffer) Does the decryption using inBuffer as input and outBuffer as output.private ByteBuffer
Gets direct buffer from pool.Gets CryptoCipher from pool.private byte
postDecryption
(PositionedCryptoInputStream.CipherState state, ByteBuffer inByteBuffer, long position, byte[] iv) This method is executed immediately after decryption.int
read
(long position, byte[] buffer, int offset, int length) Reads up to the specified number of bytes from a given position within a stream and return the number of bytes read.void
readFully
(long position, byte[] buffer) Reads the specified number of bytes from a given position within a stream.void
readFully
(long position, byte[] buffer, int offset, int length) Reads the specified number of bytes from a given position within a stream.private void
resetCipher
(PositionedCryptoInputStream.CipherState state, long position, byte[] iv) Calculates the counter and iv, reset the cipher.private void
returnToPool
(ByteBuffer buf) Returns direct buffer to pool.private void
Returns CryptoCipher to pool.Methods inherited from class org.apache.commons.crypto.stream.CtrCryptoInputStream
calculateIV, decrypt, decrypt, decryptBuffer, decryptInPlace, decryptMore, getCounter, getInitIV, getPadding, getStreamOffset, getStreamPosition, initCipher, postDecryption, read, resetCipher, resetStreamOffset, seek, setStreamOffset, skip
Methods inherited from class org.apache.commons.crypto.stream.CryptoInputStream
available, checkBufferSize, checkStream, checkStreamCipher, decryptFinal, freeBuffers, freeDirectBuffer, getBufferSize, getBufferSize, getCipher, getInput, getKey, getParams, isOpen, markSupported, read, read
Methods inherited from class java.io.InputStream
mark, read, reset
-
Field Details
-
byteBufferPool
DirectBuffer pool -
cipherStatePool
CryptoCipher pool -
properties
properties for constructing a CryptoCipher
-
-
Constructor Details
-
PositionedCryptoInputStream
public PositionedCryptoInputStream(Properties properties, Input in, byte[] key, byte[] iv, long streamOffset) throws IOException Constructs aPositionedCryptoInputStream
.- Parameters:
properties
- TheProperties
class represents a set of properties.in
- the input data.key
- crypto key for the cipher.iv
- Initialization vector for the cipher.streamOffset
- the start offset in the data.- Throws:
IOException
- if an I/O error occurs.
-
PositionedCryptoInputStream
protected PositionedCryptoInputStream(Properties properties, Input input, CryptoCipher cipher, int bufferSize, byte[] key, byte[] iv, long streamOffset) throws IOException Constructs aPositionedCryptoInputStream
.- Parameters:
properties
- the properties of streaminput
- the input data.cipher
- the CryptoCipher instance.bufferSize
- the bufferSize.key
- crypto key for the cipher.iv
- Initialization vector for the cipher.streamOffset
- the start offset in the data.- Throws:
IOException
- if an I/O error occurs.
-
-
Method Details
-
cleanByteBufferPool
private void cleanByteBufferPool()Cleans direct buffer pool -
cleanCipherStatePool
private void cleanCipherStatePool()Cleans direct buffer pool -
close
Overrides theCryptoInputStream.close()
. Closes this input stream and releases any system resources associated with the stream.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceChannel
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classCryptoInputStream
- Throws:
IOException
- if an I/O error occurs.
-
decrypt
private void decrypt(PositionedCryptoInputStream.CipherState state, ByteBuffer inByteBuffer, ByteBuffer outByteBuffer, byte padding) throws IOException 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().- Parameters:
state
- the CipherState instance.inByteBuffer
- the input buffer.outByteBuffer
- the output buffer.padding
- the padding.- Throws:
IOException
- if an I/O error occurs.
-
decrypt
Decrypts length bytes in buffer starting at offset. Output is also put into buffer starting at offset. It is thread-safe.- Parameters:
position
- the offset from the start of the stream.buffer
- the buffer into which the data is read.offset
- the start offset in the data.length
- the maximum number of bytes to read.- Throws:
IOException
- if an I/O error occurs.
-
decryptBuffer
private void decryptBuffer(PositionedCryptoInputStream.CipherState state, ByteBuffer inByteBuffer, ByteBuffer outByteBuffer) throws IOException Does the decryption using inBuffer as input and outBuffer as output.- Parameters:
state
- the CipherState instance.inByteBuffer
- the input buffer.outByteBuffer
- the output buffer.- Throws:
IOException
- if an I/O error occurs.
-
getBuffer
Gets direct buffer from pool. Caller MUST also callreturnToPool(ByteBuffer)
.- Returns:
- the buffer.
- See Also:
-
getCipherState
Gets CryptoCipher from pool. Caller MUST also callreturnToPool(CipherState)
.- Returns:
- the CipherState instance.
- Throws:
IOException
- if an I/O error occurs.
-
postDecryption
private byte postDecryption(PositionedCryptoInputStream.CipherState state, ByteBuffer inByteBuffer, long position, byte[] iv) This method is executed immediately after decryption. Check whether cipher should be updated and recalculate padding if needed.- Parameters:
state
- the CipherState instance.inByteBuffer
- the input buffer.position
- the offset from the start of the stream.iv
- the iv.- Returns:
- the padding.
-
read
Reads up to the specified number of bytes from a given position within a stream and return the number of bytes read. This does not change the current offset of the stream, and is thread-safe.- Parameters:
position
- the offset from the start of the stream.buffer
- the buffer into which the data is read.offset
- the start offset in the data.length
- the maximum number of bytes to read.- Returns:
- int the total number of decrypted data bytes read into the buffer.
- Throws:
IOException
- if an I/O error occurs.
-
readFully
Reads the specified number of bytes from a given position within a stream. This does not change the current offset of the stream and is thread-safe.- Parameters:
position
- the offset from the start of the stream.buffer
- the buffer into which the data is read.- Throws:
IOException
- if an I/O error occurs.
-
readFully
Reads the specified number of bytes from a given position within a stream. This does not change the current offset of the stream and is thread-safe.- Parameters:
position
- the offset from the start of the stream.buffer
- the buffer into which the data is read.offset
- the start offset in the data.length
- the maximum number of bytes to read.- Throws:
IOException
- if an I/O error occurs.
-
resetCipher
Calculates the counter and iv, reset the cipher.- Parameters:
state
- the CipherState instance.position
- the offset from the start of the stream.iv
- the iv.
-
returnToPool
Returns direct buffer to pool.- Parameters:
buf
- the buffer.
-
returnToPool
Returns CryptoCipher to pool.- Parameters:
state
- the CipherState instance.
-