1 7 8 package java.io; 9 10 import java.io.InputStream ; 11 import java.util.Enumeration ; 12 import java.util.Vector ; 13 14 28 public 29 class SequenceInputStream extends InputStream { 30 Enumeration e; 31 InputStream in; 32 33 49 public SequenceInputStream(Enumeration <? extends InputStream > e) { 50 this.e = e; 51 try { 52 nextStream(); 53 } catch (IOException ex) { 54 throw new Error ("panic"); 56 } 57 } 58 59 70 public SequenceInputStream(InputStream s1, InputStream s2) { 71 Vector v = new Vector (2); 72 73 v.addElement(s1); 74 v.addElement(s2); 75 e = v.elements(); 76 try { 77 nextStream(); 78 } catch (IOException ex) { 79 throw new Error ("panic"); 81 } 82 } 83 84 87 final void nextStream() throws IOException { 88 if (in != null) { 89 in.close(); 90 } 91 92 if (e.hasMoreElements()) { 93 in = (InputStream ) e.nextElement(); 94 if (in == null) 95 throw new NullPointerException (); 96 } 97 else in = null; 98 99 } 100 101 106 public int available() throws IOException { 107 if(in == null) { 108 return 0; } 110 return in.available(); 111 } 112 113 131 public int read() throws IOException { 132 if (in == null) { 133 return -1; 134 } 135 int c = in.read(); 136 if (c == -1) { 137 nextStream(); 138 return read(); 139 } 140 return c; 141 } 142 143 161 public int read(byte b[], int off, int len) throws IOException { 162 if (in == null) { 163 return -1; 164 } else if (b == null) { 165 throw new NullPointerException (); 166 } else if ((off < 0) || (off > b.length) || (len < 0) || 167 ((off + len) > b.length) || ((off + len) < 0)) { 168 throw new IndexOutOfBoundsException (); 169 } else if (len == 0) { 170 return 0; 171 } 172 173 int n = in.read(b, off, len); 174 if (n <= 0) { 175 nextStream(); 176 return read(b, off, len); 177 } 178 return n; 179 } 180 181 196 public void close() throws IOException { 197 do { 198 nextStream(); 199 } while (in != null); 200 } 201 } 202 | Popular Tags |