Java - FilterInputStream available() method



Description

The Java FilterInputStream available() method returns an estimate of the number of bytes that can be read from this input stream without blocking by the next invoker of a method for this input stream.

Declaration

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

public int available()

Parameters

NA

Return Value

The method returns an estimate of the number of bytes that can be read.

Exception

IOException − If an I/O error occurs.

Example - Usage of FilterInputStream available() method

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

FilterInputStreamDemo.java

package com.tutorialspoint;

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

public class FilterInputStreamDemo {
   public static void main(String[] args) throws IOException {
      InputStream is = null; 
      FilterInputStream fis = null; 
      int i = 0, j = 0;
      char c;
      
      try {
         // create input streams
         is = new FileInputStream("test.txt");
         fis = new BufferedInputStream(is);
         
         // read till the end of the file
         while((i = fis.read())!=-1) {
         
            // converts integer to character
            c = (char)i;
            
            // prints
            System.out.print("Read: "+c);
            
            // number of bytes available
            j = fis.available();
            
            // prints
            System.out.println("; Available bytes: "+j);
         }
         
      } catch(Exception e) {
         // if any error occurs
         e.printStackTrace();
      } finally {
         // releases any system resources associated with the stream
         if(is!=null)
            is.close();
         if(fis!=null)
            fis.close();
      }
   }
}

Output(assuming test.txt contains ABCDEF)

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

Read: A; Available bytes: 5
Read: B; Available bytes: 4
Read: C; Available bytes: 3
Read: D; Available bytes: 2
Read: E; Available bytes: 1
Read: F; Available bytes: 0

Example - Using FilterInputStream Directly

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

FilterInputStreamDemo.java

package com.tutorialspoint;

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

public class FilterInputStreamDemo {
   public static void main(String[] args) {
      try {
         // Creating FileInputStream and wrapping it in FilterInputStream
         FileInputStream fileInputStream = new FileInputStream("example.txt");
         FilterInputStream filterInputStream = new FilterInputStream(fileInputStream) {}; // Anonymous subclass

         // Checking available bytes before reading
         System.out.println("Available bytes before reading: " + filterInputStream.available());

         // Reading the first byte
         int data = filterInputStream.read();
         System.out.println("First byte read: " + (char) data);

         // Checking available bytes after reading one byte
         System.out.println("Available bytes after reading: " + filterInputStream.available());

         filterInputStream.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output(assuming example.txt contains Hello World!)

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

Available bytes before reading: 12
First byte read: H
Available bytes after reading: 11

Explanation

  • FilterInputStream is instantiated using an anonymous subclass (since it does not provide direct constructors).

  • The available() method is called to check the number of bytes before and after reading one byte.

Example - Wrapping FilterInputStream Around FileInputStream

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

FilterInputStreamDemo.java

package com.tutorialspoint;

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

public class FilterInputStreamDemo {
   public static void main(String[] args) {
      try {
         FileInputStream fileInputStream = new FileInputStream("example.txt");

         // Wrapping FileInputStream inside FilterInputStream
         FilterInputStream filterInputStream = new FilterInputStream(fileInputStream) {}; 

         System.out.println("Total available bytes: " + filterInputStream.available());

         // Reading all available bytes into a byte array
         byte[] buffer = new byte[filterInputStream.available()];
         filterInputStream.read(buffer);

         // Printing the file content
         System.out.println("File content: " + new String(buffer));

         filterInputStream.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output(assuming example.txt contains Hello World!)

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

Total available bytes: 12
File content: Hello World!

Explanation

  • FilterInputStream is used as a wrapper around FileInputStream.

  • The available() method is used to determine how many bytes can be read.

  • The file is read completely into a byte array and printed.

java_io_filterinputstream.htm
Advertisements