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 {@link OutputStream}. Named after the Unix 'tee' command. It allows a stream to be branched off 024 * so there are now two streams. 025 */ 026public class TeeOutputStream extends ProxyOutputStream { 027 028 /** 029 * The second OutputStream to write to. 030 * 031 * TODO Make private and final in 3.0. 032 */ 033 protected OutputStream branch; 034 035 /** 036 * Constructs a TeeOutputStream. 037 * 038 * @param out the main OutputStream 039 * @param branch the second OutputStream 040 */ 041 public TeeOutputStream(final OutputStream out, final OutputStream branch) { 042 super(out); 043 this.branch = branch; 044 } 045 046 /** 047 * Writes the bytes to both streams. 048 * 049 * @param b the bytes to write 050 * @throws IOException if an I/O error occurs. 051 */ 052 @Override 053 public synchronized void write(final byte[] b) throws IOException { 054 super.write(b); 055 this.branch.write(b); 056 } 057 058 /** 059 * Writes the specified bytes to both streams. 060 * 061 * @param b the bytes to write 062 * @param off The start offset 063 * @param len The number of bytes to write 064 * @throws IOException if an I/O error occurs. 065 */ 066 @Override 067 public synchronized void write(final byte[] b, final int off, final int len) throws IOException { 068 super.write(b, off, len); 069 this.branch.write(b, off, len); 070 } 071 072 /** 073 * Writes a byte to both streams. 074 * 075 * @param b the byte to write 076 * @throws IOException if an I/O error occurs. 077 */ 078 @Override 079 public synchronized void write(final int b) throws IOException { 080 super.write(b); 081 this.branch.write(b); 082 } 083 084 /** 085 * Flushes both streams. 086 * 087 * @throws IOException if an I/O error occurs. 088 */ 089 @Override 090 public void flush() throws IOException { 091 super.flush(); 092 this.branch.flush(); 093 } 094 095 /** 096 * Closes both output streams. 097 * <p> 098 * If closing the main output stream throws an exception, attempt to close the branch output stream. 099 * </p> 100 * 101 * <p> 102 * If closing the main and branch output streams both throw exceptions, which exceptions is thrown by this method is 103 * currently unspecified and subject to change. 104 * </p> 105 * 106 * @throws IOException if an I/O error occurs. 107 */ 108 @Override 109 public void close() throws IOException { 110 try { 111 super.close(); 112 } finally { 113 this.branch.close(); 114 } 115 } 116 117}