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:
java.io.Closeable,java.lang.AutoCloseable,java.nio.channels.Channel,java.nio.channels.ReadableByteChannel
public class PositionedCryptoInputStream extends CtrCryptoInputStream
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 Modifier and Type Class Description private static classPositionedCryptoInputStream.CipherState
-
Field Summary
Fields Modifier and Type Field Description private java.util.Queue<java.nio.ByteBuffer>byteBufferPoolDirectBuffer poolprivate java.util.Queue<PositionedCryptoInputStream.CipherState>cipherStatePoolCryptoCipher poolprivate java.util.Propertiespropertiesproperties for constructing a CryptoCipher-
Fields inherited from class org.apache.commons.crypto.stream.CryptoInputStream
cipher, EOS, inBuffer, input, key, outBuffer, STREAM_BUFFER_SIZE_KEY
-
-
Constructor Summary
Constructors Modifier Constructor Description PositionedCryptoInputStream(java.util.Properties properties, Input in, byte[] key, byte[] iv, long streamOffset)Constructs aPositionedCryptoInputStream.protectedPositionedCryptoInputStream(java.util.Properties properties, Input input, CryptoCipher cipher, int bufferSize, byte[] key, byte[] iv, long streamOffset)Constructs aPositionedCryptoInputStream.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidcleanByteBufferPool()Cleans direct buffer poolprivate voidcleanCipherStatePool()Cleans direct buffer poolvoidclose()Overrides theCryptoInputStream.close().protected voiddecrypt(long position, byte[] buffer, int offset, int length)Decrypts length bytes in buffer starting at offset.private voiddecrypt(PositionedCryptoInputStream.CipherState state, java.nio.ByteBuffer inByteBuffer, java.nio.ByteBuffer outByteBuffer, byte padding)Does the decryption using inBuffer as input and outBuffer as output.private voiddecryptBuffer(PositionedCryptoInputStream.CipherState state, java.nio.ByteBuffer inByteBuffer, java.nio.ByteBuffer outByteBuffer)Does the decryption using inBuffer as input and outBuffer as output.private java.nio.ByteBuffergetBuffer()Gets direct buffer from pool.private PositionedCryptoInputStream.CipherStategetCipherState()Gets CryptoCipher from pool.private bytepostDecryption(PositionedCryptoInputStream.CipherState state, java.nio.ByteBuffer inByteBuffer, long position, byte[] iv)This method is executed immediately after decryption.intread(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.voidreadFully(long position, byte[] buffer)Reads the specified number of bytes from a given position within a stream.voidreadFully(long position, byte[] buffer, int offset, int length)Reads the specified number of bytes from a given position within a stream.private voidresetCipher(PositionedCryptoInputStream.CipherState state, long position, byte[] iv)Calculates the counter and iv, reset the cipher.private voidreturnToPool(java.nio.ByteBuffer buf)Returns direct buffer to pool.private voidreturnToPool(PositionedCryptoInputStream.CipherState state)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
-
-
-
-
Field Detail
-
byteBufferPool
private final java.util.Queue<java.nio.ByteBuffer> byteBufferPool
DirectBuffer pool
-
cipherStatePool
private final java.util.Queue<PositionedCryptoInputStream.CipherState> cipherStatePool
CryptoCipher pool
-
properties
private final java.util.Properties properties
properties for constructing a CryptoCipher
-
-
Constructor Detail
-
PositionedCryptoInputStream
public PositionedCryptoInputStream(java.util.Properties properties, Input in, byte[] key, byte[] iv, long streamOffset) throws java.io.IOExceptionConstructs aPositionedCryptoInputStream.- Parameters:
properties- ThePropertiesclass 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:
java.io.IOException- if an I/O error occurs.
-
PositionedCryptoInputStream
protected PositionedCryptoInputStream(java.util.Properties properties, Input input, CryptoCipher cipher, int bufferSize, byte[] key, byte[] iv, long streamOffset) throws java.io.IOExceptionConstructs 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:
java.io.IOException- if an I/O error occurs.
-
-
Method Detail
-
cleanByteBufferPool
private void cleanByteBufferPool()
Cleans direct buffer pool
-
cleanCipherStatePool
private void cleanCipherStatePool()
Cleans direct buffer pool
-
close
public void close() throws java.io.IOExceptionOverrides theCryptoInputStream.close(). Closes this input stream and releases any system resources associated with the stream.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.nio.channels.Channel- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein classCryptoInputStream- Throws:
java.io.IOException- if an I/O error occurs.
-
decrypt
private void decrypt(PositionedCryptoInputStream.CipherState state, java.nio.ByteBuffer inByteBuffer, java.nio.ByteBuffer outByteBuffer, byte padding) throws java.io.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:
java.io.IOException- if an I/O error occurs.
-
decrypt
protected void decrypt(long position, byte[] buffer, int offset, int length) throws java.io.IOExceptionDecrypts length bytes in buffer starting at offset. Output is also put into buffer starting at offset. It is thread-safe.- Parameters:
buffer- the buffer into which the data is read.offset- the start offset in the data.position- the offset from the start of the stream.length- the maximum number of bytes to read.- Throws:
java.io.IOException- if an I/O error occurs.
-
decryptBuffer
private void decryptBuffer(PositionedCryptoInputStream.CipherState state, java.nio.ByteBuffer inByteBuffer, java.nio.ByteBuffer outByteBuffer) throws java.io.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:
java.io.IOException- if an I/O error occurs.
-
getBuffer
private java.nio.ByteBuffer getBuffer()
Gets direct buffer from pool. Caller MUST also callreturnToPool(ByteBuffer).- Returns:
- the buffer.
- See Also:
returnToPool(ByteBuffer)
-
getCipherState
private PositionedCryptoInputStream.CipherState getCipherState() throws java.io.IOException
Gets CryptoCipher from pool. Caller MUST also callreturnToPool(CipherState).- Returns:
- the CipherState instance.
- Throws:
java.io.IOException- if an I/O error occurs.
-
postDecryption
private byte postDecryption(PositionedCryptoInputStream.CipherState state, java.nio.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
public int read(long position, byte[] buffer, int offset, int length) throws java.io.IOExceptionReads 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:
buffer- the buffer into which the data is read.length- the maximum number of bytes to read.offset- the start offset in the data.position- the offset from the start of the stream.- Returns:
- int the total number of decrypted data bytes read into the buffer.
- Throws:
java.io.IOException- if an I/O error occurs.
-
readFully
public void readFully(long position, byte[] buffer) throws java.io.IOExceptionReads 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:
java.io.IOException- if an I/O error occurs.
-
readFully
public void readFully(long position, byte[] buffer, int offset, int length) throws java.io.IOExceptionReads 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:
buffer- the buffer into which the data is read.length- the maximum number of bytes to read.offset- the start offset in the data.position- the offset from the start of the stream.- Throws:
java.io.IOException- if an I/O error occurs.
-
resetCipher
private void resetCipher(PositionedCryptoInputStream.CipherState state, long position, byte[] iv)
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
private void returnToPool(java.nio.ByteBuffer buf)
Returns direct buffer to pool.- Parameters:
buf- the buffer.
-
returnToPool
private void returnToPool(PositionedCryptoInputStream.CipherState state)
Returns CryptoCipher to pool.- Parameters:
state- the CipherState instance.
-
-