KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > archive > io > CompositeFileInputStream


1 /* CompositeFileInputStream
2 *
3 * $Id: CompositeFileInputStream.java,v 1.3.4.1 2007/01/13 01:31:32 stack-sf Exp $
4 *
5 * Created on May 18, 2004
6 *
7 * Copyright (C) 2004 Internet Archive.
8 *
9 * This file is part of the Heritrix web crawler (crawler.archive.org).
10 *
11 * Heritrix is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser Public License as published by
13 * the Free Software Foundation; either version 2.1 of the License, or
14 * any later version.
15 *
16 * Heritrix is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser Public License
22 * along with Heritrix; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */

25 package org.archive.io;
26
27 import java.io.File JavaDoc;
28 import java.io.FileInputStream JavaDoc;
29 import java.io.FilterInputStream JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33
34
35 /**
36  * @author gojomo
37  */

38 public class CompositeFileInputStream extends FilterInputStream JavaDoc{
39     Iterator JavaDoc<File JavaDoc> filenames;
40
41     /* (non-Javadoc)
42      * @see java.io.InputStream#read()
43      */

44     public int read() throws IOException JavaDoc {
45         int c = super.read();
46         if( c == -1 && filenames.hasNext() ) {
47             cueStream();
48             return read();
49         }
50         return c;
51     }
52     /* (non-Javadoc)
53      * @see java.io.InputStream#read(byte[], int, int)
54      */

55     public int read(byte[] b, int off, int len) throws IOException JavaDoc {
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     /* (non-Javadoc)
64      * @see java.io.InputStream#read(byte[])
65      */

66     public int read(byte[] b) throws IOException JavaDoc {
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     /* (non-Javadoc)
76      * @see java.io.InputStream#skip(long)
77      */

78     public long skip(long n) throws IOException JavaDoc {
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 JavaDoc<File JavaDoc> files) throws IOException JavaDoc {
92         super(null);
93         filenames = files.iterator();
94         cueStream();
95     }
96
97     private void cueStream() throws IOException JavaDoc {
98         if(filenames.hasNext()) {
99             this.in = new FileInputStream JavaDoc(filenames.next());
100         }
101     }
102
103 }
104
Popular Tags