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 class
PositionedCryptoInputStream.CipherState
-
Field Summary
Fields Modifier and Type Field Description private java.util.Queue<java.nio.ByteBuffer>
byteBufferPool
DirectBuffer poolprivate java.util.Queue<PositionedCryptoInputStream.CipherState>
cipherStatePool
CryptoCipher poolprivate java.util.Properties
properties
properties 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
.protected
PositionedCryptoInputStream(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 void
cleanByteBufferPool()
Cleans direct buffer poolprivate void
cleanCipherStatePool()
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, java.nio.ByteBuffer inByteBuffer, java.nio.ByteBuffer outByteBuffer, byte padding)
Does the decryption using inBuffer as input and outBuffer as output.private void
decryptBuffer(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.ByteBuffer
getBuffer()
Gets direct buffer from pool.private PositionedCryptoInputStream.CipherState
getCipherState()
Gets CryptoCipher from pool.private byte
postDecryption(PositionedCryptoInputStream.CipherState state, java.nio.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(java.nio.ByteBuffer buf)
Returns direct buffer to pool.private void
returnToPool(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.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:
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.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:
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.IOException
Overrides theCryptoInputStream.close()
. Closes this input stream and releases any system resources associated with the stream.- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.nio.channels.Channel
- Specified by:
close
in interfacejava.io.Closeable
- Overrides:
close
in 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.IOException
Decrypts 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.IOException
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:
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.IOException
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:
java.io.IOException
- if an I/O error occurs.
-
readFully
public void readFully(long position, byte[] buffer, int offset, int length) throws java.io.IOException
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:
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.
-
-