1 2 24 25 26 27 28 29 package com.lutris.util; 30 31 import java.io.IOException ; 32 import java.io.OutputStream ; 33 import java.util.Enumeration ; 34 import java.util.Vector ; 35 36 37 51 public class OutputStreamHub extends OutputStream { 52 53 private Vector myStreams; 54 private boolean closed; 55 56 59 public OutputStreamHub() { 60 myStreams = new Vector (); 61 closed = false; 62 } 63 64 65 72 public void add(OutputStream newMember) { 73 synchronized (myStreams) { 74 myStreams.addElement(newMember); 75 } 76 } 77 78 79 85 public void remove(OutputStream member) { 86 synchronized (myStreams) { 87 myStreams.removeElement(member); 88 } 89 } 90 91 92 98 public boolean contains(OutputStream stream) { 99 synchronized (myStreams) { 100 return myStreams.contains(stream); 101 } 102 } 103 104 106 107 115 public void write(int b) throws IOException { 116 synchronized (myStreams) { 117 if (closed) 118 throw new IOException ("write() called on closed OutputStream"); 119 IOException err = null; 120 Enumeration e = myStreams.elements(); 121 while (e.hasMoreElements()) { 122 OutputStream o = (OutputStream ) e.nextElement(); 123 try { 124 o.write(b); 125 } catch (IOException ioe) { 126 err = ioe; 127 } 128 } 129 if (err != null) 130 throw err; 131 } 132 } 133 134 135 144 public void write(byte b[]) throws IOException { 145 synchronized (myStreams) { 146 if (closed) 147 throw new IOException ("write() called on closed OutputStream"); 148 IOException err = null; 149 Enumeration e = myStreams.elements(); 150 while (e.hasMoreElements()) { 151 OutputStream o = (OutputStream ) e.nextElement(); 152 try { 153 o.write(b); 154 } catch (IOException ioe) { 155 err = ioe; 156 } 157 } 158 if (err != null) 159 throw err; 160 } 161 } 162 163 164 176 public void write(byte b[], 177 int off, 178 int len) throws IOException { 179 synchronized (myStreams) { 180 if (closed) 181 throw new IOException ("write() called on closed OutputStream"); 182 IOException err = null; 183 Enumeration e = myStreams.elements(); 184 while (e.hasMoreElements()) { 185 OutputStream o = (OutputStream ) e.nextElement(); 186 try { 187 o.write(b, off, len); 188 } catch (IOException ioe) { 189 err = ioe; 190 } 191 } 192 if (err != null) 193 throw err; 194 } 195 } 196 197 198 205 public void flush() throws IOException { 206 synchronized (myStreams) { 207 if (closed) 208 throw new IOException ("flush() called on closed OutputStream"); 209 IOException err = null; 210 Enumeration e = myStreams.elements(); 211 while (e.hasMoreElements()) { 212 OutputStream o = (OutputStream ) e.nextElement(); 213 try { 214 o.flush(); 215 } catch (IOException ioe) { 216 err = ioe; 217 } 218 } 219 if (err != null) 220 throw err; 221 } 222 } 223 224 225 233 public void close() throws IOException { 234 synchronized (myStreams) { 235 if (closed) 236 throw new IOException ("OutputStream already closed."); 237 closed = true; 238 IOException err = null; 239 Enumeration e = myStreams.elements(); 240 while (e.hasMoreElements()) { 241 OutputStream o = (OutputStream ) e.nextElement(); 242 try { 243 o.close(); 244 } catch (IOException ioe) { 245 err = ioe; 246 } 247 } 248 if (err != null) 249 throw err; 250 } 251 } 252 253 } 254 255 | Popular Tags |