Class PositionedCryptoInputStream

All Implemented Interfaces:
Closeable, AutoCloseable, Channel, 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.
  • Field Details

  • Constructor Details

    • PositionedCryptoInputStream

      public PositionedCryptoInputStream(Properties properties, Input in, byte[] key, byte[] iv, long streamOffset) throws IOException
      Parameters:
      properties - The Properties 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
      Parameters:
      properties - the properties of stream
      input - 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

      public void close() throws IOException
      Overrides the CryptoInputStream.close(). Closes this input stream and releases any system resources associated with the stream.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Channel
      Specified by:
      close in interface Closeable
      Overrides:
      close in class CryptoInputStream
      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

      protected void decrypt(long position, byte[] buffer, int offset, int length) throws IOException
      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

      private ByteBuffer getBuffer()
      Gets direct buffer from pool. Caller MUST also call returnToPool(ByteBuffer).
      Returns:
      the buffer.
      See Also:
    • getCipherState

      private PositionedCryptoInputStream.CipherState getCipherState() throws IOException
      Gets CryptoCipher from pool. Caller MUST also call returnToPool(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

      public int read(long position, byte[] buffer, int offset, int length) throws 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:
      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

      public void readFully(long position, byte[] buffer) throws 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:
      IOException - if an I/O error occurs.
    • readFully

      public void readFully(long position, byte[] buffer, int offset, int length) throws 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.
      offset - the start offset in the data.
      length - the maximum number of bytes to read.
      Throws:
      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(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.