
- Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package Useful Resources
- Java.io - Discussion
Java - SequenceInputStream Class
Introduction
The Java SequenceInputStream class represents the logical concatenation of other input streams. 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.
Class declaration
Following is the declaration for Java.io.SequenceInputStream class −
public class SequenceInputStream extends InputStream
Class constructors
Sr.No. | Constructor & Description |
---|---|
1 | SequenceInputStream(Enumeration<? extends InputStream> e) This initializes a newly created SequenceInputStream by remembering the argument, which must be an Enumeration that produces objects whose run-time type is InputStream. |
2 | SequenceInputStream(InputStream s1, InputStream s2) This initializes a newly created SequenceInputStream by remembering the two arguments, which will be read in order, first s1 and then s2, to provide the bytes to be read from this SequenceInputStream. |
Class methods
Sr.No. | Method & Description |
---|---|
1 | int available() This method 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. |
2 | void close() This method closes this input stream and releases any system resources associated with the stream. |
3 | int read() This method reads the next byte of data from this input stream. |
4 | int read(byte[] b, int off, int len) This method reads up to len bytes of data from this input stream into an array of bytes. |
Methods inherited
This class inherits methods from the following classes â
- Java.io.InputStream
- Java.io.Object
Example - available() before and after reading data
The following example shows the usage of SequenceInputStream available() method.
SequenceInputStreamDemo.java
package com.tutorialspoint; import java.io.ByteArrayInputStream; import java.io.SequenceInputStream; import java.io.IOException; public class SequenceInputStreamDemo { public static void main(String[] args) { try { ByteArrayInputStream input1 = new ByteArrayInputStream("Hello".getBytes()); ByteArrayInputStream input2 = new ByteArrayInputStream("World".getBytes()); SequenceInputStream sis = new SequenceInputStream(input1, input2); System.out.println("Available before reading: " + sis.available()); // Read some data sis.read(); // Read 1 byte sis.read(); // Read another byte System.out.println("Available after reading 2 bytes: " + sis.available()); sis.close(); } catch (IOException e) { e.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result−
Available before reading: 5 Available after reading 2 bytes: 3
Explanation
Combines two streams: "Hello" (5 bytes) and "World" (5 bytes) = 10 bytes total.
After reading 2 bytes, 8 bytes remain.
available() reflects how many bytes are left.
Example - Properly closing a SequenceInputStream after reading
The following example shows the usage of SequenceInputStream close() method.
SequenceInputStreamDemo.java
package com.tutorialspoint; import java.io.ByteArrayInputStream; import java.io.SequenceInputStream; import java.io.IOException; public class SequenceInputStreamDemo { public static void main(String[] args) { try { ByteArrayInputStream input1 = new ByteArrayInputStream("Java".getBytes()); ByteArrayInputStream input2 = new ByteArrayInputStream("Stream".getBytes()); SequenceInputStream sis = new SequenceInputStream(input1, input2); int data; while ((data = sis.read()) != -1) { System.out.print((char) data); } sis.close(); // Closes SequenceInputStream and both ByteArrayInputStreams System.out.println("\nStream closed successfully."); } catch (IOException e) { e.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result−
JavaStream Stream closed successfully.
Explanation
The stream reads from both sources ("JavaStream").
After reading,close() is called to release all underlying resources.
This is good practice in all I/O code.
Example - Read one byte at a time from two combined strings
The following example shows the usage of SequenceInputStream read() method.
SequenceInputStreamDemo.java
package com.tutorialspoint; import java.io.ByteArrayInputStream; import java.io.SequenceInputStream; import java.io.IOException; public class SequenceInputStreamDemo { public static void main(String[] args) { try { ByteArrayInputStream stream1 = new ByteArrayInputStream("ABC".getBytes()); ByteArrayInputStream stream2 = new ByteArrayInputStream("123".getBytes()); SequenceInputStream seqStream = new SequenceInputStream(stream1, stream2); int byteData; while ((byteData = seqStream.read()) != -1) { System.out.print((char) byteData); // Cast int to char for printing } seqStream.close(); } catch (IOException e) { e.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result−
ABC123
Explanation
Combines "ABC" and "123" into one stream.
Reads each byte sequentially and prints it as a character.