X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Ftodesbaum%2Futil%2Fio%2FTeeOutputStream.java;fp=src%2Fmain%2Fjava%2Fde%2Ftodesbaum%2Futil%2Fio%2FTeeOutputStream.java;h=78860dc7918fd3a11e92ce65038d44eef9d1f475;hb=38bdc433e50669e8244a63b5af59e597f88f1d29;hp=0000000000000000000000000000000000000000;hpb=f14b9fbe6d88e23920b10a75ebeba4d38390301b;p=jSite.git diff --git a/src/main/java/de/todesbaum/util/io/TeeOutputStream.java b/src/main/java/de/todesbaum/util/io/TeeOutputStream.java new file mode 100644 index 0000000..78860dc --- /dev/null +++ b/src/main/java/de/todesbaum/util/io/TeeOutputStream.java @@ -0,0 +1,125 @@ +/* + * jSite - TeeOutputStream.java - Copyright © 2010 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package de.todesbaum.util.io; + +import java.io.IOException; +import java.io.OutputStream; + +/** + * {@link OutputStream} that sends all data it receives to multiple other output + * streams. If an error occurs during a {@link #write(int)} to one of the + * underlying output streams no guarantees are made about how much data is sent + * to each stream, i.e. there is no good way to recover from such an error. + * + * @author David ‘Bombe’ Roden + */ +public class TeeOutputStream extends OutputStream { + + /** The output streams. */ + private final OutputStream[] outputStreams; + + /** + * Creates a new tee output stream that sends all to all given output + * streams. + * + * @param outputStreams + * The output streams to send all data to + */ + public TeeOutputStream(OutputStream... outputStreams) { + this.outputStreams = outputStreams; + } + + /** + * {@inheritDoc} + *

+ * An effort is made to close all output streams. If multiple exceptions + * occur, only the first exception is thrown after all output streams have + * been tried to close. + */ + @Override + public void close() throws IOException { + IOException occuredException = null; + for (OutputStream outputStream : outputStreams) { + try { + outputStream.flush(); + } catch (IOException ioe1) { + if (occuredException == null) { + occuredException = ioe1; + } + } + } + if (occuredException != null) { + throw occuredException; + } + } + + /** + * {@inheritDoc} + *

+ * An effort is made to flush all output streams. If multiple exceptions + * occur, only the first exception is thrown after all output streams have + * been tried to flush. + */ + @Override + public void flush() throws IOException { + IOException occuredException = null; + for (OutputStream outputStream : outputStreams) { + try { + outputStream.flush(); + } catch (IOException ioe1) { + if (occuredException == null) { + occuredException = ioe1; + } + } + } + if (occuredException != null) { + throw occuredException; + } + } + + /** + * {@inheritDoc} + */ + @Override + public void write(byte[] buffer) throws IOException { + for (OutputStream outputStream : outputStreams) { + outputStream.write(buffer); + } + } + + /** + * {@inheritDoc} + */ + @Override + public void write(byte[] buffer, int offset, int length) throws IOException { + for (OutputStream outputStream : outputStreams) { + outputStream.write(buffer, offset, length); + } + } + + /** + * {@inheritDoc} + */ + @Override + public void write(int data) throws IOException { + for (OutputStream outputStream : outputStreams) { + outputStream.write(data); + } + } + +}