~sword-devel/jsword/trunk

« back to all changes in this revision

Viewing changes to jsword/java/historic/org/crosswire/io/TeeWriter.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.Writer;
5
 
import java.util.Vector;
6
 
 
7
 
import org.crosswire.util.Level;
8
 
import org.crosswire.util.Logger;
9
 
 
10
 
/**
11
 
 * TeeWriter 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 TeeWriter extends Writer
17
 
{
18
 
    /**
19
 
     * Add the specified Writer 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 TeeWriter add(Writer out)
24
 
    {
25
 
        if (!list.contains(out))
26
 
        {
27
 
            list.addElement(out);
28
 
        }
29
 
 
30
 
        return this;
31
 
    }
32
 
 
33
 
    /**
34
 
     * Remove the specified Writer from the list of streams
35
 
     * used in all outputs.
36
 
     * @param out The Stream to be removed
37
 
     */
38
 
    public boolean remove(Writer out)
39
 
    {
40
 
        return list.removeElement(out);
41
 
    }
42
 
 
43
 
    /**
44
 
     * Override write to ask the listed Streams.
45
 
     * @param b The byte to be written, as normal.
46
 
     */
47
 
    public void write(char[] cbuf, int off, int len) throws IOException
48
 
    {
49
 
        for (int i=0; i<list.size(); i++)
50
 
        {
51
 
            Writer out = (Writer) list.elementAt(i);
52
 
            out.write(cbuf, off, len);
53
 
        }
54
 
    }
55
 
 
56
 
    /**
57
 
     * Override write to ask the listed Streams.
58
 
     * @param b The byte to be written, as normal.
59
 
     */
60
 
    public void write(int b) throws IOException
61
 
    {
62
 
        for (int i=0; i<list.size(); i++)
63
 
        {
64
 
            Writer out = (Writer) list.elementAt(i);
65
 
            out.write(b);
66
 
        }
67
 
    }
68
 
 
69
 
    /**
70
 
     * Override flush to flush the listed Streams.
71
 
     */
72
 
    public void flush() throws IOException
73
 
    {
74
 
        for (int i=0; i<list.size(); i++)
75
 
        {
76
 
            Writer out = (Writer) list.elementAt(i);
77
 
            out.flush();
78
 
        }
79
 
    }
80
 
 
81
 
    /**
82
 
     * If someone closes the TeeWriter then we go round
83
 
     * and close all the Streams on the stack.
84
 
     */
85
 
    public void close() throws IOException
86
 
    {
87
 
        // Close each Writer catching and noting IOExceptions
88
 
        // Then rethrow at end if any failed.
89
 
        boolean failed = false;
90
 
 
91
 
        for (int i=0; i<list.size(); i++)
92
 
        {
93
 
            try
94
 
            {
95
 
                Writer out = (Writer) list.elementAt(i);
96
 
                out.close();
97
 
            }
98
 
            catch (Exception ex)
99
 
            {
100
 
                log.log(Level.INFO, "Error in closing loop", ex);
101
 
                failed = true;
102
 
            }
103
 
        }
104
 
 
105
 
        list.removeAllElements();
106
 
 
107
 
        if (failed) throw new IOException();
108
 
    }
109
 
 
110
 
    /**
111
 
     * @return The number of items on the stack
112
 
     */
113
 
    public int size()
114
 
    {
115
 
        return list.size();
116
 
    }
117
 
 
118
 
    /**
119
 
     * Primarily for debugging. Reports on th state of the Stream.
120
 
     * @return A String containing the report.
121
 
     */
122
 
    public String toString()
123
 
    {
124
 
        String retcode = "";
125
 
        String NEWLINE = System.getProperty("line.separator", "\r\n");
126
 
 
127
 
        retcode += "There are " + list.size() + " output(s)" + NEWLINE;
128
 
 
129
 
        for (int i=list.size()-1; i>=0; i--)
130
 
        {
131
 
            Writer out = (Writer) list.elementAt(i);
132
 
            retcode += "Stream" + i + ": " + out.toString() + NEWLINE;
133
 
        }
134
 
 
135
 
        return retcode;
136
 
    }
137
 
 
138
 
    private Vector list = new Vector();
139
 
 
140
 
    /** The log stream */
141
 
    protected static Logger log = Logger.getLogger(TeeWriter.class);
142
 
}