| Home >> All >> Freenet >> [ support Javadoc ] |
Source code: Freenet/support/Funnel.java
1 2 package Freenet.support; 3 4 import java.io.*; 5 import java.util.*; 6 7 public class Funnel extends InputStream 8 { 9 private Vector inputs=new Vector(); 10 OutputStream dest; 11 12 public Funnel() 13 { 14 dest=null; 15 } 16 17 public Funnel(OutputStream out) 18 { 19 dest=out; 20 } 21 22 public void funnel() 23 { 24 int c; 25 26 if(dest!=null) 27 try 28 { 29 do 30 { 31 c=read(); 32 if(c!=-1) 33 dest.write(c); 34 } 35 while(c!=-1); 36 } 37 catch(IOException e) {} 38 } 39 40 public void addinput(InputStream in) 41 { 42 inputs.addElement(in); 43 } 44 45 public void addreader(Reader r) 46 { 47 inputs.addElement(r); 48 } 49 50 public int read() 51 { 52 int c; 53 54 if(inputs.size()==0) 55 return -1; 56 Object o=inputs.firstElement(); 57 if(o instanceof Reader) 58 { 59 Reader curreader=(Reader)o; 60 if(curreader==null) return -1; 61 try{ c=curreader.read(); if(c==-1) throw new IOException(); return c;} 62 catch(IOException e) 63 { 64 inputs.removeElement(curreader); 65 return read(); 66 } 67 } 68 else 69 { 70 InputStream current=(InputStream)o; 71 if(current==null) return -1; 72 try{ c=current.read(); if(c==-1) throw new IOException(); return c;} 73 catch(IOException e) 74 { 75 inputs.removeElement(current); 76 return read(); 77 } 78 } 79 } 80 }