001    // Copyright 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry.util.io;
016    
017    import java.io.IOException;
018    import java.io.OutputStream;
019    
020    import org.apache.hivemind.util.Defense;
021    
022    /**
023     * An output stream that copies bytes pushed through it to two other output
024     * streams.
025     * 
026     * @author Howard M. Lewis Ship
027     * @since 4.0
028     */
029    public class TeeOutputStream extends OutputStream
030    {
031    
032        private final OutputStream _os1;
033    
034        private final OutputStream _os2;
035    
036        public TeeOutputStream(OutputStream os1, OutputStream os2)
037        {
038            Defense.notNull(os1, "os1");
039            Defense.notNull(os2, "os2");
040    
041            _os1 = os1;
042            _os2 = os2;
043        }
044    
045        public void close()
046            throws IOException
047        {
048            _os1.close();
049            _os2.close();
050        }
051    
052        public void flush()
053            throws IOException
054        {
055            _os1.flush();
056            _os2.flush();
057        }
058    
059        public void write(byte[] b, int off, int len)
060            throws IOException
061        {
062            _os1.write(b, off, len);
063            _os2.write(b, off, len);
064        }
065    
066        public void write(byte[] b)
067            throws IOException
068        {
069            _os1.write(b);
070            _os1.write(b);
071        }
072    
073        public void write(int b)
074            throws IOException
075        {
076            _os1.write(b);
077            _os2.write(b);
078        }
079    }