Class SequenceInputStream in Java

digitization, transformation, binary

Class SequenceInputStream in Java

The SequenceInputStream class allows an application to combine several input streams (a series of InputStream objects) serially and make them appear as if they were a single input stream( to be seamlessly concatenated into one stream.).

Each input stream is read from in turn in a specific order until it reaches the end of the stream. The SequenceInputStream class then closes that stream and automatically switches to the next InputStream.

Ads code goes here

The class structure of the SequenceInputStream is given as:

public class java.io.SequenceInputStream extends java.io.InputStream{
//constructors
public SequenceInputStream(Enumeration e);
//constructs a new SequenceInputStream object. It is initialized to the specified enumeration of InputStream.
//Each object is the enumeration must be a valid Input Stream.
public SequenceInputStream(InputStream s1,InputStream s2);
//constructs a new SequenceInputStream object. It is initialized to read first from input stream s1 and then 
//from InputStream s2
//methods
public void close();
public int read();
public int read(byte b[],int offSet,int length);
} 

In addition,all methods may throw an IOException if an IO error occurs apart from their regular Exceptions.

The details of the class structure are given as follows:

public SequenceInputStream(Enumeration e);

public SequenceInputStream(Enumeration e) constructor creates a SequenceInputStream that reads from each of the InputStream objects in the given Enumeration. Each object in the Enumeration must be an InputStream.

Parameter
e – An Enumeration of input streams.

public SequenceInputStream(InputStream s1, InputStream s2);

public SequenceInputStream(InputStream s1, InputStream s2) constructor creates a SequenceInputStream that reads first from s1 and then from s2.

READ  Class PrintStream in Java

Parameter
s1 – An input stream.
s2 – Another input stream.

public int available(); 

public int available(); method returns the number of bytes that can be read without having to wait for more data to become available. The method returns the result of calling available() on the current stream. If the end of the final stream is encountered, the method returns 0.

This method returns the number of bytes that can be read without blocking, or 0 if the end of the final stream is encountered.

public void close();

public void close() method closes the stream and releases the system resources that are associated with it. The method closes all the InputStream objects attached to this object.

public int read();

public int read() method reads the next byte of data from the current stream. When the end of the current stream is encountered, that stream is closed, and the first byte of the next InputStream is read.

If there are no more InputStream objects in the SequenceInputStream, -1 is returned to
signify the end of the SequenceInputStream. The method blocks until the byte is read, the
end of the final stream is encountered, or an exception is thrown.

This method returns the next byte of data or -1 if the end of the final stream is encountered.

public int read(byte[] buf, int pos, int len);

public int read(byte[] buf, int pos, int len) method reads up to len bytes of input from the current stream into the given array starting at index off. When the end of the current stream is encountered, that stream is closed, and bytes are read from the next InputStream. If there are no more InputStream objects in the SequenceInputStream, -1 is returned to signify the end of the SequenceInputStream.

READ  How to Work with AudioClip in Java?

The method blocks until there is some data available.

This method returns the actual number of bytes read or -1 if the end of the final stream is encountered immediately.

Parameter
buf – An array of bytes to be filled from the stream.
off – An offset into the byte array.
len – The number of bytes to read.

Apart from these SequenceInputStream class also has inherited methods from class- Object. They are as follows:

  • clone()
  • finalize()
  • hashCode()
  • notifyAll()
  • wait()
  • wait(long, int)
  • equals(Object)
  • getClass()
  • notify()
  • toString()
  • wait(long)

SequenceInputStream class also has inherited methods from InputStream. They are as follows:

  • markSupported()
  • skip(long)
  • mark(int)
  • reset()

Share and Enjoy !

Leave a Comment

Your email address will not be published. Required fields are marked *