KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > util > MultiplexOutputStream


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.jbi.util;
18
19 import edu.emory.mathcs.backport.java.util.concurrent.CopyOnWriteArrayList;
20
21 import java.io.IOException JavaDoc;
22 import java.io.OutputStream JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25
26 /**
27  * Write to multiple OutputStreams
28  *
29  * @version $Revision: 426415 $
30  */

31 public class MultiplexOutputStream extends OutputStream JavaDoc {
32     List streams = new CopyOnWriteArrayList();
33     
34     
35     /**
36      * Add an Output Stream
37      * @param os
38      */

39     public void add(OutputStream JavaDoc os){
40         streams.add(os);
41     }
42     
43     /**
44      * Remove an OutputStream
45      * @param os
46      */

47     public void remove(OutputStream JavaDoc os){
48         streams.remove(os);
49     }
50
51     /**
52      * write a byte
53      * @param b
54      * @throws IOException
55      */

56     public synchronized void write(int b) throws IOException JavaDoc {
57         for (Iterator JavaDoc i = streams.iterator();i.hasNext();) {
58             OutputStream JavaDoc s = (OutputStream JavaDoc) i.next();
59             s.write(b);
60         }
61     }
62
63     /**
64      * write an array
65      * @param b
66      * @param off
67      * @param len
68      * @throws IOException
69      */

70     public synchronized void write(byte b[], int off, int len) throws IOException JavaDoc {
71         for (Iterator JavaDoc i = streams.iterator();i.hasNext();) {
72             OutputStream JavaDoc s = (OutputStream JavaDoc) i.next();
73             s.write(b, off, len);
74         }
75     }
76
77     /**
78      * flush
79      * @throws IOException
80      */

81     public void flush() throws IOException JavaDoc {
82         for (Iterator JavaDoc i = streams.iterator();i.hasNext();) {
83             OutputStream JavaDoc s = (OutputStream JavaDoc) i.next();
84             s.flush();
85         }
86     }
87
88     /**
89      * close
90      * @throws IOException
91      */

92     public void close() throws IOException JavaDoc {
93         for (Iterator JavaDoc i = streams.iterator();i.hasNext();) {
94             OutputStream JavaDoc s = (OutputStream JavaDoc) i.next();
95             s.close();
96         }
97         streams.clear();
98     }
99 }
100
Popular Tags