de.cgarbs.io
Class MUXOutputStream

java.lang.Object
  |
  +--java.io.OutputStream
        |
        +--de.cgarbs.io.MUXOutputStream

public class MUXOutputStream
extends java.io.OutputStream

A multiplexing output stream. Data written to this stream is written unchanged to none, one or multiple underlying output streams. Underlying output streams can be added, removed, activated and deactivated at any time.

This is particularly useful for debugging purposes. If you have an output stream out_old (e.g. writing to a socket) you can easily debug it's data to System.out:

 MUXOutputStream out_new = new MUXOutputStream(out_old,System.out);
 

This class is licensed under the GNU LESSER GENERAL PUBLIC LICENSE.

Version:
0.0.1
Author:
Christian Garbs

Constructor Summary
MUXOutputStream()
          Creates a new MUXOutputStream with no underlying output streams.
MUXOutputStream(java.io.OutputStream o)
          Creates a new MUXOutputStream with one underlying output stream.
MUXOutputStream(java.io.OutputStream o1, java.io.OutputStream o2)
          Creates a new MUXOutputStream with two underlying output streams.
 
Method Summary
 void activate(java.io.OutputStream o)
          Activates an output stream.
 void activateAll()
          Activates all output streams.
 void activateOnly(java.io.OutputStream o)
          Activates only one output stream.
 void add(java.io.OutputStream o)
          Adds an output stream to the list.
 void clear()
          Removes all output streams from the list.
 void close()
          Closes all (active and inactive) output streams in the list.
 boolean contains(java.io.OutputStream o)
          Tests if an output stream is contained in the list.
 void deactivate(java.io.OutputStream o)
          Deactivates an output stream.
 void deactivateAll()
          Deactivates all output streams.
 void flush()
          Flushes all (activated and deactivated) output streams, forcing any bufferd bytes to be written.
 boolean isActive(java.io.OutputStream o)
          Tests if an output stream is active.
 boolean isEmpty()
          Tests if there are no output streams in the list.
static void main(java.lang.String[] args)
          Runs a demo of this class.
 void remove(java.io.OutputStream o)
          Removes an output stream from the list.
 int size()
          Returns the number of output streams in the list.
 void write(byte[] b)
          Writes b.length bytes from the specified byte array to all active output streams.
 void write(byte[] b, int off, int len)
          Writes len bytes from the specified byte array starting at off to all active output streams.
 void write(int j)
          Writes the specified byte to all active output streams.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MUXOutputStream

public MUXOutputStream()
Creates a new MUXOutputStream with no underlying output streams. As long as no output streams are added, this MUXOutputStream behaves like a Null Device

MUXOutputStream

public MUXOutputStream(java.io.OutputStream o)
Creates a new MUXOutputStream with one underlying output stream. The output stream is activated by default.
Parameters:
o - the output stream

MUXOutputStream

public MUXOutputStream(java.io.OutputStream o1,
                       java.io.OutputStream o2)
Creates a new MUXOutputStream with two underlying output streams. Both output streams are activated by default.
Parameters:
o1 - the first output stream
o2 - the second output stream
Method Detail

flush

public void flush()
           throws java.io.IOException
Flushes all (activated and deactivated) output streams, forcing any bufferd bytes to be written.
Overrides:
flush in class java.io.OutputStream
Throws:
java.io.IOException - if an I/O error occurs

close

public void close()
           throws java.io.IOException
Closes all (active and inactive) output streams in the list.
Overrides:
close in class java.io.OutputStream
Throws:
java.io.IOException - if an I/O error occurs

write

public void write(int j)
           throws java.io.IOException
Writes the specified byte to all active output streams.
Overrides:
write in class java.io.OutputStream
Parameters:
j - the byte
Throws:
java.io.IOException - if an I/O error occurs

write

public void write(byte[] b)
           throws java.io.IOException
Writes b.length bytes from the specified byte array to all active output streams.
Overrides:
write in class java.io.OutputStream
Parameters:
b - the data
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 the specified byte array starting at off to all active output streams.
Overrides:
write in class java.io.OutputStream
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

remove

public void remove(java.io.OutputStream o)
Removes an output stream from the list.
Parameters:
o - the output stream to remove from the list

size

public int size()
Returns the number of output streams in the list.
Returns:
the number of streams in the list

contains

public boolean contains(java.io.OutputStream o)
Tests if an output stream is contained in the list.
Parameters:
o - the output stream to test
Returns:
true - the output stream is part of the list

clear

public void clear()
Removes all output streams from the list.

add

public void add(java.io.OutputStream o)
Adds an output stream to the list. The output stream activated by default.
Parameters:
o - the output stream to add

isEmpty

public boolean isEmpty()
Tests if there are no output streams in the list.
Returns:
true - there are no output streams in the list

activate

public void activate(java.io.OutputStream o)
Activates an output stream.
Parameters:
o - the output stream to activate

deactivate

public void deactivate(java.io.OutputStream o)
Deactivates an output stream.
Parameters:
o - the output stream to deactivate

isActive

public boolean isActive(java.io.OutputStream o)
Tests if an output stream is active.
Parameters:
o - the output stream to check
Returns:
true - the output stream is active

activateAll

public void activateAll()
Activates all output streams.

deactivateAll

public void deactivateAll()
Deactivates all output streams.

activateOnly

public void activateOnly(java.io.OutputStream o)
Activates only one output stream. All other output streams are deactivated.
Parameters:
o - the output stream to activate

main

public static void main(java.lang.String[] args)
                 throws java.io.IOException
Runs a demo of this class.
Parameters:
args - command line parameters (not used)
Throws:
java.io.IOException - some error occured while reading from stdin and writing to stdout