src/share/classes/java/io/SequenceInputStream.java

Print this page




  27 
  28 import java.io.InputStream;
  29 import java.util.Enumeration;
  30 import java.util.Vector;
  31 
  32 /**
  33  * A <code>SequenceInputStream</code> represents
  34  * the logical concatenation of other input
  35  * streams. It starts out with an ordered
  36  * collection of input streams and reads from
  37  * the first one until end of file is reached,
  38  * whereupon it reads from the second one,
  39  * and so on, until end of file is reached
  40  * on the last of the contained input streams.
  41  *
  42  * @author  Author van Hoff
  43  * @since   JDK1.0
  44  */
  45 public
  46 class SequenceInputStream extends InputStream {
  47     Enumeration e;
  48     InputStream in;
  49 
  50     /**
  51      * Initializes a newly created <code>SequenceInputStream</code>
  52      * by remembering the argument, which must
  53      * be an <code>Enumeration</code>  that produces
  54      * objects whose run-time type is <code>InputStream</code>.
  55      * The input streams that are  produced by
  56      * the enumeration will be read, in order,
  57      * to provide the bytes to be read  from this
  58      * <code>SequenceInputStream</code>. After
  59      * each input stream from the enumeration
  60      * is exhausted, it is closed by calling its
  61      * <code>close</code> method.
  62      *
  63      * @param   e   an enumeration of input streams.
  64      * @see     java.util.Enumeration
  65      */
  66     public SequenceInputStream(Enumeration<? extends InputStream> e) {
  67         this.e = e;
  68         try {
  69             nextStream();
  70         } catch (IOException ex) {
  71             // This should never happen
  72             throw new Error("panic");
  73         }
  74     }
  75 
  76     /**
  77      * Initializes a newly
  78      * created <code>SequenceInputStream</code>
  79      * by remembering the two arguments, which
  80      * will be read in order, first <code>s1</code>
  81      * and then <code>s2</code>, to provide the
  82      * bytes to be read from this <code>SequenceInputStream</code>.
  83      *
  84      * @param   s1   the first input stream to read.
  85      * @param   s2   the second input stream to read.
  86      */
  87     public SequenceInputStream(InputStream s1, InputStream s2) {
  88         Vector  v = new Vector(2);
  89 
  90         v.addElement(s1);
  91         v.addElement(s2);
  92         e = v.elements();
  93         try {
  94             nextStream();
  95         } catch (IOException ex) {
  96             // This should never happen
  97             throw new Error("panic");
  98         }
  99     }
 100 
 101     /**
 102      *  Continues reading in the next stream if an EOF is reached.
 103      */
 104     final void nextStream() throws IOException {
 105         if (in != null) {
 106             in.close();
 107         }
 108 




  27 
  28 import java.io.InputStream;
  29 import java.util.Enumeration;
  30 import java.util.Vector;
  31 
  32 /**
  33  * A <code>SequenceInputStream</code> represents
  34  * the logical concatenation of other input
  35  * streams. It starts out with an ordered
  36  * collection of input streams and reads from
  37  * the first one until end of file is reached,
  38  * whereupon it reads from the second one,
  39  * and so on, until end of file is reached
  40  * on the last of the contained input streams.
  41  *
  42  * @author  Author van Hoff
  43  * @since   JDK1.0
  44  */
  45 public
  46 class SequenceInputStream extends InputStream {
  47     Enumeration<? extends InputStream> e;
  48     InputStream in;
  49 
  50     /**
  51      * Initializes a newly created <code>SequenceInputStream</code>
  52      * by remembering the argument, which must
  53      * be an <code>Enumeration</code>  that produces
  54      * objects whose run-time type is <code>InputStream</code>.
  55      * The input streams that are  produced by
  56      * the enumeration will be read, in order,
  57      * to provide the bytes to be read  from this
  58      * <code>SequenceInputStream</code>. After
  59      * each input stream from the enumeration
  60      * is exhausted, it is closed by calling its
  61      * <code>close</code> method.
  62      *
  63      * @param   e   an enumeration of input streams.
  64      * @see     java.util.Enumeration
  65      */
  66     public SequenceInputStream(Enumeration<? extends InputStream> e) {
  67         this.e = e;
  68         try {
  69             nextStream();
  70         } catch (IOException ex) {
  71             // This should never happen
  72             throw new Error("panic");
  73         }
  74     }
  75 
  76     /**
  77      * Initializes a newly
  78      * created <code>SequenceInputStream</code>
  79      * by remembering the two arguments, which
  80      * will be read in order, first <code>s1</code>
  81      * and then <code>s2</code>, to provide the
  82      * bytes to be read from this <code>SequenceInputStream</code>.
  83      *
  84      * @param   s1   the first input stream to read.
  85      * @param   s2   the second input stream to read.
  86      */
  87     public SequenceInputStream(InputStream s1, InputStream s2) {
  88         Vector<InputStream> v = new Vector<>(2);
  89 
  90         v.addElement(s1);
  91         v.addElement(s2);
  92         e = v.elements();
  93         try {
  94             nextStream();
  95         } catch (IOException ex) {
  96             // This should never happen
  97             throw new Error("panic");
  98         }
  99     }
 100 
 101     /**
 102      *  Continues reading in the next stream if an EOF is reached.
 103      */
 104     final void nextStream() throws IOException {
 105         if (in != null) {
 106             in.close();
 107         }
 108