diff options
| author | Ben Culkin <scorpress@gmail.com> | 2020-09-25 18:48:50 -0400 |
|---|---|---|
| committer | Ben Culkin <scorpress@gmail.com> | 2020-09-25 18:48:50 -0400 |
| commit | a8ac16b1838de0cc381d073f4b8ad2bffe20a04f (patch) | |
| tree | a5cb9dd6c22208b3eac4815420857e25476b92c1 /base/src/main/java | |
| parent | 8cda0679cd5f1d72158249224689f57903ba4bd2 (diff) | |
Add MirrorOutputStream
This is an output stream that mirrors what is written to it to all of
its composite output streams.
Diffstat (limited to 'base/src/main/java')
| -rw-r--r-- | base/src/main/java/bjc/utils/ioutils/MirrorOutputStream.java | 63 |
1 files changed, 63 insertions, 0 deletions
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<OutputStream> 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); + } + } +} |
