Java.io - SPLessons

Java.io SequenceInputStream

Home > Lesson > Chapter 46
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java.io SequenceInputStream

Java.io SequenceInputStream

shape Introduction

The Java.io SequenceInputStream is the class used to combine different input streams. The name itself indicates that the word sequence, to combine two or more different input streams the Java.io SequenceInputStream class is used. The sequence input stream first reads all the bytes of data from first input stream and then from another input stream it reads all bytes of data to combine. To close the sequence input stream then call close() method.

shape Conceptual figure

Class Declaration

shape Declaration

Java.io SequenceInputStream class is declared below: Public class SequenceInputStream extends InputStream

Class Constructors

shape Table

Constructor Description
SequenceInputStream (Enumeration<? extends InputStream>e) The function of this constructor is to create the SequenceInputStream instance and by enumeration which produces the objects represents the run-time of the input stream.
SequenceInputStream (InputStream s1, InputStream s2) The function of this constructor is to create the SequenceInputStream instance and providing the input streams s1, s2 to read bytes from it in order.

Class Methods

shape Table

Method Description
void close () The function of this method is to close the present stream and associated to input stream systems resources are released.
int read(byte[] b, int off, int n) By using this method it reads the data up to ‘n’ bytes from the piped input stream into array of  the bytes.
int read() The function of this method is to read one byte of data from this stream.

Inherited Methods

shape Description

From the following classes, methods are inherited to the reader class.
  • Java.io.Object
  • Java.io.InputStream

shape Examples

Usage of int read(byte[] b, int off, int n) method. Simple1.java [c] import java.io.*; public class Simple1 { public static void main(String[] args) { // create two new strings with 5 characters each String s1 = "Hello"; String s2 = "World"; // create 2 input streams byte[] b1 = s1.getBytes(); byte[] b2 = s2.getBytes(); ByteArrayInputStream is1 = new ByteArrayInputStream(b1); ByteArrayInputStream is2 = new ByteArrayInputStream(b2); // create a new Sequence Input Stream SequenceInputStream sis = new SequenceInputStream(is1, is2); try { // read 10 characters, 5 from each stream for (int i = 0; i < 10; i++) { char c = (char) sis.read(); System.out.print("" + c); } // change line System.out.println(); // close the streams sis.close(); } catch (IOException ex) { ex.printStackTrace(); } } } [/c] Output Following is the result . [c]HelloWorld[/c]

Summary

shape Key Points

  • The Java.io SequenceInputStream is the class used to combine different input streams.
  • The name itself indicates that the word sequence, to combine two or more different input streams the sequence input stream class is used.
  • The sequence input stream first reads all the bytes of data from first input stream and then from another input stream it reads all bytes of data to combine.