From a8ac16b1838de0cc381d073f4b8ad2bffe20a04f Mon Sep 17 00:00:00 2001 From: Ben Culkin Date: Fri, 25 Sep 2020 18:48:50 -0400 Subject: Add MirrorOutputStream This is an output stream that mirrors what is written to it to all of its composite output streams. --- .../java/bjc/utils/ioutils/MirrorOutputStream.java | 63 ++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 base/src/main/java/bjc/utils/ioutils/MirrorOutputStream.java diff --git a/base/src/main/java/bjc/utils/ioutils/MirrorOutputStream.java b/base/src/main/java/bjc/utils/ioutils/MirrorOutputStream.java new file mode 100644 index 0000000..6906201 --- /dev/null +++ b/base/src/main/java/bjc/utils/ioutils/MirrorOutputStream.java @@ -0,0 +1,63 @@ +package bjc.utils.ioutils; + +import java.io.*; +import java.util.*; + +/** + * An output stream that mirrors its contents to other streams. + * + * @author Ben Culkin + * + */ +public class MirrorOutputStream extends OutputStream { + private List streams; + + /** + * Create a new mirroring output stream. + * + * @param strams + * The output streams to mirror to. + */ + public MirrorOutputStream(OutputStream... strams) { + streams = new ArrayList<>(); + + for (OutputStream stram : strams) { + streams.add(stram); + } + } + + @Override + public void close() throws IOException { + for (OutputStream stream : streams) { + stream.close(); + } + } + + @Override + public void flush() throws IOException { + for (OutputStream stream : streams) { + stream.flush(); + } + } + + @Override + public void write(byte[] ba) throws IOException { + for (OutputStream stream : streams) { + stream.write(ba); + } + } + + @Override + public void write(byte[] ba, int off, int len) throws IOException { + for (OutputStream stream : streams) { + stream.write(ba, off, len); + } + } + + @Override + public void write(int b) throws IOException { + for (OutputStream stream : streams) { + stream.write(b); + } + } +} -- cgit v1.2.3