Home » Java programming language

Java SequenceInputStream available() Method with Example

SequenceInputStream Class available() method: Here, we are going to learn about the available() method of SequenceInputStream Class with its syntax and example.
Submitted by Preeti Jain, on April 27, 2020

SequenceInputStream Class available() method

  • available() method is available in java.io package.
  • available() method is used to return an approximate of the number of available bytes that can be read from the present underlying input stream without blocking by the next call of a method for the present underlying SequenceInputStream.
  • available() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • available() method may throw an exception at the time of returning available bytes.
    IOException: This exception may throw when getting any input/output error while performing.

Syntax:

    public int available();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of the method is int, it returns the number of bytes left that can be read from the present underlying input stream without blocking. It may return 0 when this stream is closed by its close() method.

Example:

// Java program to demonstrate the example 
// of int available() method of
// SequenceInputStream

import java.io.*;

public class AvailableOfSIS {
    public static void main(String[] args) {
        String str1 = "Java";
        String str2 = "Programming..";

        // Create two byte array
        byte[] b_arr1 = str1.getBytes();
        byte[] b_arr2 = str2.getBytes();

        // Instantiates two ByteArrayInputStream
        ByteArrayInputStream bais_1 = new ByteArrayInputStream(b_arr1);
        ByteArrayInputStream bais_2 = new ByteArrayInputStream(b_arr2);

        // Instantiates SequenceInputStream
        SequenceInputStream se_stm = new SequenceInputStream(bais_1, bais_2);

        try {
            // By using available() method is to
            // return the number of available bytes
            // to be read in bais1
            int avail_bytes = se_stm.available();
            System.out.println("se_stm.available(): " + avail_bytes);

            // Loop to read available input 
            for (int i = 0; i < 15; i++) {
                // By using read() method is to read integer and
                // convert it into char
                char ch = (char) se_stm.read();
                System.out.print("se_stm.read(): " + ch);
                System.out.println();
            }

            // By using close() method is to
            // close all the streams
            bais_1.close();
            bais_1.close();
            se_stm.close();
            System.out.println("Streams Shutdown.....");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

Output

se_stm.available(): 4
se_stm.read(): J
se_stm.read(): a
se_stm.read(): v
se_stm.read(): a
se_stm.read(): P
se_stm.read(): r
se_stm.read(): o
se_stm.read(): g
se_stm.read(): r
se_stm.read(): a
se_stm.read(): m
se_stm.read(): m
se_stm.read(): i
se_stm.read(): n
se_stm.read(): g
Streams Shutdown.....






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.