KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > packtag > util > CombinedInputStream


1 /**
2  * Project pack:tag >> http://packtag.sf.net
3  *
4  * This software is published under the terms of the LGPL
5  * License version 2.1, a copy of which has been included with this
6  * distribution in the 'lgpl.txt' file.
7  *
8  * Creation date: 15.04.2007 - 18:27:26
9  * Last author: $Author: danielgalan $
10  * Last modified: $Date: 2007/05/02 21:38:38 $
11  * Revision: $Revision: 1.3 $
12  *
13  * $Log: CombinedInputStream.java,v $
14  * Revision 1.3 2007/05/02 21:38:38 danielgalan
15  * alias to name
16  *
17  * Revision 1.2 2007/05/02 21:29:18 danielgalan
18  * last fixes for 2.0, attribute media
19  *
20  * Revision 1.1 2007/04/22 19:04:24 danielgalan
21  * pack.tag moved from subversion to good old CVS
22  */

23 package net.sf.packtag.util;
24
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27
28
29
30 /**
31  * Reads multiple Inputstreams as one
32  *
33  * @author Daniel Galán y Martins
34  * @version $Revision: 1.3 $
35  */

36 public class CombinedInputStream extends InputStream JavaDoc {
37
38     /** The current Stream to read from */
39     private int current;
40
41     /** The Streams to combine */
42     private InputStream JavaDoc[] streams;
43
44
45     /**
46      * Constructs an combined InputStream, that reads from array stream per stream, till the last stream
47      *
48      * @param streams All Streams that will be combined
49      */

50     public CombinedInputStream(InputStream JavaDoc[] streams) {
51         current = 0;
52         this.streams = streams;
53     }
54
55
56     /**
57      * Reads from the list of streams, when the last stream is over, -1 will be returned (as usual)
58      */

59     public int read() throws IOException JavaDoc {
60         int i = -1;
61         if (current < streams.length) {
62             i = streams[current].read();
63             if (i == -1) {
64                 // the current stream has been finished, use the next one
65
current++;
66                 return read();
67             }
68         }
69         return i;
70     }
71
72
73     /** Closes all streams */
74     public void close() throws IOException JavaDoc {
75         for (int i = 0; i < streams.length; i++) {
76             streams[i].close();
77         }
78     }
79
80
81     /** Is there more data to read */
82     public int available() throws IOException JavaDoc {
83         return current < streams.length ? streams[current].available() : 0;
84     }
85 }
86
Popular Tags