Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

In this tutorial, we’ll learn how to use the SequenceInputStream class in Java. In particular, this class is helpful in reading a series of files or streams continuously.

For more basics on Java IO and other related Java classes, we can read Java IO Tutorials.

2. Using the SequenceInputStream Class

SequenceInputStream takes two or more InputStream objects as sources. It reads from one source after the other in the given order. When it completes reading from the first InputStream, it automatically starts reading from the second. This process continues until it finishes reading from all the source streams.

2.1. Object Creation

We can initialize a SequenceInputStream using two InputStream objects:

InputStream first = new FileInputStream(file1);
InputStream second = new FileInputStream(file2);
SequenceInputStream sequenceInputStream = new SequenceInputStream(first, second);

We can also instantiate it using an Enumeration of InputStream objects:

Vector<InputStream> inputStreams = new Vector<>();
for (String fileName: fileNames) {
    inputStreams.add(new FileInputStream(fileName));
}
sequenceInputStream = new SequenceInputStream(inputStreams.elements());

2.2. Reading From the Stream

SequenceInputStream provides two simple methods to read from input sources. The first method reads one single byte, whereas the second method reads an array of bytes.

To read a single byte of data, we use the read() method:

int byteValue = sequenceInputStream.read();

In the above example, the read method returns the next byte (0 – 255) value from the stream. If the stream comes to an end, then it returns -1.

We can also read an array of bytes:

byte[] bytes = new byte[100];
sequenceInputStream.read(bytes, 0, 50);

In the above example, it reads 50 bytes and places them starting from index 0.

2.3. Example Showing Sequence Reading

Two strings are taken as the input source to demonstrate the read sequence:

InputStream first = new ByteArrayInputStream("One".getBytes());
InputStream second = new ByteArrayInputStream("Magic".getBytes());
SequenceInputStream sequenceInputStream = new SequenceInputStream(first, second);
StringBuilder stringBuilder = new StringBuilder();
int byteValue;
while ((byteValue = sequenceInputStream.read()) != -1) {
    stringBuilder.append((char) byteValue);
}
assertEquals("OneMagic", stringBuilder.toString());

From the above example, if we print stringBuilder.toString() it shows the following output:

OneMagic

3. Conclusion

In this short article, we’ve seen how to work with SequenceInputStream. It just combines all underlying input streams into one single stream.

A complete code sample can be found on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.