Combined InputStream : InputStream « File Input Output « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » File Input Output » InputStreamScreenshots 
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 Galán 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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.