1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package eu.simuline.octave.util;
20
21 import java.io.IOException;
22 import java.io.Writer;
23
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.commons.logging.Log;
26
27
28
29
30
31
32
33
34
35
36
37 public final class TeeWriter extends Writer {
38
39 private static final Log LOG = LogFactory.getLog(TeeWriter.class);
40
41 private final Writer[] writers;
42
43
44
45
46 public TeeWriter() {
47
48 this(new Writer[0]);
49 }
50
51
52
53
54
55
56
57 public TeeWriter(final Writer... writers) {
58 this.writers = writers;
59 }
60
61 @Override
62 public void write(final char[] cbuf, final int off, final int len)
63 throws IOException {
64
65 IOException ioe = null;
66 for (final Writer writer : writers) {
67 try {
68 writer.write(cbuf, off, len);
69 } catch (final IOException e) {
70 LOG.debug("Exception during write()", e);
71 ioe = e;
72 }
73 }
74 if (ioe != null) {
75 throw ioe;
76 }
77 }
78
79 @Override
80 public void flush() throws IOException {
81 IOException ioe = null;
82 for (final Writer writer : writers) {
83 try {
84 writer.flush();
85 } catch (final IOException e) {
86 LOG.debug("Exception during flush()", e);
87 ioe = e;
88 }
89 }
90 if (ioe != null) {
91 throw ioe;
92 }
93 }
94
95 @Override
96 public void close() throws IOException {
97 IOException ioe = null;
98 for (final Writer writer : writers) {
99 try {
100 writer.close();
101 } catch (final IOException e) {
102 LOG.debug("Exception during close()", e);
103 ioe = e;
104 }
105 }
106 if (ioe != null) {
107 throw ioe;
108 }
109 }
110
111 }