to.etc.util
Class FileBackedBuffer

java.lang.Object
  extended by java.io.OutputStream
      extended by to.etc.util.FileBackedBuffer
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable

public class FileBackedBuffer
extends java.io.OutputStream

A buffer which accepts data and buffers it. The data can be obtained again by reading an input stream. If the data grows too large (user definable) it can flush the data into a tempfile. This class is not threadsafe of course.

Because this class may retain a file it is important that it gets cleaned up after use by calling discard().

Author:
Frits Jalvingh Created on Aug 10, 2006

Constructor Summary
FileBackedBuffer()
           
FileBackedBuffer(int size)
           
FileBackedBuffer(int size, int initial)
           
 
Method Summary
 void clear()
          Removes all contents from this thingy, and reopens it as an output stream.
 void close()
          This closes the write channel.
 void copy(java.io.OutputStream os)
          Sends all of the contained data to the outputstream.
 void discard()
          This MUST be called after use.
protected  void finalize()
          This finalizer at least tries to cleanup the mess if discard() was not called...
 java.io.InputStream getInputStream()
          Returns an inputstream which reads this.
 int size()
           
 void write(byte[] b, int off, int len)
           
 void write(int b)
           
 
Methods inherited from class java.io.OutputStream
flush, write
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileBackedBuffer

public FileBackedBuffer()

FileBackedBuffer

public FileBackedBuffer(int size)

FileBackedBuffer

public FileBackedBuffer(int size,
                        int initial)
Method Detail

discard

public void discard()
This MUST be called after use. It releases all resources held by this object.


finalize

protected void finalize()
                 throws java.lang.Throwable
This finalizer at least tries to cleanup the mess if discard() was not called...

Overrides:
finalize in class java.lang.Object
Throws:
java.lang.Throwable
See Also:
Object.finalize()

close

public void close()
           throws java.io.IOException
This closes the write channel. After this the data can be read using getInputStream() or another call.

Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.OutputStream
Throws:
java.io.IOException
See Also:
OutputStream.close()

write

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

write

public void write(int b)
           throws java.io.IOException
Specified by:
write in class java.io.OutputStream
Throws:
java.io.IOException

clear

public void clear()
Removes all contents from this thingy, and reopens it as an output stream.


getInputStream

public java.io.InputStream getInputStream()
                                   throws java.io.IOException
Returns an inputstream which reads this. This always returns the same stream instance(!) so it cannot be called >1ce while the previous stream is still open. If you do you'll get exceptioned.

Throws:
java.io.IOException

size

public int size()

copy

public void copy(java.io.OutputStream os)
          throws java.io.IOException
Sends all of the contained data to the outputstream.

Parameters:
os -
Throws:
java.io.IOException