1. Overview
1.概述
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.
在本教程中,我们将学习如何使用Java中的SequenceInputStream类。特别是,该类在连续读取一系列文件或流时很有帮助。
For more basics on Java IO and other related Java classes, we can read Java IO Tutorials.
关于Java IO和其他相关Java类的更多基础知识,我们可以阅读Java IO教程。
2. Using the SequenceInputStream Class
2.使用SequenceInputStream类
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.
SequenceInputStream将两个或多个InputStream对象作为源。它按照给定的顺序从一个源头到另一个源头进行读取。当它完成了从第一个InputStream的读取后,它自动开始从第二个读取。这个过程一直持续到它完成从所有源流的读取。
2.1. Object Creation
2.1.对象创建
We can initialize a SequenceInputStream using two InputStream objects:
我们可以初始化一个SequenceInputStream 使用两个InputStream对象。
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:
我们也可以用Enumeration的InputStream对象来实例化它。
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
2.2.从流中读取
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.
SequenceInputStream提供了两个简单的方法来从输入源读取。第一个方法读取一个单一的字节,而第二个方法读取一个字节的数组。
To read a single byte of data, we use the read() method:
为了读取一个字节的数据,我们使用read()方法。
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.
在上面的例子中,读取方法从流中返回下一个字节(0-255)值。如果流到了尽头,那么它返回-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.
在上面的例子中,它读取50字节并从索引0开始放置。
2.3. Example Showing Sequence Reading
2.3.显示顺序阅读的例子
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:
从上面的例子来看,如果我们打印stringBuilder.toString(),就会显示以下输出。
OneMagic
3. Conclusion
3.总结
In this short article, we’ve seen how to work with SequenceInputStream. It just combines all underlying input streams into one single stream.
在这篇短文中,我们已经看到如何使用SequenceInputStream。它只是将所有的底层输入流合并成一个单一的流。
A complete code sample can be found on GitHub.