home *** CD-ROM | disk | FTP | other *** search
/ Chip 1997 October / Chip_1997-10_cd.bin / tema / sybase / powerj / java.z / SequenceInputStream.java < prev    next >
Text File  |  1996-05-03  |  3KB  |  130 lines

  1. /*
  2.  * @(#)SequenceInputStream.java    1.10 95/11/13 Arthur van Hoff
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package java.io;
  21.  
  22. import java.io.InputStream;
  23. import java.util.Enumeration;
  24. import java.util.Vector;
  25.  
  26. /**
  27.  * Converts a sequence of input streams into an InputStream.
  28.  * 
  29.  * @author   Author van Hoff
  30.  * @version  1.10, 13 Nov 1995
  31.  */
  32. public
  33. class SequenceInputStream extends InputStream {
  34.     Enumeration e;
  35.     InputStream in;
  36.     
  37.     /**
  38.      * Constructs a new SequenceInputStream initialized to the 
  39.      * specified list.
  40.      * @param e the list
  41.      */
  42.     public SequenceInputStream(Enumeration e) {
  43.     this.e = e;
  44.     try {
  45.         nextStream();
  46.     } catch (IOException ex) {
  47.         // This should never happen
  48.         throw new Error("panic");
  49.     }
  50.     }
  51.   
  52.     /**
  53.      * Constructs a new SequenceInputStream initialized to the two
  54.      * specified input streams.
  55.      * @param s1 the first input stream
  56.      * @param s2 the second input stream
  57.      */
  58.     public SequenceInputStream(InputStream s1, InputStream s2) {
  59.     Vector    v = new Vector(2);
  60.  
  61.     v.addElement(s1);
  62.     v.addElement(s2);
  63.     e = v.elements();
  64.     try {
  65.         nextStream();
  66.     } catch (IOException ex) {
  67.         // This should never happen
  68.         throw new Error("panic");
  69.     }
  70.     }
  71.    
  72.     /**
  73.      *  Continues reading in the next stream if an EOF is reached.
  74.      */
  75.     final void nextStream() throws IOException {
  76.     if (in != null) {
  77.         in.close();
  78.     }
  79.     in = e.hasMoreElements() ? (InputStream) e.nextElement() : null;
  80.     }
  81.  
  82.     /**
  83.      * Reads a stream, and upon reaching an EOF, flips to the next 
  84.      * stream.
  85.      */
  86.     public int read() throws IOException {
  87.     if (in == null) {
  88.         return -1;
  89.     }
  90.     int c = in.read();
  91.     if (c == -1) {
  92.         nextStream();
  93.         return read();
  94.     }
  95.     return c;
  96.     }
  97.  
  98.     /**
  99.      * Reads data into an array of bytes, and upon reaching an EOF, 
  100.      * flips to the next stream.
  101.      * @param buf the buffer into which the data is read
  102.      * @param pos the start position of the data
  103.      * @param len the maximum number of bytes read
  104.      * @exception IOException If an I/O error has occurred.
  105.      */
  106.     public int read(byte buf[], int pos, int len) throws IOException {
  107.     if (in == null) {
  108.         return -1;
  109.     }
  110.     int n = in.read(buf, pos, len);
  111.     if (n <= 0) {
  112.         nextStream();
  113.         return read(buf, pos, len);
  114.     }
  115.     return n;
  116.     }
  117.  
  118.     /**
  119.      * Closes the input stream; flipping to the next stream,
  120.      * if an EOF is reached.   This method must be called to release
  121.      * any resources associated with the stream.
  122.      * @exception IOException If an I/O error has occurred.
  123.      */
  124.     public void close() throws IOException {
  125.     do {
  126.         nextStream();
  127.     } while (in != null);
  128.     }
  129. }
  130.