Class CryptoOutputStream

java.lang.Object
java.io.OutputStream
org.apache.commons.crypto.stream.CryptoOutputStream
All Implemented Interfaces:
Closeable, Flushable, AutoCloseable, Channel, WritableByteChannel
Direct Known Subclasses:
CtrCryptoOutputStream

public class CryptoOutputStream extends OutputStream implements 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 Details

    • 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 Key key
      Crypto key for the cipher.
    • params

      private final AlgorithmParameterSpec params
      the algorithm parameters
    • closed

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

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

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

    • CryptoOutputStream

      protected CryptoOutputStream(Output output, CryptoCipher cipher, int bufferSize, Key key, AlgorithmParameterSpec params) throws 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:
      IOException - if an I/O error occurs.
    • CryptoOutputStream

      protected CryptoOutputStream(OutputStream outputStream, CryptoCipher cipher, int bufferSize, Key key, AlgorithmParameterSpec params) throws 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:
      IOException - if an I/O error occurs.
    • CryptoOutputStream

      public CryptoOutputStream(String transformation, Properties properties, OutputStream outputStream, Key key, AlgorithmParameterSpec params) throws 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:
      IOException - if an I/O error occurs.
    • CryptoOutputStream

      public CryptoOutputStream(String transformation, Properties properties, WritableByteChannel out, Key key, AlgorithmParameterSpec params) throws 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:
      IOException - if an I/O error occurs.
    • CryptoOutputStream

      protected CryptoOutputStream(WritableByteChannel channel, CryptoCipher cipher, int bufferSize, Key key, AlgorithmParameterSpec params) throws 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:
      IOException - if an I/O error occurs.
  • Method Details

    • checkStream

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

      public void close() throws IOException
      Overrides the OutputStream.close(). Closes this output stream and releases any system resources associated with this stream.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Channel
      Specified by:
      close in interface Closeable
      Overrides:
      close in class OutputStream
      Throws:
      IOException - if an I/O error occurs.
    • encrypt

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

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

      public void flush() throws 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 Flushable
      Overrides:
      flush in class OutputStream
      Throws:
      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 ByteBuffer getInBuffer()
      Gets the inBuffer.
      Returns:
      the inBuffer.
    • getOutBuffer

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

      protected void initCipher() throws IOException
      Initializes the cipher.
      Throws:
      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 Channel
      Returns:
      true if, and only if, this channel is open
    • write

      public void write(byte[] array, int off, int len) throws 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 OutputStream
      Parameters:
      array - the data.
      off - the start offset in the data.
      len - the number of bytes to write.
      Throws:
      IOException - if an I/O error occurs.
    • write

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

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