Class 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.
  • 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

    • 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

      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.
    • 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.