1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.io.output;
19
20 import java.io.FilterWriter;
21 import java.io.IOException;
22 import java.io.Writer;
23 import java.util.Arrays;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.Objects;
27 import java.util.stream.Stream;
28
29 import org.apache.commons.io.IOExceptionList;
30 import org.apache.commons.io.IOIndexedException;
31 import org.apache.commons.io.function.IOConsumer;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 public class FilterCollectionWriter extends Writer {
48
49
50
51
52 protected final Collection<Writer> EMPTY_WRITERS = Collections.emptyList();
53
54
55
56
57 protected final Collection<Writer> writers;
58
59
60
61
62
63
64 protected FilterCollectionWriter(final Collection<Writer> writers) {
65 this.writers = writers == null ? EMPTY_WRITERS : writers;
66 }
67
68
69
70
71
72
73 protected FilterCollectionWriter(final Writer... writers) {
74 this.writers = writers == null ? EMPTY_WRITERS : Arrays.asList(writers);
75 }
76
77 @Override
78 public Writer append(final char c) throws IOException {
79 return forAllWriters(w -> w.append(c));
80 }
81
82 @Override
83 public Writer append(final CharSequence csq) throws IOException {
84 return forAllWriters(w -> w.append(csq));
85 }
86
87 @Override
88 public Writer append(final CharSequence csq, final int start, final int end) throws IOException {
89 return forAllWriters(w -> w.append(csq, start, end));
90 }
91
92 @SuppressWarnings("resource")
93 @Override
94 public void close() throws IOException {
95 forAllWriters(Writer::close);
96 }
97
98
99
100
101
102
103 @SuppressWarnings("resource")
104 @Override
105 public void flush() throws IOException {
106 forAllWriters(Writer::flush);
107 }
108
109 private FilterCollectionWriter forAllWriters(final IOConsumer<Writer> action) throws IOExceptionList {
110 IOConsumer.forAll(action, writers());
111 return this;
112 }
113
114 @SuppressWarnings("resource")
115 @Override
116 public void write(final char[] cbuf) throws IOException {
117 forAllWriters(w -> w.write(cbuf));
118 }
119
120
121
122
123
124
125
126
127
128 @SuppressWarnings("resource")
129 @Override
130 public void write(final char[] cbuf, final int off, final int len) throws IOException {
131 forAllWriters(w -> w.write(cbuf, off, len));
132 }
133
134
135
136
137
138
139 @SuppressWarnings("resource")
140 @Override
141 public void write(final int c) throws IOException {
142 forAllWriters(w -> w.write(c));
143 }
144
145 @SuppressWarnings("resource")
146 @Override
147 public void write(final String str) throws IOException {
148 forAllWriters(w -> w.write(str));
149 }
150
151
152
153
154
155
156
157
158
159 @SuppressWarnings("resource")
160 @Override
161 public void write(final String str, final int off, final int len) throws IOException {
162 forAllWriters(w -> w.write(str, off, len));
163 }
164
165 private Stream<Writer> writers() {
166 return writers.stream().filter(Objects::nonNull);
167 }
168
169 }