Class CryptoOutputStream

  • All Implemented Interfaces:
    java.io.Closeable, java.io.Flushable, java.lang.AutoCloseable, java.nio.channels.Channel, java.nio.channels.WritableByteChannel
    Direct Known Subclasses:
    CtrCryptoOutputStream

    public class CryptoOutputStream
    extends java.io.OutputStream
    implements java.nio.channels.WritableByteChannel
    CryptoOutputStream encrypts data and writes to the under layer output. It supports any mode of operations such as AES CBC/CTR/GCM mode in concept. It is not thread-safe.

    This class should only be used with blocking sinks. Using this class to wrap a non-blocking sink may lead to high CPU usage.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private int bufferSize
      The buffer size.
      (package private) CryptoCipher cipher
      the CryptoCipher instance
      private boolean closed
      Flag to mark whether the output stream is closed.
      (package private) java.nio.ByteBuffer inBuffer
      Input data buffer.
      (package private) java.security.Key key
      Crypto key for the cipher.
      private byte[] oneByteBuf  
      (package private) java.nio.ByteBuffer outBuffer
      Encrypted data buffer.
      (package private) Output output
      The output.
      private java.security.spec.AlgorithmParameterSpec params
      the algorithm parameters
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected CryptoOutputStream​(java.io.OutputStream outputStream, CryptoCipher cipher, int bufferSize, java.security.Key key, java.security.spec.AlgorithmParameterSpec params)
      Constructs a CryptoOutputStream.
        CryptoOutputStream​(java.lang.String transformation, java.util.Properties properties, java.io.OutputStream outputStream, java.security.Key key, java.security.spec.AlgorithmParameterSpec params)
      Constructs a CryptoOutputStream.
        CryptoOutputStream​(java.lang.String transformation, java.util.Properties properties, java.nio.channels.WritableByteChannel out, java.security.Key key, java.security.spec.AlgorithmParameterSpec params)
      Constructs a CryptoOutputStream.
      protected CryptoOutputStream​(java.nio.channels.WritableByteChannel channel, CryptoCipher cipher, int bufferSize, java.security.Key key, java.security.spec.AlgorithmParameterSpec params)
      Constructs a CryptoOutputStream.
      protected CryptoOutputStream​(Output output, CryptoCipher cipher, int bufferSize, java.security.Key key, java.security.spec.AlgorithmParameterSpec params)
      Constructs a CryptoOutputStream.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected void checkStream()
      Checks whether the stream is closed.
      void close()
      Overrides the OutputStream.close().
      protected void encrypt()
      Does the encryption, input is inBuffer and output is outBuffer.
      protected void encryptFinal()
      Does final encryption of the last data.
      void flush()
      Overrides the OutputStream.flush().
      protected void freeBuffers()
      Forcibly free the direct buffers.
      protected int getBufferSize()
      Gets the buffer size.
      protected CryptoCipher getCipher()
      Gets the internal Cipher.
      protected java.nio.ByteBuffer getInBuffer()
      Gets the inBuffer.
      protected java.nio.ByteBuffer getOutBuffer()
      Gets the outBuffer.
      protected void initCipher()
      Initializes the cipher.
      boolean isOpen()
      Overrides the Channel.isOpen().
      void write​(byte[] array, int off, int len)
      Overrides the OutputStream.write(byte[], int, int).
      void write​(int b)
      Overrides the OutputStream.write(byte[]).
      int write​(java.nio.ByteBuffer src)
      Overrides the WritableByteChannel.write(ByteBuffer).
      • Methods inherited from class java.io.OutputStream

        write
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • oneByteBuf

        private final byte[] oneByteBuf
      • output

        final Output output
        The output.
      • cipher

        final CryptoCipher cipher
        the CryptoCipher instance
      • bufferSize

        private final int bufferSize
        The buffer size.
      • key

        final java.security.Key key
        Crypto key for the cipher.
      • params

        private final java.security.spec.AlgorithmParameterSpec params
        the algorithm parameters
      • closed

        private boolean closed
        Flag to mark whether the output stream is closed.
      • inBuffer

        java.nio.ByteBuffer inBuffer
        Input data buffer. The data starts at inBuffer.position() and ends at inBuffer.limit().
      • outBuffer

        java.nio.ByteBuffer outBuffer
        Encrypted data buffer. The data starts at outBuffer.position() and ends at outBuffer.limit().
    • Constructor Detail

      • CryptoOutputStream

        protected CryptoOutputStream​(Output output,
                                     CryptoCipher cipher,
                                     int bufferSize,
                                     java.security.Key key,
                                     java.security.spec.AlgorithmParameterSpec params)
                              throws java.io.IOException
        Constructs a CryptoOutputStream.
        Parameters:
        output - the output stream.
        cipher - the CryptoCipher instance.
        bufferSize - the bufferSize.
        key - crypto key for the cipher.
        params - the algorithm parameters.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • CryptoOutputStream

        protected CryptoOutputStream​(java.io.OutputStream outputStream,
                                     CryptoCipher cipher,
                                     int bufferSize,
                                     java.security.Key key,
                                     java.security.spec.AlgorithmParameterSpec params)
                              throws java.io.IOException
        Constructs a CryptoOutputStream.
        Parameters:
        outputStream - the output stream.
        cipher - the CryptoCipher instance.
        bufferSize - the bufferSize.
        key - crypto key for the cipher.
        params - the algorithm parameters.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • CryptoOutputStream

        public CryptoOutputStream​(java.lang.String transformation,
                                  java.util.Properties properties,
                                  java.io.OutputStream outputStream,
                                  java.security.Key key,
                                  java.security.spec.AlgorithmParameterSpec params)
                           throws java.io.IOException
        Constructs a CryptoOutputStream.
        Parameters:
        transformation - the name of the transformation, e.g., AES/CBC/PKCS5Padding. See the Java Cryptography Architecture Standard Algorithm Name Documentation for information about standard transformation names.
        properties - The Properties class represents a set of properties.
        outputStream - the output stream.
        key - crypto key for the cipher.
        params - the algorithm parameters.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • CryptoOutputStream

        public CryptoOutputStream​(java.lang.String transformation,
                                  java.util.Properties properties,
                                  java.nio.channels.WritableByteChannel out,
                                  java.security.Key key,
                                  java.security.spec.AlgorithmParameterSpec params)
                           throws java.io.IOException
        Constructs a CryptoOutputStream.
        Parameters:
        transformation - the name of the transformation, e.g., AES/CBC/PKCS5Padding. See the Java Cryptography Architecture Standard Algorithm Name Documentation for information about standard transformation names.
        properties - The Properties class represents a set of properties.
        out - the WritableByteChannel instance.
        key - crypto key for the cipher.
        params - the algorithm parameters.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • CryptoOutputStream

        protected CryptoOutputStream​(java.nio.channels.WritableByteChannel channel,
                                     CryptoCipher cipher,
                                     int bufferSize,
                                     java.security.Key key,
                                     java.security.spec.AlgorithmParameterSpec params)
                              throws java.io.IOException
        Constructs a CryptoOutputStream.
        Parameters:
        channel - the WritableByteChannel instance.
        cipher - the cipher instance.
        bufferSize - the bufferSize.
        key - crypto key for the cipher.
        params - the algorithm parameters.
        Throws:
        java.io.IOException - if an I/O error occurs.
    • Method Detail

      • checkStream

        protected void checkStream()
                            throws java.io.IOException
        Checks whether the stream is closed.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • close

        public void close()
                   throws java.io.IOException
        Overrides the OutputStream.close(). Closes this output stream and releases any system resources associated with this 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 java.io.OutputStream
        Throws:
        java.io.IOException - if an I/O error occurs.
      • encrypt

        protected void encrypt()
                        throws java.io.IOException
        Does the encryption, input is inBuffer and output is outBuffer.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • encryptFinal

        protected void encryptFinal()
                             throws java.io.IOException
        Does final encryption of the last data.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • flush

        public void flush()
                   throws java.io.IOException
        Overrides the OutputStream.flush(). To flush, we need to encrypt the data in the buffer and write to the underlying stream, then do the flush.
        Specified by:
        flush in interface java.io.Flushable
        Overrides:
        flush in class java.io.OutputStream
        Throws:
        java.io.IOException - if an I/O error occurs.
      • freeBuffers

        protected void freeBuffers()
        Forcibly free the direct buffers.
      • getBufferSize

        protected int getBufferSize()
        Gets the buffer size.
        Returns:
        the buffer size.
      • getCipher

        protected CryptoCipher getCipher()
        Gets the internal Cipher.
        Returns:
        the cipher instance.
      • getInBuffer

        protected java.nio.ByteBuffer getInBuffer()
        Gets the inBuffer.
        Returns:
        the inBuffer.
      • getOutBuffer

        protected java.nio.ByteBuffer getOutBuffer()
        Gets the outBuffer.
        Returns:
        the outBuffer.
      • initCipher

        protected void initCipher()
                           throws java.io.IOException
        Initializes the cipher.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • isOpen

        public boolean isOpen()
        Overrides the Channel.isOpen(). Tells whether or not this channel is open.
        Specified by:
        isOpen in interface java.nio.channels.Channel
        Returns:
        true if, and only if, this channel is open
      • write

        public void write​(byte[] array,
                          int off,
                          int len)
                   throws java.io.IOException
        Overrides the OutputStream.write(byte[], int, int). Encryption is buffer based. If there is enough room in inBuffer, then write to this buffer. If inBuffer is full, then do encryption and write data to the underlying stream.
        Overrides:
        write in class java.io.OutputStream
        Parameters:
        array - the data.
        off - the start offset in the data.
        len - the number of bytes to write.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • write

        public int write​(java.nio.ByteBuffer src)
                  throws java.io.IOException
        Overrides the WritableByteChannel.write(ByteBuffer). Writes a sequence of bytes to this channel from the given buffer.
        Specified by:
        write in interface java.nio.channels.WritableByteChannel
        Parameters:
        src - The buffer from which bytes are to be retrieved.
        Returns:
        The number of bytes written, possibly zero.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • write

        public void write​(int b)
                   throws java.io.IOException
        Overrides the OutputStream.write(byte[]). Writes the specified byte to this output stream.
        Specified by:
        write in class java.io.OutputStream
        Parameters:
        b - the data.
        Throws:
        java.io.IOException - if an I/O error occurs.