Filter Streams   «Prev  Next»

Lesson 10Connecting streams
ObjectiveWrite a Java Program that connects Streams in Sequence

Write Java Program to connect Streams in Sequence

The SequenceInputStream class connects multiple input streams in a particular order. Then, reads from the SequenceInputStream first read all the bytes from stream 1, then all the bytes from stream 2, then all the bytes from stream 3, and so on. When the end of one of the underlying streams is reached, that stream is closed and the next read reads from the next stream in the sequence. Of course this assumes that the streams in sequence are in fact finite.
There are two constructors for this class.

Sequence InputStream Constructors

public SequenceInputStream(Enumeration e)

public SequenceInputStream(InputStream s1, InputStream s2)
public class SequenceInputStream
extends InputStream
A SequenceInputStream represents the logical concatenation of other input streams. It starts out with an ordered collection of input streams and reads from the first one until end of file is reached, whereupon it reads from the second one, and so on, until end of file is reached on the last of the contained input streams.

The first constructor creates a sequence out of all the elements of the enumeration e.
The second constructor creates a sequence input stream that reads first from s1, then from s2.
Note that s1 or s2 may themselves be sequence input streams, so repeated application of this constructor allows a sequence input stream with an indefinite number of underlying streams to be created.


To read the home pages of both JavaSoft and AltaVista, you might write code similar to this:
try {
  URL u1 = new URL("https://www.gofpattern.com");
  URL u2 = new URL("https://www.cplusoop.com");
  SequenceInputStream sis = new SequenceInputStream(u1.openStream(),
   u2.openStream());
  //...
}
catch (IOException e) { //...

Java Program to connect Streams in sequence using SequenceInputStream Class to connect multiple input streams in order

Here is an example of a Java program that uses the SequenceInputStream class to connect multiple input streams in order:
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.SequenceInputStream;

public class SequenceExample {
   public static void main(String[] args) {
       try {
           // Create the input streams
           ByteArrayInputStream stream1 = new ByteArrayInputStream("Hello, ".getBytes());
           ByteArrayInputStream stream2 = new ByteArrayInputStream("World!".getBytes());

           // Create the SequenceInputStream
           SequenceInputStream input = new SequenceInputStream(stream1, stream2);

           // Read data from the SequenceInputStream
           int data = input.read();
           while (data != -1) {
               System.out.print((char) data);
               data = input.read();
           }
           input.close();
       } catch (IOException e) {
           e.printStackTrace();
       }
   }
}
The program starts by creating two ByteArrayInputStream objects, each containing a different string of bytes. Then it creates a SequenceInputStream object, passing the two input streams as arguments. The SequenceInputStream reads data from the first input stream until it is exhausted, then reads from the second input stream, and so on. In this example the program reads data from the SequenceInputStream and prints it to the console. The output will be "Hello, World!" which is a concatenation of the two input streams.
It is worth noting that you can pass multiple input streams to the SequenceInputStream constructor and it will read them in the order they are passed.

Connecting Streams - Exercise

Click the Exercise link below to write a program that reads a series of filenames from the command line, creates a sequence input stream from file input streams for each file named on the command line, then copies the contents of all the files onto System.out.
Connecting Streams - Exercise