home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / SequenceInputStream.java < prev    next >
Text File  |  1997-05-20  |  6KB  |  191 lines

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