1 25 package org.archive.io; 26 27 import java.io.File ; 28 import java.io.FileInputStream ; 29 import java.io.FilterInputStream ; 30 import java.io.IOException ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 34 35 38 public class CompositeFileInputStream extends FilterInputStream { 39 Iterator <File > filenames; 40 41 44 public int read() throws IOException { 45 int c = super.read(); 46 if( c == -1 && filenames.hasNext() ) { 47 cueStream(); 48 return read(); 49 } 50 return c; 51 } 52 55 public int read(byte[] b, int off, int len) throws IOException { 56 int c = super.read(b, off, len); 57 if( c == -1 && filenames.hasNext() ) { 58 cueStream(); 59 return read(b,off,len); 60 } 61 return c; 62 } 63 66 public int read(byte[] b) throws IOException { 67 int c = super.read(b); 68 if( c == -1 && filenames.hasNext() ) { 69 cueStream(); 70 return read(b); 71 } 72 return c; 73 } 74 75 78 public long skip(long n) throws IOException { 79 long s = super.skip(n); 80 if( s<n && filenames.hasNext() ) { 81 cueStream(); 82 return s + skip(n-s); 83 } 84 return s; 85 } 86 87 91 public CompositeFileInputStream(List <File > files) throws IOException { 92 super(null); 93 filenames = files.iterator(); 94 cueStream(); 95 } 96 97 private void cueStream() throws IOException { 98 if(filenames.hasNext()) { 99 this.in = new FileInputStream (filenames.next()); 100 } 101 } 102 103 } 104 | Popular Tags |