Class CryptoInputStream

java.lang.Object
java.io.InputStream
org.apache.commons.crypto.stream.CryptoInputStream
All Implemented Interfaces:
Closeable, AutoCloseable, Channel, ReadableByteChannel
Direct Known Subclasses:
CtrCryptoInputStream

public class CryptoInputStream extends InputStream implements ReadableByteChannel
CryptoInputStream reads input data and decrypts data in stream manner. It supports any mode of operations such as AES CBC/CTR/GCM mode in concept.It is not thread-safe.
  • Field Details

  • Constructor Details

  • Method Details

    • available

      public int available() throws IOException
      Overrides the InputStream.available(). Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream.
      Overrides:
      available in class InputStream
      Returns:
      an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking or 0 when it reaches the end of the input stream.
      Throws:
      IOException - if an I/O error occurs.
    • 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 InputStream.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 InputStream
      Throws:
      IOException - if an I/O error occurs.
    • decrypt

      protected void decrypt() 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().
      Throws:
      IOException - if an I/O error occurs.
    • decryptFinal

      protected void decryptFinal() throws IOException
      Does final of the cipher to end the decrypting stream.
      Throws:
      IOException - if an I/O error occurs.
    • decryptMore

      protected int decryptMore() throws IOException
      Decrypts more data by reading the under layer stream. The decrypted data will be put in the output buffer. If the end of the under stream reached, we will do final of the cipher to finish all the decrypting of data.
      Returns:
      The number of decrypted data. return -1 (if end of the decrypted stream) return 0 (no data now, but could have more later)
      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 bufferSize.
    • getCipher

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

      protected Input getInput()
      Gets the input.
      Returns:
      the input.
    • getKey

      protected Key getKey()
      Gets the key.
      Returns:
      the key.
    • getParams

      Gets the specification of cryptographic parameters.
      Returns:
      the params.
    • 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().
      Specified by:
      isOpen in interface Channel
      Returns:
      true if, and only if, this channel is open.
    • markSupported

      public boolean markSupported()
      Overrides:
      markSupported in class InputStream
      Returns:
      false,the CtrCryptoInputStream don't support the mark method.
    • read

      public int read() throws IOException
      Overrides the InputStream.read(). Reads the next byte of data from the input stream.
      Specified by:
      read in class InputStream
      Returns:
      the next byte of data, or EOS (-1) if the end of the stream is reached.
      Throws:
      IOException - if an I/O error occurs.
    • read

      public int read(byte[] array, int off, int len) throws IOException
      Overrides the InputStream.read(byte[], int, int). Decryption is buffer based. If there is data in outBuffer, then read it out of this buffer. If there is no data in outBuffer, then read more from the underlying stream and do the decryption.
      Overrides:
      read in class InputStream
      Parameters:
      array - the buffer into which the decrypted data is read.
      off - the buffer offset.
      len - the maximum number of decrypted data bytes to read.
      Returns:
      int the total number of decrypted data bytes read into the buffer.
      Throws:
      IOException - if an I/O error occurs.
    • read

      public int read(ByteBuffer dst) throws IOException
      Overrides the ReadableByteChannel.read(ByteBuffer). Reads a sequence of bytes from this channel into the given buffer.
      Specified by:
      read in interface ReadableByteChannel
      Parameters:
      dst - The buffer into which bytes are to be transferred.
      Returns:
      The number of bytes read, possibly zero, or EOS (-1) if the channel has reached end-of-stream.
      Throws:
      IOException - if an I/O error occurs.
    • skip

      public long skip(long n) throws IOException
      Overrides the InputStream.skip(long). Skips over and discards n bytes of data from this input stream.
      Overrides:
      skip in class InputStream
      Parameters:
      n - the number of bytes to be skipped.
      Returns:
      the actual number of bytes skipped.
      Throws:
      IOException - if an I/O error occurs.