de.cgarbs.io
Class MUXInputStream

java.lang.Object
  |
  +--java.io.InputStream
        |
        +--de.cgarbs.io.MUXInputStream

public class MUXInputStream
extends java.io.InputStream

A multiplexing input stream. Data read from this input stream is written unchanged to none, one or multiple attached output streams. The attached output streams can be added, removed, activated and deactivated at any time.

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

 MUXInputStream in_new = new MUXInputStream(in_old);
 in_old.add(System.out);
 

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

Version:
0.0.1
Author:
Christian Garbs

Constructor Summary
MUXInputStream(java.io.InputStream in)
          Creates a new MUXInputStream on top of an existing input stream.
 
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.
 int available()
          Returns the number of bytes that can be read from the underlying input stream without blocking.
 void clear()
          Removes all output streams from the list.
 void close()
          Closes the input stream as well as all (active and inactive) output streams in the list and releases any system resources associated with the streams.
 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.
 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 mark(int readlimit)
          Marks the current position in the underlying input stream.
 boolean markSupported()
          Tests if the underlying input stream supports the mark and reset methods.
 int read()
          Reads the next byte of data from the underlying input stream.
 int read(byte[] b)
          Reads up to b.length bytes of data from the underlying input stream into an array of bytes.
 int read(byte[] b, int off, int len)
          Reads up to len bytes of data from the underlying input stream into an array of bytes.
 void remove(java.io.OutputStream o)
          Removes an output stream from the list.
 void reset()
          Repositions this stream to the position at the time the mark method was last called on the unserlying input stream.
 int size()
          Returns the number of output streams in the list.
 long skip(long n)
          Skips over and discards n bytes of data from the underlying input stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MUXInputStream

public MUXInputStream(java.io.InputStream in)
Creates a new MUXInputStream on top of an existing input stream. This stream initially is not connected to any output streams.
Parameters:
in - the underlying input stream
Method Detail

available

public int available()
              throws java.io.IOException
Returns the number of bytes that can be read from the underlying input stream without blocking.
Overrides:
available in class java.io.InputStream
Returns:
the number of bytes that can be read from this input stream without blocking.
Throws:
java.io.IOException - if an I/O error occurs.

mark

public void mark(int readlimit)
Marks the current position in the underlying input stream. A subsequent call to the reset method repositions this stream at the last marked position so that subsequent reads re-read the same bytes.

The readlimit arguments tells this input stream to allow that many bytes to be read before the mark position gets invalidated.

Overrides:
mark in class java.io.InputStream
Parameters:
readlimit - the maximum limit of bytes that can be read before the mark position becomes invalid.

reset

public void reset()
           throws java.io.IOException
Repositions this stream to the position at the time the mark method was last called on the unserlying input stream.
Overrides:
reset in class java.io.InputStream
Throws:
java.io.IOException - if an I/O error occurs.

markSupported

public boolean markSupported()
Tests if the underlying input stream supports the mark and reset methods.
Overrides:
markSupported in class java.io.InputStream
Returns:
true if the underlying input stream supports the mark and reset method; false otherwise.

skip

public long skip(long n)
          throws java.io.IOException
Skips over and discards n bytes of data from the underlying input stream. The skip method may, for a variety of reasons, end up skipping over some smaller number of bytes, possibly 0. The actual number of bytes skipped is returned.
Overrides:
skip in class java.io.InputStream
Parameters:
n - the number of bytes to be skipped.
Returns:
the actual number of bytes skipped.
Throws:
java.io.IOException - if an I/O error occurs.

read

public int read()
         throws java.io.IOException
Reads the next byte of data from the underlying input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.
Overrides:
read in class java.io.InputStream
Returns:
the next byte of data, or -1 if the end of the stream is reached.
Throws:
java.io.IOException - if an I/O error occurs.

read

public int read(byte[] b)
         throws java.io.IOException
Reads up to b.length bytes of data from the underlying input stream into an array of bytes.
Overrides:
read in class java.io.InputStream
Parameters:
b - the buffer into which the data is read.
Returns:
the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.
Throws:
java.io.IOException - if an I/O error occurs.

read

public int read(byte[] b,
                int off,
                int len)
         throws java.io.IOException
Reads up to len bytes of data from the underlying input stream into an array of bytes. This method blocks until some input is available. If the first argument is null, up to len bytes are read and discarded.
Overrides:
read in class java.io.InputStream
Parameters:
b - the buffer into which the data is read.
off - the start offset of the data.
len - the maximum number of bytes read.
Returns:
the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.
Throws:
java.io.IOException - if an I/O error occurs.

close

public void close()
           throws java.io.IOException
Closes the input stream as well as all (active and inactive) output streams in the list and releases any system resources associated with the streams.
Overrides:
close in class java.io.InputStream
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