~sword-devel/jsword/trunk

« back to all changes in this revision

Viewing changes to jsword/java/historic/org/crosswire/io/TeeOutputStream.java

  • Committer: joe
  • Date: 2002-10-08 21:36:18 UTC
  • Revision ID: svn-v4:a88caf3b-7e0a-0410-8d0d-cecb45342206:trunk:80
big config and comment update

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package org.crosswire.io;
2
 
 
3
 
import java.io.IOException;
4
 
import java.io.OutputStream;
5
 
import java.util.Vector;
6
 
 
7
 
import org.crosswire.util.Level;
8
 
import org.crosswire.util.Logger;
9
 
 
10
 
/**
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
13
 
 * of the streams.
14
 
 * @author Joe Walker
15
 
 */
16
 
public class TeeOutputStream extends OutputStream
17
 
{
18
 
    /**
19
 
     * Add the specified OutputStream to the list of streams.
20
 
     * @param out
21
 
     * @return "this". So we can do <pre>tee.add(a).add(b).add(c);</pre>
22
 
     */
23
 
    public TeeOutputStream add(OutputStream out)
24
 
    {
25
 
        if (!list.contains(out))
26
 
        {
27
 
            list.addElement(out);
28
 
        }
29
 
 
30
 
        return this;
31
 
    }
32
 
 
33
 
    /**
34
 
     * Remove the specified OutputStream from the list of streams
35
 
     * used in all outputs.
36
 
     * @param out The Stream to be removed
37
 
     */
38
 
    public boolean remove(OutputStream out)
39
 
    {
40
 
        return list.removeElement(out);
41
 
    }
42
 
 
43
 
    /**
44
 
     * Override to write to ass the listed Streams.
45
 
     * @param b The byte to be written, as normal.
46
 
     */
47
 
    public void write(int b) throws IOException
48
 
    {
49
 
        for (int i=0; i<list.size(); i++)
50
 
        {
51
 
            OutputStream out = (OutputStream) list.elementAt(i);
52
 
            out.write(b);
53
 
        }
54
 
    }
55
 
 
56
 
    /**
57
 
     * If someone closes the TeeOutputStream then we go round
58
 
     * and close all the Streams on the stack.
59
 
     */
60
 
    public void close() throws IOException
61
 
    {
62
 
        // Close each OutputStream catching and noting IOExceptions
63
 
        // Then rethrow at end if any failed.
64
 
        boolean failed = false;
65
 
 
66
 
        for (int i=0; i<list.size(); i++)
67
 
        {
68
 
            try
69
 
            {
70
 
                OutputStream out = (OutputStream) list.elementAt(i);
71
 
                out.close();
72
 
            }
73
 
            catch (Exception ex)
74
 
            {
75
 
                log.log(Level.INFO, "Error in closing loop", ex);
76
 
                failed = true;
77
 
            }
78
 
        }
79
 
 
80
 
        list.removeAllElements();
81
 
 
82
 
        if (failed) throw new IOException();
83
 
    }
84
 
 
85
 
    /**
86
 
     * @return The number of items on the stack
87
 
     */
88
 
    public int size()
89
 
    {
90
 
        return list.size();
91
 
    }
92
 
 
93
 
    /**
94
 
     * Primarily for debugging. Reports on th state of the Stream.
95
 
     * @return A String containing the report.
96
 
     */
97
 
    public String toString()
98
 
    {
99
 
        String retcode = "";
100
 
        String NEWLINE = System.getProperty("line.separator", "\r\n");
101
 
 
102
 
        retcode += "There are " + list.size() + " output(s)" + NEWLINE;
103
 
 
104
 
        for (int i=list.size()-1; i>=0; i--)
105
 
        {
106
 
            OutputStream out = (OutputStream) list.elementAt(i);
107
 
            retcode += "Stream" + i + ": " + out.toString() + NEWLINE;
108
 
        }
109
 
 
110
 
        return retcode;
111
 
    }
112
 
 
113
 
    private Vector list = new Vector();
114
 
 
115
 
    /** The log stream */
116
 
    protected static Logger log = Logger.getLogger(TeeOutputStream.class);
117
 
}