Java - SequenceInputStream available() method



Description

The Java SequenceInputStream available() 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. The next invocation might be the same thread or another thread. A single read or skip of this many bytes will not block, but may read or skip fewer bytes.

Declaration

Following is the declaration for java.io.SequenceInputStream.available() method.

public int available()

Parameters

NA

Return Value

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 or 0 if this input stream has been closed by invoking its close() method.

Exception

  • IOException − If an I/O error occurs.

Example - Usage of SequenceInputStream available() method

The following example shows the usage of SequenceInputStream available() method.

SequenceInputStreamDemo.java

package com.tutorialspoint;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.SequenceInputStream;

public class SequenceInputStreamDemo {
   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 {
         // determine how many bytes are available in the first stream
         System.out.println(sis.available());
         
         // read 10 characters, 5 from each stream
         for (int i = 0; i < 10; i++) {
            char c = (char) sis.read();
            System.out.print(c);
         }

         // close the stream
         sis.close();

      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

5
HelloWorld

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 - Using available() after switching streams internally

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("ABC".getBytes());
         ByteArrayInputStream input2 = new ByteArrayInputStream("12345".getBytes());

         SequenceInputStream sis = new SequenceInputStream(input1, input2);

         // Read all from first stream (3 bytes)
         for (int i = 0; i < 3; i++) {
            sis.read(); // consumes input1
         }

         // Trigger switch to second stream
         sis.read(); // reads first byte from input2

         // Now input2 is active, available() reflects its remaining bytes
         System.out.println("Available after switching streams: " + 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 after switching streams: 4

Explanation

  • First stream (ABC) has 3 bytes, second stream (12345) has 5 bytes.

  • After reading 3 bytes, the stream automatically switches to the second one.

  • available() now reports the bytes available in the second stream (5).

java_io_sequenceinputstream.htm