KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lutris > util > OutputStreamHub


1
2 /*
3  * Enhydra Java Application Server Project
4  *
5  * The contents of this file are subject to the Enhydra Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License on
8  * the Enhydra web site ( http://www.enhydra.org/ ).
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific terms governing rights and limitations
13  * under the License.
14  *
15  * The Initial Developer of the Enhydra Application Server is Lutris
16  * Technologies, Inc. The Enhydra Application Server and portions created
17  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
18  * All Rights Reserved.
19  *
20  * Contributor(s):
21  *
22  * $Id: OutputStreamHub.java,v 1.2 2005/03/24 10:51:25 slobodan Exp $
23  */

24
25
26
27
28
29 package com.lutris.util;
30
31 import java.io.IOException JavaDoc;
32 import java.io.OutputStream JavaDoc;
33 import java.util.Enumeration JavaDoc;
34 import java.util.Vector JavaDoc;
35
36
37 /**
38     This class implements a "hub", or redistribution center.
39     Like a "Y" connector for garden hoses, but with an arbitrary
40     number of outputs, not just 2. <P>
41
42     Instances of this class maintain teir own set of OutputStreams.
43     When you create an instance of this class, the set is empty.
44     Writes to the hub have no effect. call <CODE>add()</CODE> to
45     register OutputStreams with the hub. All OutputStream calls are
46     routed to all the current members of the set.
47
48     @see java.io.OutputStream
49     @author Andy John
50 */

51 public class OutputStreamHub extends OutputStream JavaDoc {
52     
53     private Vector JavaDoc myStreams;
54     private boolean closed;
55
56     /**
57         Create a new hub with no members.
58     */

59     public OutputStreamHub() {
60         myStreams = new Vector JavaDoc();
61         closed = false;
62     }
63
64     
65     /**
66         Add a new OutputStream to the set. When <code>write()</code>s
67         happen, the will be coppied to all the current members of the set.
68     
69         @param newMember The OutputStream to add to the set.
70         @see java.io.OutputStream
71     */

72     public void add(OutputStream JavaDoc newMember) {
73         synchronized (myStreams) {
74             myStreams.addElement(newMember);
75         }
76     }
77
78
79     /**
80         Remove an OutputStream from the set.
81
82         @param member The OutputStream to remove.
83         @see java.io.OutputStream
84     */

85     public void remove(OutputStream JavaDoc member) {
86         synchronized (myStreams) {
87             myStreams.removeElement(member);
88         }
89     }
90
91
92     /**
93         Is the given OutputStream currently in the set?
94
95         @param stream The OutputStream to search for.
96         @see java.io.OutputStream
97     */

98     public boolean contains(OutputStream JavaDoc stream) {
99         synchronized (myStreams) {
100             return myStreams.contains(stream);
101         }
102     }
103
104     //----------------------------------------
105

106
107     /**
108         Write an integer to all the members. Writes are attempted on all
109         members. The last exception thrown, if there are more than
110         one, will be thown from this method.
111     
112         @param b The int to write.
113         @see java.io.OutputStream
114     */

115     public void write(int b) throws IOException JavaDoc {
116         synchronized (myStreams) {
117             if (closed)
118                 throw new IOException JavaDoc("write() called on closed OutputStream");
119             IOException JavaDoc err = null;
120             Enumeration JavaDoc e = myStreams.elements();
121             while (e.hasMoreElements()) {
122                 OutputStream JavaDoc o = (OutputStream JavaDoc) e.nextElement();
123                 try {
124                     o.write(b);
125                 } catch (IOException JavaDoc ioe) {
126                     err = ioe;
127                 }
128             }
129             if (err != null)
130                 throw err;
131         }
132     }
133
134
135     /**
136         Write an array of bytes to all the members.
137         Writes are attempted on all
138         members. The last exception thrown, if there are more than
139         one, will be thown from this method.
140     
141         @param b The byte array to write.
142         @see java.io.OutputStream
143     */

144     public void write(byte b[]) throws IOException JavaDoc {
145         synchronized (myStreams) {
146             if (closed)
147                 throw new IOException JavaDoc("write() called on closed OutputStream");
148             IOException JavaDoc err = null;
149             Enumeration JavaDoc e = myStreams.elements();
150             while (e.hasMoreElements()) {
151                 OutputStream JavaDoc o = (OutputStream JavaDoc) e.nextElement();
152                 try {
153                     o.write(b);
154                 } catch (IOException JavaDoc ioe) {
155                     err = ioe;
156                 }
157             }
158             if (err != null)
159                 throw err;
160         }
161     }
162
163
164     /**
165         Write part of an array of bytes to all the members.
166         Bytes b[off]..b[off+len-1] will be written.
167         Writes are attempted on all
168         members. The last exception thrown, if there are more than
169         one, will be thown from this method.
170     
171         @param b The byte array to write part of.
172         @param off The offset.
173         @param len How many bytes to write.
174         @see java.io.OutputStream
175     */

176     public void write(byte b[],
177                    int off,
178                    int len) throws IOException JavaDoc {
179         synchronized (myStreams) {
180             if (closed)
181                 throw new IOException JavaDoc("write() called on closed OutputStream");
182             IOException JavaDoc err = null;
183             Enumeration JavaDoc e = myStreams.elements();
184             while (e.hasMoreElements()) {
185                 OutputStream JavaDoc o = (OutputStream JavaDoc) e.nextElement();
186                 try {
187                     o.write(b, off, len);
188                 } catch (IOException JavaDoc ioe) {
189                     err = ioe;
190                 }
191             }
192             if (err != null)
193                 throw err;
194         }
195     }
196
197
198     /**
199         Flushes are attempted on all members.
200         The last exception thrown, if there are more than
201         one, will be thown from this method.
202         
203         @see java.io.OutputStream
204     */

205     public void flush() throws IOException JavaDoc {
206         synchronized (myStreams) {
207             if (closed)
208                 throw new IOException JavaDoc("flush() called on closed OutputStream");
209             IOException JavaDoc err = null;
210             Enumeration JavaDoc e = myStreams.elements();
211             while (e.hasMoreElements()) {
212                 OutputStream JavaDoc o = (OutputStream JavaDoc) e.nextElement();
213                 try {
214                     o.flush();
215                 } catch (IOException JavaDoc ioe) {
216                     err = ioe;
217                 }
218             }
219             if (err != null)
220                 throw err;
221         }
222     }
223
224
225     /**
226         Closes the stream. Do not call <CODE>write()</CODE> after this.
227         Calls <CODE>close()</CODE> on all member OutputStreams.
228         The last exception thrown, if there are more than
229         one, will be thown from this method.
230         
231         @see java.io.OutputStream
232     */

233     public void close() throws IOException JavaDoc {
234         synchronized (myStreams) {
235             if (closed)
236                 throw new IOException JavaDoc("OutputStream already closed.");
237             closed = true;
238             IOException JavaDoc err = null;
239             Enumeration JavaDoc e = myStreams.elements();
240             while (e.hasMoreElements()) {
241                 OutputStream JavaDoc o = (OutputStream JavaDoc) e.nextElement();
242                 try {
243                     o.close();
244                 } catch (IOException JavaDoc ioe) {
245                     err = ioe;
246                 }
247             }
248             if (err != null)
249                 throw err;
250         }
251     }
252
253 }
254
255
Popular Tags