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

      • byteBufferPool

        private final java.util.Queue<java.nio.ByteBuffer> byteBufferPool
        DirectBuffer 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
        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:
        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
        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:
        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 the CryptoInputStream.close(). Closes this input stream and releases any system resources associated with the stream.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.nio.channels.Channel
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class CryptoInputStream
        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.
      • 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.