1 36 package org.columba.ristretto.io; 37 38 import java.io.IOException ; 39 import java.io.InputStream ; 40 import java.util.Arrays ; 41 import java.util.Iterator ; 42 import java.util.List ; 43 44 45 46 51 public class SequenceInputStream extends InputStream { 52 53 List streams; 54 int index; 55 56 61 public SequenceInputStream(List e) { 62 streams = e; 63 } 64 65 71 public SequenceInputStream(InputStream s1, InputStream s2) { 72 streams = Arrays.asList(new InputStream [] {s1, s2}); 73 } 74 75 78 public int read() throws IOException { 79 int read = ((InputStream )streams.get(index)).read(); 80 if( read == -1 && index < streams.size()-1) { 81 index++; 82 read = ((InputStream )streams.get(index)).read(); 83 } 84 85 return read; 86 } 87 88 91 public int read(byte[] b, int off, int len) throws IOException { 92 int read = ((InputStream )streams.get(index)).read(b,off,len); 93 94 while( read != len && index < streams.size()-1) { 95 if (read == -1) read = 0; 96 index++; 97 int nextread = ((InputStream )streams.get(index)).read(b,off+read,len-read); 98 99 if( nextread != -1) read += nextread; 100 } 101 102 return read; 103 } 104 105 108 public int available() throws IOException { 109 int size = 0; 110 for( int i=0; i<streams.size(); i++) { 111 size += ((InputStream )streams.get(i)).available(); 112 } 113 return size; 114 } 115 116 119 public void close() throws IOException { 120 Iterator it = streams.iterator(); 121 while( it.hasNext() ) { 122 ((InputStream ) it.next()).close(); 123 } 124 } 125 126 } 127 | Popular Tags |