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.servicemix.jbi.util;
018
019 import java.io.IOException;
020 import java.io.OutputStream;
021 import java.util.Iterator;
022 import java.util.List;
023 import java.util.concurrent.CopyOnWriteArrayList;
024
025 /**
026 * Write to multiple OutputStreams
027 *
028 * @version $Revision: 564607 $
029 */
030 public class MultiplexOutputStream extends OutputStream {
031
032 List streams = new CopyOnWriteArrayList();
033
034
035 /**
036 * Add an Output Stream
037 * @param os
038 */
039 public void add(OutputStream os) {
040 streams.add(os);
041 }
042
043 /**
044 * Remove an OutputStream
045 * @param os
046 */
047 public void remove(OutputStream os) {
048 streams.remove(os);
049 }
050
051 /**
052 * write a byte
053 * @param b
054 * @throws IOException
055 */
056 public synchronized void write(int b) throws IOException {
057 for (Iterator i = streams.iterator(); i.hasNext();) {
058 OutputStream s = (OutputStream) i.next();
059 s.write(b);
060 }
061 }
062
063 /**
064 * write an array
065 * @param b
066 * @param off
067 * @param len
068 * @throws IOException
069 */
070 public synchronized void write(byte b[], int off, int len) throws IOException {
071 for (Iterator i = streams.iterator(); i.hasNext();) {
072 OutputStream s = (OutputStream) i.next();
073 s.write(b, off, len);
074 }
075 }
076
077 /**
078 * flush
079 * @throws IOException
080 */
081 public void flush() throws IOException {
082 for (Iterator i = streams.iterator(); i.hasNext();) {
083 OutputStream s = (OutputStream) i.next();
084 s.flush();
085 }
086 }
087
088 /**
089 * close
090 * @throws IOException
091 */
092 public void close() throws IOException {
093 for (Iterator i = streams.iterator(); i.hasNext();) {
094 OutputStream s = (OutputStream) i.next();
095 s.close();
096 }
097 streams.clear();
098 }
099 }