javax.crypto
Class CipherOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by java.io.FilterOutputStream
          extended by javax.crypto.CipherOutputStream
All Implemented Interfaces:
Closeable, Flushable

public class CipherOutputStream
extends FilterOutputStream

A filtered output stream that transforms data written to it with a Cipher before sending it to the underlying output stream.


Field Summary
 
Fields inherited from class java.io.FilterOutputStream
out
 
Constructor Summary
protected CipherOutputStream(OutputStream out)
          Create a cipher output stream with no cipher.
  CipherOutputStream(OutputStream out, Cipher cipher)
          Create a new cipher output stream.
 
Method Summary
 void close()
          Close this output stream, and the sink output stream.
 void flush()
          Flush any pending output.
 void write(byte[] buf)
          Write a byte array to the output stream.
 void write(byte[] buf, int off, int len)
          Write a portion of a byte array to the output stream.
 void write(int b)
          Write a single byte to the output stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CipherOutputStream

public CipherOutputStream(OutputStream out,
                          Cipher cipher)
Create a new cipher output stream. The cipher argument must have already been initialized.

Parameters:
out - The sink for transformed data.
cipher - The cipher to transform data with.

CipherOutputStream

protected CipherOutputStream(OutputStream out)
Create a cipher output stream with no cipher.

Parameters:
out - The sink for transformed data.
Method Detail

close

public void close()
           throws IOException
Close this output stream, and the sink output stream.

This method will first invoke the Cipher.doFinal() method of the underlying Cipher, and writes the output of that method to the sink output stream.

Specified by:
close in interface Closeable
Overrides:
close in class FilterOutputStream
Throws:
IOException - If an I/O error occurs, or if an error is caused by finalizing the transformation.

flush

public void flush()
           throws IOException
Flush any pending output.

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

write

public void write(int b)
           throws IOException
Write a single byte to the output stream.

Overrides:
write in class FilterOutputStream
Parameters:
b - The next byte.
Throws:
IOException - If an I/O error occurs, or if the underlying cipher is not in the correct state to transform data.

write

public void write(byte[] buf)
           throws IOException
Write a byte array to the output stream.

Overrides:
write in class FilterOutputStream
Parameters:
buf - The next bytes.
Throws:
IOException - If an I/O error occurs, or if the underlying cipher is not in the correct state to transform data.

write

public void write(byte[] buf,
                  int off,
                  int len)
           throws IOException
Write a portion of a byte array to the output stream.

Overrides:
write in class FilterOutputStream
Parameters:
buf - The next bytes.
off - The offset in the byte array to start.
len - The number of bytes to write.
Throws:
IOException - If an I/O error occurs, or if the underlying cipher is not in the correct state to transform data.