org.n52.wps.io.binary
Class LargeBufferStream

java.lang.Object
  extended by java.io.OutputStream
      extended by org.n52.wps.io.binary.LargeBufferStream
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable

public class LargeBufferStream
extends java.io.OutputStream

A fully buffered output stream using local disk storage for large data.

Initially this output stream buffers to memory, like ByteArrayOutputStream might do, but it shifts to using an on disk temporary file if the output gets too large.

The content of this buffered stream may be sent to another OutputStream only after this stream has been properly closed by close().


Constructor Summary
LargeBufferStream()
          Create a new empty temporary buffer.
 
Method Summary
 void close()
           
 void destroy()
          Clear this buffer so it has no data, and cannot be used again.
 long length()
          Obtain the length (in bytes) of the buffer.
 void write(byte[] b, int off, int len)
           
 void write(int b)
           
 void writeTo(java.io.OutputStream os)
          Send this buffer to an output stream.
 
Methods inherited from class java.io.OutputStream
flush, write
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LargeBufferStream

public LargeBufferStream()
Create a new empty temporary buffer.

Method Detail

write

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

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

close

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

length

public long length()
Obtain the length (in bytes) of the buffer.

The length is only accurate after close() has been invoked.

Returns:
total length of the buffer, in bytes.

writeTo

public void writeTo(java.io.OutputStream os)
             throws java.io.IOException
Send this buffer to an output stream.

This method may only be invoked after close() has completed normally, to ensure all data is completely transferred.

Parameters:
os - stream to send this buffer's complete content to.
pm - if not null progress updates are sent here. Caller should initialize the task and the number of work units to length()/1024.
Throws:
java.io.IOException - an error occurred reading from a temporary file on the local system, or writing to the output stream.

destroy

public void destroy()
Clear this buffer so it has no data, and cannot be used again.