ccs.crypt
Class CipherOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by java.io.FilterOutputStream
          extended by ccs.crypt.CipherOutputStream
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable
Direct Known Subclasses:
AbstractBlockCipherOutputStream

public abstract class CipherOutputStream
extends java.io.FilterOutputStream


Field Summary
protected  long plainlen
          The plaintext length for this stream.
protected  byte[] temp8
          An 8-byte scratch space.
 
Fields inherited from class java.io.FilterOutputStream
out
 
Constructor Summary
CipherOutputStream(java.io.OutputStream os, long plainlen)
          Construct a CipherOutputStream.
 
Method Summary
 void close()
           
abstract  void finish()
          Completes the encryption but does not close the underlying stream.
 void flush()
          Flushes at least the underlying stream.
abstract  void write(byte[] b, int off, int len)
           
 void write(int b)
          writes a single byte.
 
Methods inherited from class java.io.FilterOutputStream
write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

temp8

protected byte[] temp8
An 8-byte scratch space. Useful for a) buffering single-byte reads; b) working with packed longs.

See Also:
Unsigned.writePacked(java.io.OutputStream, long, byte[])

plainlen

protected long plainlen
The plaintext length for this stream. It is up to subclasses to work with this appropriately.

Constructor Detail

CipherOutputStream

public CipherOutputStream(java.io.OutputStream os,
                          long plainlen)
Construct a CipherOutputStream. NOTE: this ctor does *not* set the dam, that's up to subclasses. Recommended strategy: on first write (set a flag to determine this) set the dam to the ciphertext length, then write away. This is less friendly than it could be - wrong-length writes will throw in a non-obvious place - but it's the only way to avoid scribbling on the underlying stream.

Method Detail

write

public void write(int b)
           throws java.io.IOException
writes a single byte. This by default calls the method of three arguments; this is inefficient and should be avoided where efficiency is an issue.

Overrides:
write in class java.io.FilterOutputStream
Parameters:
b - The value (in the range 0-255) to be written
Throws:
java.io.IOException - if an error occurs.

flush

public void flush()
           throws java.io.IOException
Flushes at least the underlying stream. However, some subclasses (especially block ciphers) may cache plaintext in internal buffers for encryption en bloc; this cached plaintext might not be flushed. Use finish() to ensure that all plaintext has been encrypted and written.

Specified by:
flush in interface java.io.Flushable
Overrides:
flush in class java.io.FilterOutputStream
Throws:
java.io.IOException - If thrown by the underlying stream.

write

public abstract void write(byte[] b,
                           int off,
                           int len)
                    throws java.io.IOException
Overrides:
write in class java.io.FilterOutputStream
Throws:
java.io.IOException

finish

public abstract void finish()
                     throws java.io.IOException
Completes the encryption but does not close the underlying stream.

Throws:
java.io.IOException - if the finish failed for some reason.

close

public void close()
           throws java.io.IOException
Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.FilterOutputStream
Throws:
java.io.IOException