The SequenceInputStream class allows you to concatenate multiple InputStreams. It reads data of streams one by one. 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.
Constructor and Description
- SequenceInputStream(Enumeration e) : Initializes a newly created SequenceInputStream , which must be an Enumeration that produces objects whose type is InputStream.
- SequenceInputStream(InputStream s1, InputStream s2) : Initializes a newly created SequenceInputStream by remembering the two arguments, which will be read in order, first s1 and then s2.
- read :Reads the next byte of data from this input stream.
Syntax: public int read() throws IOException Returns: the next byte of data, or -1 if the end of the stream is reached. Throws: IOException - if an I/O error occurs.
- read(byte b, int off, int len) : Reads up to len bytes of data from this input stream into an array of
Syntax: public int read(byte b, int off, int len) throws IOException Overrides: read in class InputStream Parameters: b - the buffer into which the data is read. off - the start offset in array b at which the data is written. len - the maximum number of bytes read. Returns: int the number of bytes read. Throws: NullPointerException - If b is null. IndexOutOfBoundsException - If off is negative, len is negative, or len is greater than b.length - off IOException - if an I/O error occurs.
- available : Returns an estimate of the number of bytes that can be read (or skipped over) from the current underlying input stream without blocking by the next invocation of a method for the current underlying input stream.
Syntax : public int available() throws IOException Overrides: 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 this input stream has been closed by invoking its close() method Throws: IOException - if an I/O error occurs.
- close:Closes this input stream and releases any system resources associated with the stream.
Syntax : public void close() throws IOException Overrides: close in class InputStream Throws: IOException - if an I/O error occurs.
The following is an example of SequenceInputStream class that implements some of the important methods.
19 This is first file This is second file This is third file
Note: This program will not run on online IDE as there are no files associated with it.
This article is contributed by Nishant Sharma. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.