001    /*--------------------------------------------------------------------------+
002    $Id: MultiplexedOutputStream.java 26283 2010-02-18 11:18:57Z juergens $
003    |                                                                          |
004    | Copyright 2005-2010 Technische Universitaet Muenchen                     |
005    |                                                                          |
006    | Licensed under the Apache License, Version 2.0 (the "License");          |
007    | you may not use this file except in compliance with the License.         |
008    | You may obtain a copy of the License at                                  |
009    |                                                                          |
010    |    http://www.apache.org/licenses/LICENSE-2.0                            |
011    |                                                                          |
012    | Unless required by applicable law or agreed to in writing, software      |
013    | distributed under the License is distributed on an "AS IS" BASIS,        |
014    | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
015    | See the License for the specific language governing permissions and      |
016    | limitations under the License.                                           |
017    +--------------------------------------------------------------------------*/
018    package edu.tum.cs.commons.io;
019    
020    import java.io.IOException;
021    import java.io.OutputStream;
022    
023    /**
024     * This class enables multiplexing of output streams. It can be e.g. used to
025     * output content to multiple files.
026     * 
027     * 
028     * @author Florian Deissenboeck
029     * @author $Author: juergens $
030     * @version $Rev: 26283 $
031     * @levd.rating GREEN Hash: 5DB2776C03CD4F1EF5CE43768985E2BD
032     */
033    public class MultiplexedOutputStream extends OutputStream {
034    
035        /** The underlying output streams. */
036        private final OutputStream[] streams;
037    
038        /**
039         * Create new multiplexed output streams.
040         * 
041         * @param streams
042         *            any number of output streams.
043         */
044        public MultiplexedOutputStream(OutputStream... streams) {
045            this.streams = streams;
046        }
047    
048        /**
049         * Forwards close operation to all underlying output streams.
050         */
051        @Override
052        public void close() throws IOException {
053            for (OutputStream stream : streams) {
054                stream.close();
055            }
056        }
057    
058        /**
059         * Forwards flush operation to all underlying output streams.
060         */
061        @Override
062        public void flush() throws IOException {
063            for (OutputStream stream : streams) {
064                stream.flush();
065            }
066        }
067    
068        /**
069         * Forwards write operation to all underlying output streams.
070         */
071        @Override
072            public void write(int b) throws IOException {
073            for (OutputStream stream : streams) {
074                stream.write(b);
075            }
076    
077        }
078    
079    }