001: package com.quadcap.io;
002:
003: /* Copyright 1997 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.InputStream;
042: import java.io.IOException;
043:
044: import java.util.Vector;
045:
046: import com.quadcap.util.Debug;
047:
048: /**
049: * This class implements an input stream which is formed by the concatenation
050: * of two or more separate input streams.
051: *
052: * @author Stan Bailes
053: */
054: public class ConcatInputStream extends InputStream {
055: Vector streams = new Vector();
056: int stream = 0;
057:
058: /**
059: * Construct a new ConcatInputStream formed by the concatenation of
060: * two input streams.
061: *
062: * @param in1 the first input stream
063: * @param in2 the second input stream
064: */
065: public ConcatInputStream(InputStream in1, InputStream in2) {
066: streams.addElement(in1);
067: streams.addElement(in2);
068: }
069:
070: /**
071: * Add another input stream to the concatenation.
072: *
073: * @param in the input stream to add.
074: */
075: public void addInputStream(InputStream in) {
076: streams.addElement(in);
077: }
078:
079: /**
080: * Reads the next byte of data from this input stream. The value
081: * byte is returned as an <code>int</code> in the range
082: * <code>0</code> to <code>255</code>. If no byte is available
083: * because the end of the stream has been reached, the value
084: * <code>-1</code> is returned. This method blocks until input data
085: * is available, the end of the stream is detected, or an exception
086: * is thrown.
087: * <p>
088: *
089: * @return the next byte of data, or <code>-1</code> if the end of the
090: * stream is reached.
091: * @exception IOException if an I/O error occurs.
092: */
093: public int read() throws IOException {
094: while (stream < streams.size()) {
095: InputStream is = (InputStream) streams.elementAt(stream);
096: int c = is.read();
097: if (c >= 0)
098: return c;
099: stream++;
100: }
101: return -1;
102: }
103:
104: /**
105: * Returns the number of bytes that can be read from this input
106: * stream without blocking. The available method of
107: * <code>InputStream</code> returns <code>0</code>.
108: *
109: * @return the number of bytes that can be read from this input stream
110: * without blocking.
111: * @exception IOException if an I/O error occurs.
112: */
113: public int available() throws IOException {
114: int avail = 0;
115: for (int i = stream; i < streams.size(); i++) {
116: InputStream is = (InputStream) streams.elementAt(i);
117: avail += is.available();
118: }
119: return avail;
120: }
121:
122: }
|