Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until end-of-day today:

>> GET ACCESS NOW

November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until end-of-day today:

>> GET ACCESS NOW

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.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until end-of-day today:

>> GET ACCESS NOW

Generic footer banner
Comments are closed on this article!