    1   /* CompositeFileInputStream
    2   *
    3   * $Id: CompositeFileInputStream.java 4647 2006-09-22 18:39:39Z paul_jack $
    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;
    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      /* (non-Javadoc)
    42       * @see java.io.InputStream#read()
    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      /* (non-Javadoc)
    53       * @see java.io.InputStream#read(byte[], int, int)
    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      /* (non-Javadoc)
    64       * @see java.io.InputStream#read(byte[])
    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      /* (non-Javadoc)
    76       * @see java.io.InputStream#skip(long)
    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 }