home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / io / SequenceInputStream.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  5.5 KB  |  178 lines

  1. /*
  2.  * @(#)SequenceInputStream.java    1.16 98/03/18
  3.  *
  4.  * Copyright 1994-1997 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.io;
  16.  
  17. import java.io.InputStream;
  18. import java.util.Enumeration;
  19. import java.util.Vector;
  20.  
  21. /**
  22.  * The sequence input stream class allows an application to combine 
  23.  * several input streams serially and make them appear as if they 
  24.  * were a single input stream. Each input stream is read from, in 
  25.  * turn, until it reaches the end of the stream. The sequence input 
  26.  * stream class then closes that stream and automatically switches to 
  27.  * the next input stream. 
  28.  *
  29.  * @author  Author van Hoff
  30.  * @version 1.16, 03/18/98
  31.  * @since   JDK1.0
  32.  */
  33. public
  34. class SequenceInputStream extends InputStream {
  35.     Enumeration e;
  36.     InputStream in;
  37.     
  38.     /**
  39.      * Constructs a new sequence input stream initialized to the 
  40.      * specified enumeration of input streams. Each object in the 
  41.      * enumeration must be an <code>InputStream</code>. 
  42.      *
  43.      * @param   e   an enumeration of input streams.
  44.      * @see     java.util.Enumeration
  45.      */
  46.     public SequenceInputStream(Enumeration e) {
  47.     this.e = e;
  48.     try {
  49.         nextStream();
  50.     } catch (IOException ex) {
  51.         // This should never happen
  52.         throw new Error("panic");
  53.     }
  54.     }
  55.   
  56.     /**
  57.      * Constructs a new sequence input stream initialized to read first 
  58.      * from the input stream <code>s1</code>, and then from the input 
  59.      * stream <code>s2</code>. 
  60.      *
  61.      * @param   s1   the first input stream to read.
  62.      * @param   s2   the second input stream to read.
  63.      */
  64.     public SequenceInputStream(InputStream s1, InputStream s2) {
  65.     Vector    v = new Vector(2);
  66.  
  67.     v.addElement(s1);
  68.     v.addElement(s2);
  69.     e = v.elements();
  70.     try {
  71.         nextStream();
  72.     } catch (IOException ex) {
  73.         // This should never happen
  74.         throw new Error("panic");
  75.     }
  76.     }
  77.    
  78.     /**
  79.      *  Continues reading in the next stream if an EOF is reached.
  80.      */
  81.     final void nextStream() throws IOException {
  82.     if (in != null) {
  83.         in.close();
  84.     }
  85.     in = e.hasMoreElements() ? (InputStream) e.nextElement() : null;
  86.     }
  87.  
  88.     /**
  89.      * Returns the number of bytes available on the current stream.
  90.      *
  91.      * @since   JDK1.1
  92.      */
  93.     public int available() throws IOException {
  94.     if(in == null) {
  95.         return 0; // no way to signal EOF from available()
  96.     } 
  97.     return in.available();
  98.     }
  99.  
  100.     /**
  101.      * Reads the next byte of data from this input stream. The byte is 
  102.      * returned as an <code>int</code> in the range <code>0</code> to 
  103.      * <code>255</code>. If no byte is available because the end of the 
  104.      * stream has been reached, the value <code>-1</code> is returned. 
  105.      * This method blocks until input data is available, the end of the 
  106.      * stream is detected, or an exception is thrown. 
  107.      * <p>
  108.      * The <code>read</code> method of <code>SequenceInputStream</code> 
  109.      * tries to read one character from the current substream. If it 
  110.      * reaches the end of the stream, it calls the <code>close</code> 
  111.      * method of the current substream and begins reading from the next 
  112.      * substream. 
  113.      *
  114.      * @return     the next byte of data, or <code>-1</code> if the end of the
  115.      *             stream is reached.
  116.      * @exception  IOException  if an I/O error occurs.
  117.      */
  118.     public int read() throws IOException {
  119.     if (in == null) {
  120.         return -1;
  121.     }
  122.     int c = in.read();
  123.     if (c == -1) {
  124.         nextStream();
  125.         return read();
  126.     }
  127.     return c;
  128.     }
  129.  
  130.     /**
  131.      * Reads up to <code>len</code> bytes of data from this input stream 
  132.      * into an array of bytes. This method blocks until at least 1 byte 
  133.      * of input is available. If the first argument is <code>null</code>, 
  134.      * up to <code>len</code> bytes are read and discarded. 
  135.      * <p>
  136.      * The <code>read</code> method of <code>SequenceInputStream</code> 
  137.      * tries to read the data from the current substream. If it fails to 
  138.      * read any characters because the substream has reached the end of 
  139.      * the stream, it calls the <code>close</code> method of the current 
  140.      * substream and begins reading from the next substream. 
  141.      *
  142.      * @param      b     the buffer into which the data is read.
  143.      * @param      off   the start offset of the data.
  144.      * @param      len   the maximum number of bytes read.
  145.      * @exception  IOException  if an I/O error occurs.
  146.      */
  147.     public int read(byte buf[], int pos, int len) throws IOException {
  148.     if (in == null) {
  149.         return -1;
  150.     } else if (len == 0) { 
  151.         return 0;
  152.     }
  153.     int n = in.read(buf, pos, len);
  154.     if (n <= 0) {
  155.         nextStream();
  156.         return read(buf, pos, len);
  157.     }
  158.     return n;
  159.     }
  160.  
  161.     /**
  162.      * Closes this input stream and releases any system resources 
  163.      * associated with the stream. 
  164.      * <p>
  165.      * The <code>close</code> method of <code>SequenceInputStream</code> 
  166.      * calls the <code>close</code> method of both the substream from 
  167.      * which it is currently reading and the <code>close</code> method of 
  168.      * all the substreams that it has not yet begun to read from. 
  169.      *
  170.      * @exception  IOException  if an I/O error occurs.
  171.      */
  172.     public void close() throws IOException {
  173.     do {
  174.         nextStream();
  175.     } while (in != null);
  176.     }
  177. }
  178.