001package runwar.util; 002import java.io.OutputStream; 003import java.io.IOException; 004 005/** 006 * An OutputStream that sends bytes written to it to multiple output streams 007 * in much the same way as the UNIX 'tee' command. 008 * 009 * @author Sabre150 010 */ 011public class TeeOutputStream extends OutputStream 012{ 013 /** 014 * Constructs from a varags set of output streams 015 * 016 * @param ostream... the varags array of OutputStreams 017 */ 018 public TeeOutputStream(OutputStream... ostream) 019 { 020 ostream_ = ostream; 021 } 022 023 /** 024 * Writes a byte to both output streams 025 * 026 * @param b the byte to write 027 * @throws IOException from any of the OutputStreams 028 */ 029 @Override 030 public void write(int b) throws IOException 031 { 032 for (OutputStream ostream : ostream_) 033 { 034 ostream.write(b); 035 } 036 } 037 038 /** 039 * Writes an array of bytes to all OutputStreams 040 * 041 * @param b the bytes to write 042 * @param off the offset to start writing from 043 * @param len the number of bytes to write 044 * @throws IOException from any of the OutputStreams 045 */ 046 @Override 047 public void write(byte[] b, int off, int len) throws IOException 048 { 049 for (OutputStream ostream : ostream_) 050 { 051 ostream.write(b, off, len); 052 } 053 } 054 055 @Override 056 public void flush() throws IOException 057 { 058 for (OutputStream ostream : ostream_) 059 { 060 ostream.flush(); 061 } 062 } 063 064 @Override 065 public void close() throws IOException 066 { 067 for (OutputStream ostream : ostream_) 068 { 069 ostream.flush(); 070 ostream.close(); 071 } 072 } 073 // Obvious 074 private final OutputStream[] ostream_; 075}