| Home >> All >> Freenet >> [ support Javadoc ] |
Source code: Freenet/support/SplitOutputStream.java
1 package Freenet.support; 2 3 import java.io.*; 4 import java.util.*; 5 6 public class SplitOutputStream extends OutputStream implements Callback 7 { 8 private Vector outputs; 9 10 public SplitOutputStream() 11 { 12 outputs = new Vector(); 13 } 14 15 public void addOutput(OutputStream out) 16 { 17 addOutput(out, null); 18 } 19 20 public void addOutput(OutputStream out, Callback cb) 21 { 22 outputs.addElement(new StreamElement(out, cb)); 23 } 24 25 public void write(int c) 26 { 27 int len, x; 28 StreamElement current; 29 Writer cw; 30 31 len=outputs.size(); 32 x=0; 33 while(x<len) { 34 current=(StreamElement)outputs.elementAt(x); 35 try { 36 if (current.out!=null) 37 current.out.write(c); 38 x++; 39 } 40 catch(IOException e) { 41 if (current.cb != null) 42 current.cb.callback(); 43 outputs.removeElementAt(x); 44 len--; 45 } 46 } 47 } 48 49 50 public void close() 51 { 52 for (Enumeration e = outputs.elements() ; e.hasMoreElements() ; ) { 53 ((StreamElement)e.nextElement()).close(); 54 } 55 56 outputs.removeAllElements(); 57 58 } 59 60 /** 61 * When callback is called, the connections have their 62 * respective callbacks called, and are then removed. 63 **/ 64 65 public synchronized void callback() 66 { 67 for (Enumeration e = outputs.elements() ; e.hasMoreElements() ; ) { 68 ((StreamElement)e.nextElement()).callback(); 69 } 70 71 outputs.removeAllElements(); 72 } 73 } 74 75 class StreamElement implements Callback 76 { 77 public OutputStream out; 78 public Callback cb; 79 80 public StreamElement(OutputStream out, Callback cb) { 81 this.out = out; 82 this.cb = cb; 83 } 84 85 public void close() 86 { 87 try { 88 if (out!=null) out.close(); 89 } catch (IOException e) {} 90 callback(); 91 } 92 93 public void callback() 94 { 95 if (cb!=null) cb.callback(); 96 } 97 }