Java QueueInputStream-class And Method Code Example
Here is an example of how you might use the QueueInputStream
class from the org.apache.commons.io package in Java:
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import org.apache.commons.io.input.QueueInputStream;
public class MyClass {
public void doSomethingWithQueueInputStream() throws Exception {
// Create a byte array input stream
byte[] data = new byte[] {1, 2, 3, 4, 5};
InputStream inputStream1 = new ByteArrayInputStream(data);
// Create another byte array input stream
byte[] moreData = new byte[] {6, 7, 8, 9};
InputStream inputStream2 = new ByteArrayInputStream(moreData);
// Create a queue input stream
QueueInputStream queueInputStream = new QueueInputStream();
// Add the input streams to the queue
queueInputStream.addInputStream(inputStream1);
queueInputStream.addInputStream(inputStream2);
// Read data from the queue input stream
int value = queueInputStream.read();
while (value != -1) {
System.out.println(value);
value = queueInputStream.read();
}
}
}
The QueueInputStream
class is a composite input stream that allows multiple input streams to be read in sequence as if they were one input stream. In this example, we created two ByteArrayInputStream and added to the QueueInputStream and then read data from the QueueInputStream, it will read the data from both streams in the order they were added, as if they were one continuous stream.
Note that the QueueInputStream
class also provides a method removeInputStream() to remove an input stream from the queue and a method clearQueue() to remove all input streams from the queue.