001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.commons.io.output; 018 019 import java.io.IOException; 020 import java.io.OutputStream; 021 022 /** 023 * Classic splitter of OutputStream. Named after the unix 'tee' 024 * command. It allows a stream to be branched off so there 025 * are now two streams. 026 * 027 * @version $Id: TeeOutputStream.java 610010 2008-01-08 14:50:59Z niallp $ 028 */ 029 public class TeeOutputStream extends ProxyOutputStream { 030 031 /** the second OutputStream to write to */ 032 protected OutputStream branch; 033 034 /** 035 * Constructs a TeeOutputStream. 036 * @param out the main OutputStream 037 * @param branch the second OutputStream 038 */ 039 public TeeOutputStream( OutputStream out, OutputStream branch ) { 040 super(out); 041 this.branch = branch; 042 } 043 044 /** 045 * Write the bytes to both streams. 046 * @param b the bytes to write 047 * @throws IOException if an I/O error occurs 048 */ 049 public synchronized void write(byte[] b) throws IOException { 050 super.write(b); 051 this.branch.write(b); 052 } 053 054 /** 055 * Write the specified bytes to both streams. 056 * @param b the bytes to write 057 * @param off The start offset 058 * @param len The number of bytes to write 059 * @throws IOException if an I/O error occurs 060 */ 061 public synchronized void write(byte[] b, int off, int len) throws IOException { 062 super.write(b, off, len); 063 this.branch.write(b, off, len); 064 } 065 066 /** 067 * Write a byte to both streams. 068 * @param b the byte to write 069 * @throws IOException if an I/O error occurs 070 */ 071 public synchronized void write(int b) throws IOException { 072 super.write(b); 073 this.branch.write(b); 074 } 075 076 /** 077 * Flushes both streams. 078 * @throws IOException if an I/O error occurs 079 */ 080 public void flush() throws IOException { 081 super.flush(); 082 this.branch.flush(); 083 } 084 085 /** 086 * Closes both streams. 087 * @throws IOException if an I/O error occurs 088 */ 089 public void close() throws IOException { 090 super.close(); 091 this.branch.close(); 092 } 093 094 }