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
1int 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.

2void close()

This method closes this input stream and releases any system resources associated with the stream.

3int read()

This method reads the next byte of data from this input stream.

4int 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.