1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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 /***
36 * @author gojomo
37 */
38 public class CompositeFileInputStream extends FilterInputStream{
39 Iterator<File> filenames;
40
41
42
43
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
53
54
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
64
65
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
76
77
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 /***
88 * @param files
89 * @throws IOException
90 */
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 }