14fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy/* 24fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * Licensed to the Apache Software Foundation (ASF) under one or more 34fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * contributor license agreements. See the NOTICE file distributed with 44fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * this work for additional information regarding copyright ownership. 54fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * The ASF licenses this file to You under the Apache License, Version 2.0 64fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * (the "License"); you may not use this file except in compliance with 74fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * the License. You may obtain a copy of the License at 84fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * 94fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * http://www.apache.org/licenses/LICENSE-2.0 104fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * 114fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * Unless required by applicable law or agreed to in writing, software 124fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * distributed under the License is distributed on an "AS IS" BASIS, 134fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 144fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * See the License for the specific language governing permissions and 154fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * limitations under the License. 164fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy */ 174fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedypackage org.apache.commons.io.output; 184fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy 194fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedyimport java.io.IOException; 204fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedyimport java.io.OutputStream; 214fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy 224fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy/** 234fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * Classic splitter of OutputStream. Named after the unix 'tee' 244fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * command. It allows a stream to be branched off so there 254fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * are now two streams. 264fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * 274fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * @version $Id: TeeOutputStream.java 610010 2008-01-08 14:50:59Z niallp $ 284fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy */ 294fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedypublic class TeeOutputStream extends ProxyOutputStream { 304fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy 314fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy /** the second OutputStream to write to */ 324fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy protected OutputStream branch; 334fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy 344fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy /** 354fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * Constructs a TeeOutputStream. 364fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * @param out the main OutputStream 374fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * @param branch the second OutputStream 384fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy */ 394fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy public TeeOutputStream( OutputStream out, OutputStream branch ) { 404fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy super(out); 414fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy this.branch = branch; 424fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy } 434fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy 444fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy /** 454fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * Write the bytes to both streams. 464fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * @param b the bytes to write 474fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * @throws IOException if an I/O error occurs 484fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy */ 494fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy public synchronized void write(byte[] b) throws IOException { 504fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy super.write(b); 514fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy this.branch.write(b); 524fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy } 534fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy 544fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy /** 554fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * Write the specified bytes to both streams. 564fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * @param b the bytes to write 574fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * @param off The start offset 584fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * @param len The number of bytes to write 594fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * @throws IOException if an I/O error occurs 604fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy */ 614fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy public synchronized void write(byte[] b, int off, int len) throws IOException { 624fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy super.write(b, off, len); 634fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy this.branch.write(b, off, len); 644fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy } 654fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy 664fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy /** 674fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * Write a byte to both streams. 684fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * @param b the byte to write 694fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * @throws IOException if an I/O error occurs 704fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy */ 714fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy public synchronized void write(int b) throws IOException { 724fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy super.write(b); 734fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy this.branch.write(b); 744fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy } 754fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy 764fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy /** 774fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * Flushes both streams. 784fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * @throws IOException if an I/O error occurs 794fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy */ 804fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy public void flush() throws IOException { 814fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy super.flush(); 824fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy this.branch.flush(); 834fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy } 844fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy 854fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy /** 864fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * Closes both streams. 874fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * @throws IOException if an I/O error occurs 884fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy */ 894fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy public void close() throws IOException { 904fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy super.close(); 914fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy this.branch.close(); 924fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy } 934fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy 944fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy} 95