/*
 * #%L
 * SciJava Common shared library for SciJava software.
 * %%
 * Copyright (C) 2009 - 2017 Board of Regents of the University of
 * Wisconsin-Madison, Broad Institute of MIT and Harvard, Max Planck
 * Institute of Molecular Cell Biology and Genetics, University of
 * Konstanz, and KNIME GmbH.
 * %%
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 * #L%
 */

package org.scijava.console;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;

/**
 * A {@code MultiOutputStream} is a collection of constituent
 * {@link OutputStream} objects, to which all output is forwarded.
 * <p>
 * Thanks to Ian F. Darwin for <a href=
 * "http://www.java2s.com/Code/Java/File-Input-Output/TeePrintStreamteesallPrintStreamoperationsintoafileratherliketheUNIXtee1command.htm"
 * >his implementation of a similar concept</a>.
 * </p>
 *
 * @author Curtis Rueden
 */
public class MultiOutputStream extends OutputStream {

	private final ArrayList<OutputStream> streams;

	private OutputStream[] cachedStreams;

	/**
	 * Forwards output to a list of output streams.
	 *
	 * @param os Output streams which will receive this stream's output.
	 */
	public MultiOutputStream(final OutputStream... os) {
		streams = new ArrayList<>(os.length);
		for (int i = 0; i < os.length; i++) {
			streams.add(os[i]);
		}
		cacheStreams();
	}

	// -- MultiOutputStream methods --

	/** Adds an output stream to those receiving this stream's output. */
	public void addOutputStream(final OutputStream os) {
		synchronized (streams) {
			streams.add(os);
			cacheStreams();
		}
	}

	/** Removes an output stream from those receiving this stream's output. */
	public void removeOutputStream(final OutputStream os) {
		synchronized (streams) {
			streams.remove(os);
			cacheStreams();
		}
	}

	// -- OutputStream methods --

	@Override
	public void write(final int b) throws IOException {
		final OutputStream[] toWrite = cachedStreams;
		for (final OutputStream stream : toWrite)
			stream.write(b);
	}

	@Override
	public void write(final byte[] buf, final int off, final int len)
		throws IOException
	{
		final OutputStream[] toWrite = cachedStreams;
		for (final OutputStream stream : toWrite)
			stream.write(buf, off, len);
	}

	// -- Closeable methods --

	@Override
	public void close() throws IOException {
		final OutputStream[] toClose = cachedStreams;
		for (final OutputStream stream : toClose)
			stream.close();
	}

	// -- Flushable methods --

	@Override
	public void flush() throws IOException {
		final OutputStream[] toFlush = cachedStreams;
		for (final OutputStream stream : toFlush)
			stream.flush();
	}

	// -- Helper methods --

	private void cacheStreams() {
		cachedStreams = streams.toArray(new OutputStream[streams.size()]);
	}

}

@@ -1733,16 +1733,89 @@
 rayList;
+%0Aimport java.util.List;%0Aimport java.util.concurrent.CopyOnWriteArrayList;
 %0A%0A/**%0A *
@@ -2286,21 +2286,16 @@
 e final 
-Array
 List%3COut
@@ -2319,48 +2319,8 @@
 s;%0A%0A
-%09private OutputStream%5B%5D cachedStreams;%0A%0A
 %09/**
@@ -2518,16 +2518,27 @@
 s = new 
+CopyOnWrite
 ArrayLis
@@ -2547,100 +2547,8 @@
 %3E(os
-.length);%0A%09%09for (int i = 0; i %3C os.length; i++) %7B%0A%09%09%09streams.add(os%5Bi%5D);%0A%09%09%7D%0A%09%09cacheStreams(
 );%0A%09
@@ -2719,74 +2719,23 @@
 %0A%09%09s
-ynchronized (streams) %7B%0A%09%09%09streams.add(os);%0A%09%09%09cacheStreams();%0A%09%09%7D
+treams.add(os);
 %0A%09%7D%0A
@@ -2875,77 +2875,26 @@
 %0A%09%09s
-ynchronized (streams) %7B%0A%09%09%09streams.remove(os);%0A%09%09%09cacheStreams();%0A%09%09%7D
+treams.remove(os);
 %0A%09%7D%0A
@@ -2994,56 +2994,8 @@
 n %7B%0A
-%09%09final OutputStream%5B%5D toWrite = cachedStreams;%0A
 %09%09fo
@@ -3017,39 +3017,39 @@
 Stream stream : 
-toWrite
+streams
 )%0A%09%09%09stream.writ
@@ -3164,56 +3164,8 @@
 %0A%09%7B%0A
-%09%09final OutputStream%5B%5D toWrite = cachedStreams;%0A
 %09%09fo
@@ -3199,15 +3199,15 @@
 m : 
-toWrite
+streams
 )%0A%09%09
@@ -3326,56 +3326,8 @@
 n %7B%0A
-%09%09final OutputStream%5B%5D toClose = cachedStreams;%0A
 %09%09fo
@@ -3361,15 +3361,15 @@
 m : 
-toClose
+streams
 )%0A%09%09
@@ -3478,243 +3478,65 @@
 %0A%09%09f
-inal OutputStream%5B%5D toFlush = cachedStreams;%0A%09%09for (final OutputStream stream : toFlush)%0A%09%09%09stream.flush();%0A%09%7D%0A%0A%09// -- Helper methods --%0A%0A%09private void cacheStreams() %7B%0A%09%09cachedStreams = streams.toArray(new OutputStream%5Bstreams.size()%5D
+or (final OutputStream stream : streams)%0A%09%09%09stream.flush(
 );%0A%09

use CopyOnWriteArrayList to simplify code