de.schlichtherle.crypto.io
Class CipherOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by java.io.FilterOutputStream
          extended by de.schlichtherle.crypto.io.CipherOutputStream
All Implemented Interfaces:
Closeable, Flushable
Direct Known Subclasses:
RaesOutputStream

public class CipherOutputStream
extends FilterOutputStream

Similar to javax.crypto.CipherOutputStream, with some exceptions:

Author:
Christian Schlichtherle

Field Summary
protected  BufferedBlockCipher cipher
          The buffered block cipher used for preprocessing the output.
 
Fields inherited from class java.io.FilterOutputStream
out
 
Constructor Summary
CipherOutputStream(OutputStream out, BufferedBlockCipher cipher)
          Creates a new instance of CipherOutputStream.
 
Method Summary
 void close()
          Closes this output stream and releases any resources associated with it.
 void finish()
          Finishes this stream and resets it to it's initial state.
 void write(byte[] buf, int off, int len)
          Ciphers and writes the contents of the given byte array to the underlying output stream.
 void write(int b)
          Ciphers and writes the given byte to the underlying output stream.
 
Methods inherited from class java.io.FilterOutputStream
flush, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

cipher

protected BufferedBlockCipher cipher
The buffered block cipher used for preprocessing the output.

Constructor Detail

CipherOutputStream

public CipherOutputStream(OutputStream out,
                          BufferedBlockCipher cipher)
Creates a new instance of CipherOutputStream. Please note that unlike javax.crypto.CipherOutputStream, the cipher does not need to be initialized before calling this constructor. However, the cipher must be initialized before anything is actually written to this stream or before this stream is closed.

Parameters:
out - The output stream to write the encrypted or decrypted data to. Maybe null if initialized by the subclass constructor.
cipher - The cipher to use for encryption or decryption. Maybe null for subsequent initialization by a subclass.
Method Detail

write

public void write(int b)
           throws IOException
Ciphers and writes the given byte to the underlying output stream.

Overrides:
write in class FilterOutputStream
Parameters:
b - The byte to cipher and write.
Throws:
IOException - If out or cipher aren't properly initialized, the stream has been closed or an I/O error occured.

write

public void write(byte[] buf,
                  int off,
                  int len)
           throws IOException
Ciphers and writes the contents of the given byte array to the underlying output stream.

Overrides:
write in class FilterOutputStream
Parameters:
buf - The buffer holding the data to cipher and write.
off - The start offset in the data buffer.
len - The number of bytes to cipher and write.
Throws:
IOException - If out or cipher aren't properly initialized, the stream has been closed or an I/O error occured.

finish

public void finish()
            throws IOException
Finishes this stream and resets it to it's initial state. Calling this method causes all remaining buffered bytes to be written, padding to be added if necessary and the underlying output stream to get flushed.

Please note that subsequent calls to any write operations after this method may cause an error in the output data if padding is used!

Throws:
IOException - If out or cipher aren't properly initialized, the stream has been closed, an I/O error occured the cipher text is invalid, i.e. required padding information is missing.

close

public void close()
           throws IOException
Closes this output stream and releases any resources associated with it. This method calls finish() and then closes and nullifies the underlying output stream FilterOutputStream.out and the cipher cipher.

Specified by:
close in interface Closeable
Overrides:
close in class FilterOutputStream
Throws:
IOException - If an I/O error occurs.