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 */ 017package org.apache.commons.io.output; 018 019import java.io.IOException; 020import 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 */ 028public class TeeOutputStream extends ProxyOutputStream { 029 030 /** the second OutputStream to write to */ 031 protected OutputStream branch; //TODO consider making this private 032 033 /** 034 * Constructs a TeeOutputStream. 035 * @param out the main OutputStream 036 * @param branch the second OutputStream 037 */ 038 public TeeOutputStream(final OutputStream out, final OutputStream branch) { 039 super(out); 040 this.branch = branch; 041 } 042 043 /** 044 * Write the bytes to both streams. 045 * @param b the bytes to write 046 * @throws IOException if an I/O error occurs 047 */ 048 @Override 049 public synchronized void write(final 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 @Override 062 public synchronized void write(final byte[] b, final int off, final int len) throws IOException { 063 super.write(b, off, len); 064 this.branch.write(b, off, len); 065 } 066 067 /** 068 * Write a byte to both streams. 069 * @param b the byte to write 070 * @throws IOException if an I/O error occurs 071 */ 072 @Override 073 public synchronized void write(final int b) throws IOException { 074 super.write(b); 075 this.branch.write(b); 076 } 077 078 /** 079 * Flushes both streams. 080 * @throws IOException if an I/O error occurs 081 */ 082 @Override 083 public void flush() throws IOException { 084 super.flush(); 085 this.branch.flush(); 086 } 087 088 /** 089 * Closes both output streams. 090 * 091 * If closing the main output stream throws an exception, attempt to close the branch output stream. 092 * 093 * If closing the main and branch output streams both throw exceptions, which exceptions is thrown by this method is 094 * currently unspecified and subject to change. 095 * 096 * @throws IOException 097 * if an I/O error occurs 098 */ 099 @Override 100 public void close() throws IOException { 101 try { 102 super.close(); 103 } finally { 104 this.branch.close(); 105 } 106 } 107 108}