Description

On this document we will be showing a java example on how to use the available() method of BufferedInputStream Class. This method returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this 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.

This method returns the sum of the number of bytes remaining to be read in the buffer (count – pos) and the result of calling the in.available().

Override by:

  • available in class FilterInputStream

Throws:

  • IOException – if this input stream has been closed by invoking its close() method, or an I/O error occurs.

Method Syntax

public int available()
throws IOException

Method Argument

Data Type Parameter Description
N/A N/A N/A

Method Returns

This method returns void.

Compatibility

Requires Java 1.0 and up

Java BufferedInputStream available() Example

Below is a java code demonstrates the use of available() method of BufferedInputStream class. The example presented might be simple however it shows the behaviour of the available().

package com.javatutorialhq.java.examples;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;


/*
 * This example source code demonstrates the use of  
 * available() method of BufferedInputStream class
 */

public class BufferedInputStreamAvailableExample {

	public static void main(String[] args) {		
		
		try {
			// initialize an input stream which in this case
			// we are intended to read a file thus 
			// FileInputStream object suits it best
			FileInputStream fis = new FileInputStream("C:javatutorialhq"
					+ "inputtest_file.txt");
			
			// initialize BufferedInputStream object
			BufferedInputStream buffIs = new BufferedInputStream(fis);
			
			// initialize variables
			int val; //character place holder
			
			
			while((val=buffIs.read())!=-1){
				// convert the value to character
				char result = (char)val;
				System.out.println("Character read:"+result);
				
				// check how many available bytes on the stream
				int available = buffIs.available();
				System.out.println("How many available?:"+available);				
			}
			
			buffIs.close();
			fis.close();
		} catch (FileNotFoundException e) {
			System.out.println("File does not exists");
		} catch (IOException e) {
			System.out.println("IOException occured");
		}		
		

	}

}

Sample Output

Below is the sample output when you run the above example.

Character read:A
How many available?:9
Character read:B
How many available?:8
Character read:C
How many available?:7
Character read:D
How many available?:6
Character read:E
How many available?:5
Character read:a
How many available?:4
Character read:b
How many available?:3
Character read:c
How many available?:2
Character read:d
How many available?:1
Character read:e
How many available?:0