001: /* CompositeFileInputStream
002: *
003: * $Id: CompositeFileInputStream.java 4647 2006-09-22 18:39:39Z paul_jack $
004: *
005: * Created on May 18, 2004
006: *
007: * Copyright (C) 2004 Internet Archive.
008: *
009: * This file is part of the Heritrix web crawler (crawler.archive.org).
010: *
011: * Heritrix is free software; you can redistribute it and/or modify
012: * it under the terms of the GNU Lesser Public License as published by
013: * the Free Software Foundation; either version 2.1 of the License, or
014: * any later version.
015: *
016: * Heritrix is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
019: * GNU Lesser Public License for more details.
020: *
021: * You should have received a copy of the GNU Lesser Public License
022: * along with Heritrix; if not, write to the Free Software
023: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024: */
025: package org.archive.io;
026:
027: import java.io.File;
028: import java.io.FileInputStream;
029: import java.io.FilterInputStream;
030: import java.io.IOException;
031: import java.util.Iterator;
032: import java.util.List;
033:
034: /**
035: * @author gojomo
036: */
037: public class CompositeFileInputStream extends FilterInputStream {
038: Iterator<File> filenames;
039:
040: /* (non-Javadoc)
041: * @see java.io.InputStream#read()
042: */
043: public int read() throws IOException {
044: int c = super .read();
045: if (c == -1 && filenames.hasNext()) {
046: cueStream();
047: return read();
048: }
049: return c;
050: }
051:
052: /* (non-Javadoc)
053: * @see java.io.InputStream#read(byte[], int, int)
054: */
055: public int read(byte[] b, int off, int len) throws IOException {
056: int c = super .read(b, off, len);
057: if (c == -1 && filenames.hasNext()) {
058: cueStream();
059: return read(b, off, len);
060: }
061: return c;
062: }
063:
064: /* (non-Javadoc)
065: * @see java.io.InputStream#read(byte[])
066: */
067: public int read(byte[] b) throws IOException {
068: int c = super .read(b);
069: if (c == -1 && filenames.hasNext()) {
070: cueStream();
071: return read(b);
072: }
073: return c;
074: }
075:
076: /* (non-Javadoc)
077: * @see java.io.InputStream#skip(long)
078: */
079: public long skip(long n) throws IOException {
080: long s = super .skip(n);
081: if (s < n && filenames.hasNext()) {
082: cueStream();
083: return s + skip(n - s);
084: }
085: return s;
086: }
087:
088: /**
089: * @param files
090: * @throws IOException
091: */
092: public CompositeFileInputStream(List<File> files)
093: throws IOException {
094: super (null);
095: filenames = files.iterator();
096: cueStream();
097: }
098:
099: private void cueStream() throws IOException {
100: if (filenames.hasNext()) {
101: this .in = new FileInputStream(filenames.next());
102: }
103: }
104:
105: }
|