Java SequenceInputStream Class with Examples
Sequence As sources, InputStream accepts two or more InputStream objects. It reads from one source after another in the order specified. When it has finished reading from the first InputStream, it will automatically begin reading from the second. This process is repeated until all of the source streams have been read.
Object Creation
We can use two InputStream objects to initialize a SequenceInputStream:
first InputStream = new FileInputStream(file1); second InputStream = new FileInputStream(file2); InputStream SequenceInputStream SequenceSequenceInputStream(first, second) = new SequenceInputStream(first, second); Copy
We can also use an Enumeration of InputStream objects to create it:
Vector<InputStream> inputStreams = new Vector<>();
for (String fileName: fileNames) {
inputStreams.add(new FileInputStream(fileName));
}
sequenceInputStream = new SequenceInputStream(inputStreams.elements());
Reading from a stream:
Reading From the Stream SequenceTo read from input sources, InputStream provides two simple methods. The first method reads a single byte, while the second reads an array of bytes.
To read a single byte of data, we use the read() method:
sequenceInputStream.read(); int byteValue
The read method in the preceding example returns the next byte (0 – 255) value from the stream. If the stream is exhausted, it returns -1.
Example:
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.SequenceInputStream;
public class TechVidvan {
public static void main(String[] args) {
try (InputStream first = new ByteArrayInputStream("Learning Java in ".getBytes());
InputStream second = new ByteArrayInputStream("TechVidvan Tutorials".getBytes());
SequenceInputStream sequenceInputStream = new SequenceInputStream(first, second)) {
StringBuilder stringBuilder = new StringBuilder();
int byteValue;
while ((byteValue = sequenceInputStream.read()) != -1) {
stringBuilder.append((char) byteValue);
}
System.out.println("Result: " + stringBuilder.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output:
Result: Learning Java in TechVidvan Tutorials
Notes:
public int available() raises an exception IOException Returns an estimate of the number of bytes that can be read (or skipped over) from the current underlying input stream without the following method invocation for the current underlying input stream blocking. The subsequent invocation could be from the same thread or a different one. A single read or skip of these many bytes will not cause a blocking error, but it may read or skip fewer bytes.
This method calls the current underlying input stream’s available method and returns the result.
Overrides:
These are available in class. InputStream Returns: an estimate of the number of bytes that can be read (or skipped over) from the current underlying input stream without blocking, or 0 if the input stream has been closed by invoking closeInputStream().
IOException is thrown by public void close().
This function closes this input stream and releases any associated system resources. A closed SequenceInputStream cannot be used for input or reopened.
If this stream was created from an enumeration, the enumeration’s remaining elements are requested and closed before the close method returns.
- close in interface is specified Closeable
- close in interface is specified AutoCloseable
Overrides: close in class InputStream Throws: IOException – if there is an I/O error.
Constructors of SequenceInputStream class in Java
| Constructor | Description |
| The subsequent byte is read from the input stream. | Creates a new input stream by sequentially reading the data from two input streams, s1 and s2. |
| Enumeration SequenceInputStream | reads the data of an enumeration type InputStream to create a new input stream. |
Methods of SequenceInputStream class in Java
| Method | Description |
| int read() | It reads the subsequent data byte from the input stream. |
| int read(byte[] ary, int off, int len) | The next data byte is read from the input stream by it. |
| int available() | It accepts the subsequent data byte from the input stream. |
| void close() | It reads the subsequent byte from the input stream. |
Method Details:
1. IOException is thrown by public int available().
2. Returns an estimate of the number of bytes that can be read (or skipped over) from the current underlying input stream without the following method invocation for the current underlying input stream blocking. The subsequent invocation could be from the same thread or a different one. A single read or skip of these many bytes will not cause a blocking error, but it may read or skip fewer bytes.
3. This method calls the current underlying input stream’s available method and returns the result.
4. Overrides: These are available in class. InputStream Returns: an estimate of the number of bytes that can be read (or skipped over) from the current underlying input stream without blocking, or 0 if the input stream has been closed by invoking closeInputStream().
5. Constructor Detail:
SequenceInputStream public
SequenceInputStream(Enumeration? extends InputStream> e) creates a new SequenceInputStream by remembering the argument, which must be an Enumeration that produces objects with the run-time type InputStream. The enumerated input streams will be read to provide the bytes to be read from this SequenceInputStream. After the enumeration’s input streams have been exhausted, they are closed by calling their close method.
- Parameters:
- e – an array of input streams.
- Also see: enumeration
SequenceInputStream:
public SequenceInputStream(InputStream s1, InputStream s2) creates a new SequenceInputStream by remembering the two arguments that will be read in order, s1 and then s2, to provide the bytes to be read from this SequenceInputStream.
- s1 is the first input stream to be read.
- s2 denotes the second input stream to be read.
Read
public int read() raises an exception IOException
This function reads the next byte of data from this input stream. The byte is returned as an int with a value between 0 and 255. If no byte is available because the stream has reached its end, the value -1 is returned. This method will block until input data is available, the stream’s end is detected, or an exception is thrown.
This method attempts to read one character from the currently selected substream. If it reaches the end of the stream, it calls the current substream’s close method and starts reading from the next substream.
Specified by: class reading InputStream returns the next byte of data, or -1 if the stream has reached its end.
close:
public void close() throws an IOException
This function closes this input stream and releases any associated system resources. A closed SequenceInputStream cannot be used for input or reopened.
If this stream was created from an enumeration, the enumeration’s remaining elements are requested and closed before the close method returns.
- close in interface is specified Closeable
- close in interface is specified AutoCloseable
Overrides: close in class InputStream Throws: IOException – if there is an I/O error.
Conclusion
In this short article, we’ve seen how to work with SequenceInputStream. It simply merges all underlying input streams into a single stream.
