Combined InputStream : InputStream « File Input Output « Java






Combined InputStream

    
/**
 * Project pack:tag >> http://packtag.sf.net
 *
 * This software is published under the terms of the LGPL
 * License version 2.1, a copy of which has been included with this
 * distribution in the 'lgpl.txt' file.
 * 
 * Creation date: 15.04.2007 - 18:27:26
 * Last author:   $Author: danielgalan $
 * Last modified: $Date: 2008/02/10 20:03:21 $
 * Revision:      $Revision: 1.4 $
 * 
 * $Log: CombinedInputStream.java,v $
 * Revision 1.4  2008/02/10 20:03:21  danielgalan
 * Fix for IE - if after a closing bracket a var keyword is, IE can't interpret this, eg "varx={}var y ={}" mus be seperated: "varx={}\nvar y ={}"
 *
 * Revision 1.3  2007/05/02 21:38:38  danielgalan
 * alias to name
 *
 * Revision 1.2  2007/05/02 21:29:18  danielgalan
 * last fixes for 2.0, attribute media
 *
 * Revision 1.1  2007/04/22 19:04:24  danielgalan
 * pack.tag moved from subversion to good old CVS
 */

import java.io.IOException;
import java.io.InputStream;



/**
 * Reads multiple Inputstreams as one
 * 
 * @author  Daniel Galn y Martins
 * @version $Revision: 1.4 $
 */
public class CombinedInputStream extends InputStream {

  /** The current Stream to read from */
  private int current;

  boolean isNextLineFeed = false;

  /** The Streams to combine */
  private final InputStream[] streams;


  /**
   * Constructs an combined InputStream, that reads from array stream per stream, till the last stream
   * 
   * @param streams All Streams that will be combined
   */
  public CombinedInputStream(final InputStream[] streams) {
    current = 0;
    this.streams = streams;
  }


  /**
   * Reads from the list of streams, when the last stream is over, -1 will be returned (as usual)
   */
  public int read() throws IOException {
    if (isNextLineFeed) {
      isNextLineFeed = false;
      return '\n';
    }
    int i = -1;
    if (current < streams.length) {
      i = streams[current].read();
      if (i == -1) {
        isNextLineFeed = true;
        // the current stream has been finished, use the next one
        current++;
        return read();
      }
    }
    return i;
  }


  /** Closes all streams */
  public void close() throws IOException {
    for (int i = 0; i < streams.length; i++) {
      streams[i].close();
    }
  }


  /** Is there more data to read */
  public int available() throws IOException {
    return current < streams.length ? streams[current].available() : 0;
  }
}

   
    
    
    
  








Related examples in the same category

1.Creating an input or output stream on a ByteBuffer
2.Creating a Manifest for a JAR File
3.Convert InputStream to String
4.An limited-data-size input stream
5.An input stream which reads sequentially from multiple sources
6.Size Limit InputStream
7.Minimal InputStream subclass to fetch bytes form a String
8.Read and return the entire contents of the supplied InputStream. This method always closes the stream when finished reading.
9.Compare two InputStream
10.Read and return the entire contents of the supplied InputStream.
11.Deserializes an object from an input stream.
12.Reads at most certain bytes from input stream and returns them as a byte array.
13.Convert Reader to InputStream
14.A trace of the data that is being retrieved from an input stream
15.EOLConvertingInputStream: InputStream which converts \r bytes not followed by \n and \n not preceded by \r to \r\n.
16.A line reading wrapper that works with byte streams.
17.Smart Encoding InputStream
18.Wraps a stream that has been exported to import the data in readable form
19.This input stream wrapper closes the base input stream when fully read.
20.LZFInputStream and LZFOutputStream
21.Random input stream