Signup/Sign In

Java FilterInputStream available() method

In this tutorial, we will learn about the available() method of the FilterInputStream class in Java. This method is used to return the total number of available bytes that can be read from the current Filter Input Stream without interrupted by the next invoke of this method, the invoker can be the same thread or another thread.

Syntax

This is the syntax declaration of this method. It does not accept any parameter. It returns the total number of bytes that can be read from the given input stream.

public int available() throws IOException  

Example 1: Find Available bytes in Java

In the following example, we are using available() method to get available bytes to read from the file. Since the given file output.txt contains 6 bytes present to read it will give output accordingly.

package com.studytonight;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;

public class Studytonight {
   public static void main(String[] args) throws IOException {
      InputStream is = null; 
      FilterInputStream st = null; 
      int i = 0, j = 0;
      char c;
      
      try {
         
         is = new FileInputStream("E:/studytonight/output.txt");
         st = new BufferedInputStream(is);
                  
         while((i = st.read())!=-1) {
                    
            c = (char)i;
                        
            System.out.print("Read: "+c);
                        
            j = st.available();
                        
            System.out.println("; Available bytes: "+j);
         }
         
      } catch(Exception e) {
         
         e.printStackTrace();
      } finally {
         
         if(is!=null)
            is.close();
         if(st!=null)
            st.close();
      }
   }
}

Output.txt

CURIOUS


Read: C; Available bytes: 6
Read: U; Available bytes: 5
Read: R; Available bytes: 4
Read: I; Available bytes: 3
Read: O; Available bytes: 2
Read: U; Available bytes: 1
Read: S; Available bytes: 0

Example 1: Find Available bytes in Java

In the following example, we are using available() method to get available bytes to read from the file. Since the given file output.txt contains 7 bytes present to read it will give output accordingly.

import java.io.*;

public class Studytonight {
 public static void main(String[] args) throws Exception {
  FileInputStream st = null;
  FilterInputStream std = null;
  int count = 0;

  try {
   
   st = new FileInputStream("E:\Studytonight\output.txt");
   std = new BufferedInputStream(st);
   
   while ((count = st.read()) != -1) {

    int avail_bytes = st.available();
    
    byte b = (byte) count;

    System.out.print("st.available(): " + avail_bytes);
    System.out.println(" : " + "byte: " + b);
   }
  } catch (Exception ex) {
   System.out.println(ex.toString());
  } finally {
   
   if (st != null) {
    st.close();

    if (std != null) {
     std.close();
    }
   }
  }
 }
}


st.available(): 6 : byte: 100
st.available(): 5 : byte: 74
st.available(): 4 : byte: 118
st.available(): 3 : byte: 32
st.available(): 2 : byte: 46
st.available(): 1 : byte: 46
st.available(): 0 : byte: 46

mail Conclusion

In this tutorial, we learned about the available() method of the FilterInputStream class in Java, which returns the total number of remaining bytes that are available to be read from the given File Input Stream. It is a non-static method available in the java.io package and 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.



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.