ccs.utils
Class DammedOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by java.io.FilterOutputStream
          extended by ccs.utils.DammedOutputStream
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable

public class DammedOutputStream
extends java.io.FilterOutputStream

An output stream filter which only permits a fixed amount of data to be written to the underlying stream. Data written after this "dam" has been reached throws an IOException; so does closing prematurely. This rather strange stream is required by BeeTree, but could conceivably be useful elsewhere. Note that this is not the exact mirror-image of DammedInputStream.


Field Summary
 
Fields inherited from class java.io.FilterOutputStream
out
 
Constructor Summary
DammedOutputStream(java.io.OutputStream out, long quota)
          Creates a dammed output stream.
 
Method Summary
 void close()
          Closes the stream if the quota has been fulfilled.
 void finish()
          Asserts that the write on the stream should be complete, and throws IOException if it isn't.
 void setDam(long quota)
          Sets the dam to a new quota, relative to the current position
 void write(byte[] b, int off, int len)
          Writes len bytes from b starting at off to this output stream.
 void write(int b)
          Writes the specified byte to this 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
 

Constructor Detail

DammedOutputStream

public DammedOutputStream(java.io.OutputStream out,
                          long quota)
Creates a dammed output stream.

Parameters:
out - The underlying output stream.
quota - The number of bytes that may be written to out, or -1 to disable the dam. (Use setDam to activate it).
Method Detail

write

public void write(int b)
           throws java.io.IOException
Writes the specified byte to this output stream.

Overrides:
write in class java.io.FilterOutputStream
Parameters:
b - the byte.
Throws:
java.io.IOException - if an I/O error occurs.

write

public void write(byte[] b,
                  int off,
                  int len)
           throws java.io.IOException
Writes len bytes from b starting at off to this output stream. No bytes are written if doing so would exceed the quota.

Overrides:
write in class java.io.FilterOutputStream
Parameters:
b - the data.
off - the start offset in the data.
len - the number of bytes to write.
Throws:
java.io.IOException - if an I/O error occurs.

finish

public void finish()
            throws java.io.IOException
Asserts that the write on the stream should be complete, and throws IOException if it isn't.

Throws:
java.io.IOException - if the the quota has not been fulfilled.

close

public void close()
           throws java.io.IOException
Closes the stream if the quota has been fulfilled.

Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.FilterOutputStream
Throws:
java.io.IOException - if the the quota has not been fulfilled.

setDam

public void setDam(long quota)
Sets the dam to a new quota, relative to the current position. -1 to disable.