Skip to content

Commit 3d04900

Browse files
maarztctrueden
authored andcommitted
MultiOutputStream: use CopyOnWriteArrayList to simplify code
1 parent f7b4e30 commit 3d04900

File tree

1 file changed

+10
-30
lines changed

1 file changed

+10
-30
lines changed

src/main/java/org/scijava/console/MultiOutputStream.java

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
import java.io.IOException;
3636
import java.io.OutputStream;
3737
import java.util.ArrayList;
38+
import java.util.List;
39+
import java.util.concurrent.CopyOnWriteArrayList;
3840

3941
/**
4042
* A {@code MultiOutputStream} is a collection of constituent
@@ -49,81 +51,59 @@
4951
*/
5052
public class MultiOutputStream extends OutputStream {
5153

52-
private final ArrayList<OutputStream> streams;
53-
54-
private OutputStream[] cachedStreams;
54+
private final List<OutputStream> streams;
5555

5656
/**
5757
* Forwards output to a list of output streams.
5858
*
5959
* @param os Output streams which will receive this stream's output.
6060
*/
6161
public MultiOutputStream(final OutputStream... os) {
62-
streams = new ArrayList<>(os.length);
63-
for (int i = 0; i < os.length; i++) {
64-
streams.add(os[i]);
65-
}
66-
cacheStreams();
62+
streams = new CopyOnWriteArrayList<>(os);
6763
}
6864

6965
// -- MultiOutputStream methods --
7066

7167
/** Adds an output stream to those receiving this stream's output. */
7268
public void addOutputStream(final OutputStream os) {
73-
synchronized (streams) {
74-
streams.add(os);
75-
cacheStreams();
76-
}
69+
streams.add(os);
7770
}
7871

7972
/** Removes an output stream from those receiving this stream's output. */
8073
public void removeOutputStream(final OutputStream os) {
81-
synchronized (streams) {
82-
streams.remove(os);
83-
cacheStreams();
84-
}
74+
streams.remove(os);
8575
}
8676

8777
// -- OutputStream methods --
8878

8979
@Override
9080
public void write(final int b) throws IOException {
91-
final OutputStream[] toWrite = cachedStreams;
92-
for (final OutputStream stream : toWrite)
81+
for (final OutputStream stream : streams)
9382
stream.write(b);
9483
}
9584

9685
@Override
9786
public void write(final byte[] buf, final int off, final int len)
9887
throws IOException
9988
{
100-
final OutputStream[] toWrite = cachedStreams;
101-
for (final OutputStream stream : toWrite)
89+
for (final OutputStream stream : streams)
10290
stream.write(buf, off, len);
10391
}
10492

10593
// -- Closeable methods --
10694

10795
@Override
10896
public void close() throws IOException {
109-
final OutputStream[] toClose = cachedStreams;
110-
for (final OutputStream stream : toClose)
97+
for (final OutputStream stream : streams)
11198
stream.close();
11299
}
113100

114101
// -- Flushable methods --
115102

116103
@Override
117104
public void flush() throws IOException {
118-
final OutputStream[] toFlush = cachedStreams;
119-
for (final OutputStream stream : toFlush)
105+
for (final OutputStream stream : streams)
120106
stream.flush();
121107
}
122108

123-
// -- Helper methods --
124-
125-
private void cacheStreams() {
126-
cachedStreams = streams.toArray(new OutputStream[streams.size()]);
127-
}
128-
129109
}

0 commit comments

Comments
 (0)