1
package org.crosswire.io;
3
import java.io.IOException;
4
import java.io.OutputStream;
5
import java.util.Vector;
7
import org.crosswire.util.Level;
8
import org.crosswire.util.Logger;
11
* TeeOutputStream allows you to have one stream act as a proxy
12
* to a number of other streams, so that output to one goes to all
16
public class TeeOutputStream extends OutputStream
19
* Add the specified OutputStream to the list of streams.
21
* @return "this". So we can do <pre>tee.add(a).add(b).add(c);</pre>
23
public TeeOutputStream add(OutputStream out)
25
if (!list.contains(out))
34
* Remove the specified OutputStream from the list of streams
35
* used in all outputs.
36
* @param out The Stream to be removed
38
public boolean remove(OutputStream out)
40
return list.removeElement(out);
44
* Override to write to ass the listed Streams.
45
* @param b The byte to be written, as normal.
47
public void write(int b) throws IOException
49
for (int i=0; i<list.size(); i++)
51
OutputStream out = (OutputStream) list.elementAt(i);
57
* If someone closes the TeeOutputStream then we go round
58
* and close all the Streams on the stack.
60
public void close() throws IOException
62
// Close each OutputStream catching and noting IOExceptions
63
// Then rethrow at end if any failed.
64
boolean failed = false;
66
for (int i=0; i<list.size(); i++)
70
OutputStream out = (OutputStream) list.elementAt(i);
75
log.log(Level.INFO, "Error in closing loop", ex);
80
list.removeAllElements();
82
if (failed) throw new IOException();
86
* @return The number of items on the stack
94
* Primarily for debugging. Reports on th state of the Stream.
95
* @return A String containing the report.
97
public String toString()
100
String NEWLINE = System.getProperty("line.separator", "\r\n");
102
retcode += "There are " + list.size() + " output(s)" + NEWLINE;
104
for (int i=list.size()-1; i>=0; i--)
106
OutputStream out = (OutputStream) list.elementAt(i);
107
retcode += "Stream" + i + ": " + out.toString() + NEWLINE;
113
private Vector list = new Vector();
115
/** The log stream */
116
protected static Logger log = Logger.getLogger(TeeOutputStream.class);