de.unibi.techfak.misc
Class MultiplyOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by de.unibi.techfak.misc.MultiplyOutputStream
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable

public class MultiplyOutputStream
extends java.io.OutputStream

This OutputStream is designed to multiply the output data and send it to all registered output streams. You can use this class to write to a file and at the same time to a graphical component, or at the same time to two files.
Any Exception thrown by a stream is ignored. If no stream is set, the output is not redirected. But for the purpose of erasing output use NilOutputStream.


Field Summary
private  java.util.Vector streams
          Holds the streams all output is sent to.
 
Constructor Summary
MultiplyOutputStream()
          Constructs this class with no stream present to send data to.
MultiplyOutputStream(java.io.OutputStream os)
          Registers the first output stream to this class, more can be added using add(OutputStream).
MultiplyOutputStream(java.io.OutputStream os1, java.io.OutputStream os2)
          Registers the first two output streams to this class, more can be added using add(OutputStream).
 
Method Summary
 void add(java.io.OutputStream os)
          Registers the given OutputStream.
 void close()
          This call is directed to all registered OutputStreams.
 int count()
           
 void flush()
          This call is directed to all registered OutputStreams.
 java.io.OutputStream get(int index)
           
 boolean remove(java.io.OutputStream os)
          Removes the given OutputStream from the list of receivers.
 void removeAll()
          Removes all receivers.
 void write(byte[] b)
          This call is directed to all registered OutputStreams.
 void write(byte[] b, int offset, int len)
          This call is directed to all registered OutputStreams.
 void write(int b)
          The byte is sent to all registered OutputStreams.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

streams

private java.util.Vector streams
Holds the streams all output is sent to.

Constructor Detail

MultiplyOutputStream

public MultiplyOutputStream()
Constructs this class with no stream present to send data to. Streams can be added using add(OutputStream).


MultiplyOutputStream

public MultiplyOutputStream(java.io.OutputStream os)
Registers the first output stream to this class, more can be added using add(OutputStream).

Parameters:
os - The first output stream the data is sent to.

MultiplyOutputStream

public MultiplyOutputStream(java.io.OutputStream os1,
                            java.io.OutputStream os2)
Registers the first two output streams to this class, more can be added using add(OutputStream).

Parameters:
os1 - The first output stream the data is sent to.
os2 - The second output stream the data is sent to.
Method Detail

close

public void close()
This call is directed to all registered OutputStreams.

Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.OutputStream

write

public void write(int b)
The byte is sent to all registered OutputStreams.

Specified by:
write in class java.io.OutputStream
Parameters:
b - The byte to output.

write

public void write(byte[] b,
                  int offset,
                  int len)
This call is directed to all registered OutputStreams.

Overrides:
write in class java.io.OutputStream
Parameters:
b - The byte buffer to be sent to all streams.
offset - The index of the first byte in the buffer to sent.
len - The number of bytes to sent.

write

public void write(byte[] b)
This call is directed to all registered OutputStreams.

Overrides:
write in class java.io.OutputStream
Parameters:
b - The byte buffer to be sent to all streams.

flush

public void flush()
This call is directed to all registered OutputStreams.

Specified by:
flush in interface java.io.Flushable
Overrides:
flush in class java.io.OutputStream

add

public void add(java.io.OutputStream os)
Registers the given OutputStream. If it is null, the method does nothing.

Parameters:
os - The stream to get registered.

remove

public boolean remove(java.io.OutputStream os)
Removes the given OutputStream from the list of receivers. If it is null or if it could not be found, the method does nothing else but to return false.
Note, that if a stream was registered more than once one needs to call remove(OutputStream) until it returns false.

Parameters:
os - The stream to get removed from the list of receivers.
Returns:
true, if the stream could be removed, false otherwise.

removeAll

public void removeAll()
Removes all receivers.


count

public int count()
Returns:
The number of registered OutputStreams.

get

public java.io.OutputStream get(int index)
Returns:
The OutputStreams with the index.