tribble.io
Class MultiOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by tribble.io.MultiOutputStream
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable

public class MultiOutputStream
extends java.io.OutputStream

Multiple output stream. Replicates all output data written to it among one or more output streams.

Data written to this stream are written to all of the underlying output streams in the order that the streams were added to this object (see the addOutput() method).

Note that data is written to all of the output streams even if any of them throw IOExceptions, i.e., the output operation does not stop after the first exception, but continues until all of the streams have been written to. If more than one of the output streams throws an IOException, only the first exception thrown is re-thrown by these methods.

Source code:
http://david.tribble.com/src/java/tribble/io/MultiOutputStream.java
Documentation:
http://david.tribble.com/docs/tribble/io/MultiOutputStream.html

Since:
2008-09-25
Version:
$Revision: 1.3 $ $Date: 2008/09/26 14:38:30 $
Author:
David R. Tribble (david@tribble.com)

Copyright ©2008 by David R. Tribble, all rights reserved.
Permission is granted to any person or entity except those designated by by the United States Department of State as a terrorist, or terrorist government or agency, to use and distribute this source code provided that the original copyright notice remains present and unaltered.


Field Summary
(package private) static java.lang.String REV
          Revision information.
static MultiOutputStream stdouts
          A multiple output stream which writes output to both System.out and System.err.
 
Constructor Summary
MultiOutputStream()
          Default constructor.
MultiOutputStream(java.io.OutputStream out)
          Constructor.
MultiOutputStream(java.io.OutputStream out1, java.io.OutputStream out2)
          Constructor.
 
Method Summary
 int addOutput(java.io.OutputStream out)
          Add an output stream to this object.
 void close()
          Close all the output streams of this object.
 void flush()
          Flush all the output streams of this object.
 java.io.OutputStream getOutput(int n)
          Retrieve an output stream from this object.
 void write(byte[] buf)
          Write a set of bytes to the output streams of this object.
 void write(byte[] buf, int off, int len)
          Write a set of bytes to the output streams of this object.
 void write(int b)
          Write a byte to the output streams of this object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

REV

static final java.lang.String REV
Revision information.

See Also:
Constant Field Values

stdouts

public static final MultiOutputStream stdouts
A multiple output stream which writes output to both System.out and System.err.

Constructor Detail

MultiOutputStream

public MultiOutputStream()
Default constructor. Constructs a multiple output stream with no underlying output streams. Data written to this stream is ignored until an underlying output stream is established by calling addOutput().

Since:
1.1, 2008-09-25

MultiOutputStream

public MultiOutputStream(java.io.OutputStream out)
Constructor. Contructs a multiple output stream with a single underlying output stream.

Parameters:
out - An output stream, to which all the output of this object is written.
Since:
1.1, 2008-09-25
See Also:
addOutput()

MultiOutputStream

public MultiOutputStream(java.io.OutputStream out1,
                         java.io.OutputStream out2)
Constructor. Contructs a multiple output stream with two underlying output streams.

Parameters:
out1 - An output stream, to which all the output of this object is written.
out2 - Another output stream, to which all the output of this object is written.
Since:
1.2, 2008-09-25
See Also:
addOutput()
Method Detail

addOutput

public int addOutput(java.io.OutputStream out)
Add an output stream to this object.

Parameters:
out - An output stream, to which all the output of this object is written.
Returns:
The index that the specified output stream (out) has within this object. Note that the first output stream added has an index of 0.
Throws:
java.lang.NullPointerException - (unchecked) Thrown if out is null.
Since:
1.1, 2008-09-25
See Also:
getOutput()

getOutput

public java.io.OutputStream getOutput(int n)
Retrieve an output stream from this object.

Parameters:
n - Index of the underlying output stream to retrieve. The index number is the value returned by a previous call to addOutput().
Returns:
The index that the specified output stream (out) has within this object. Note that the first output stream added has an index of 0.
Throws:
java.lang.ArrayIndexOutOfBoundsException - (unchecked) Thrown if n does not specify a valid output stream index.
Since:
1.2, 2008-09-25
See Also:
addOutput()

flush

public void flush()
           throws java.io.IOException
Flush all the output streams of this object. All of the underlying output streams are flushed, forcing any pending output data to be written.

Specified by:
flush in interface java.io.Flushable
Overrides:
flush in class java.io.OutputStream
Throws:
java.io.IOException - Thrown if flushing any of the underlying output streams causes an IOException to be thrown. If this happens, the first exception thrown is re-thrown by this method after all of the underlying streams are flushed.
Since:
1.2, 2008-09-25

close

public void close()
           throws java.io.IOException
Close all the output streams of this object. All of the underlying output streams are flushed and closed, and then forgotten, so this object will no longer have any output streams assigned to it. Any subsequent writes to this object will therefore be ignored.

Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.OutputStream
Throws:
java.io.IOException - Thrown if closing any of the underlying output streams causes an IOException to be thrown. If this happens, the first exception thrown is re-thrown by this method after all of the underlying streams are closed.
Since:
1.2, 2008-09-25

write

public void write(int b)
           throws java.io.IOException
Write a byte to the output streams of this object.

Specified by:
write in class java.io.OutputStream
Parameters:
b - An output byte.
Throws:
java.io.IOException - Thrown if any of the underlying output streams throws this exception.
Since:
1.1, 2008-09-25

write

public void write(byte[] buf)
           throws java.io.IOException
Write a set of bytes to the output streams of this object. This is identical to the call:
    write(buf, 0, buf.length)

Overrides:
write in class java.io.OutputStream
Parameters:
buf - An array of bytes to be written.
Throws:
java.io.IOException - Thrown if any of the underlying output streams throws this exception.
Since:
1.1, 2008-09-25

write

public void write(byte[] buf,
                  int off,
                  int len)
           throws java.io.IOException
Write a set of bytes to the output streams of this object.

Overrides:
write in class java.io.OutputStream
Parameters:
buf - An array of bytes to be written.
off - Index of the first byte in buf to write.
len - Number of bytes in buf to write.
Throws:
java.io.IOException - Thrown if any of the underlying output streams throws this exception.
Since:
1.1, 2008-09-25