org.dellroad.stuff.io
Class OutputStreamWriter

java.lang.Object
  extended by java.io.OutputStream
      extended by java.io.FilterOutputStream
          extended by org.dellroad.stuff.io.OutputStreamWriter
All Implemented Interfaces:
Closeable, Flushable

public class OutputStreamWriter
extends FilterOutputStream

Serializes zero or more OutputStreams inside a single underlying OutputStream. The results can be deserialized as equally many distinct InputStreams on the other end using an InputStreamReader. Each OutputStream may contain an arbitrary amount of data.

To use this class, invoke start() to start a new OutputStream, write to it by writing to this class normally, and then use stop() to close the current OutputStream. A new, subsequent OutputStream is created by invoking start() again.

Each OutputStream written in this way will be read as distinct InputStream by the InputStreamReader at the other end.

Instances of this class are thread safe.

See Also:
InputStreamReader

Field Summary
 
Fields inherited from class java.io.FilterOutputStream
out
 
Constructor Summary
OutputStreamWriter(OutputStream output)
          Constructor.
 
Method Summary
 void close()
          Close this instance.
 void start()
          Start a new OutputStream.
 void stop()
          End the current OutputStream.
 void write(int ch)
           
 
Methods inherited from class java.io.FilterOutputStream
flush, write, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

OutputStreamWriter

public OutputStreamWriter(OutputStream output)
Constructor.

Parameters:
output - the underlying OutputStream that will carry nested OutputStreams within it
Method Detail

start

public void start()
           throws IOException
Start a new OutputStream.

Throws:
IOException - if this instance is closed
IOException - if an OutputStream is already started

stop

public void stop()
          throws IOException
End the current OutputStream. This flushes the underlying output. A new OutputStream will be created upon the next invocation of start().

Throws:
IOException - if this instance is closed
IOException - if no OutputStream is currently started
IOException - if the underlying OutputStream throws an exception

close

public void close()
           throws IOException
Close this instance. Does nothing if already closed. If there an OutputStream is already started when this method is invoked, it will be implicitly stopped.

This ends the current OutputStream and closes the underlying input.

Specified by:
close in interface Closeable
Overrides:
close in class FilterOutputStream
Throws:
IOException - if an there is an error closing the underlying OutputStream

write

public void write(int ch)
           throws IOException
Overrides:
write in class FilterOutputStream
Throws:
IOException