In InputStreams, read() calls are said to be "blocking" method calls. That means that if no data is available at the time of the method call, the method will wait for data to be made available.

The available() method tells you how many bytes can be read until the read() call will block the execution flow of your program. On most of the input streams, all call to read() are blocking, that's why available returns 0 by default.

However, on some streams (such as BufferedInputStream, that have an internal buffer), some bytes are read and kept in memory, so you can read them without blocking the program flow. In this case, the available() method tells you how many bytes are kept in the buffer.

Answer from Vivien Barousse on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › io › InputStream.html
InputStream (Java Platform SE 7 )
It is never correct to use the ... an IOException if this input stream has been closed by invoking the close() method. The available method for class InputStream always returns 0....
🌐
Tutorialspoint
tutorialspoint.com › java › io › fileinputstream_available.htm
Java - FileInputStream available() method
The Java FileInputStream available() method returns the number of bytes that can be read from the file input stream without blocking. It does not return the total file size but rather the number of remaining unread bytes in the stream.
🌐
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › io › InputStream.html
InputStream (Java SE 11 & JDK 11 )
January 20, 2026 - It is never correct to use the ... if this input stream has been closed by invoking the close() method. The available method of InputStream always returns 0....
🌐
IncludeHelp
includehelp.com › java › inputstream-available-method-with-example.aspx
Java InputStream available() Method with Example
April 3, 2020 - // Java program to demonstrate the example // of int available() method of InputStream import java.io.*; public class AvailableOfIS { public static void main(String[] args) throws Exception { InputStream is_stm = null; int val = 0; try { // Instantiates FileInputStream is_stm = new FileInputStream("D:\\includehelp.txt"); // Loop to read until available // bytes left while ((val = is_stm.read()) != -1) { // By using available() method is to // return the available bytes to be read int avail_bytes = is_stm.available(); // Display corresponding byte value byte b = (byte) val; // Display value of
🌐
Oracle
docs.oracle.com › en › java › javase › 21 › docs › api › java.base › java › io › InputStream.html
InputStream (Java SE 21 & JDK 21)
January 20, 2026 - It is never correct to use the ... close() method. ... This method should be overridden by subclasses. ... The available method of InputStream always returns 0....
🌐
Tabnine
tabnine.com › home page › code › java › java.io.inputstream
Java Examples & Tutorials of InputStream.available (java.io) | Tabnine
Payload payload = blob.getPayload(); InputStream is = payload.getInput(); File tempFile = new File(downloadDir, name + ".tmp"); Timer.Context downloadContext = downloadTimer.time(); try { byte[] buf = new byte[BUF_SIZE]; while (read < length) { int avail = Math.min(is.available(), BUF_SIZE); if (avail < 0) { try { Thread.sleep(100); } catch (Exception ex) {} } else { int readLength = is.read(buf); read += readLength; out.write(buf, 0, readLength); File permFile = new File(downloadDir, name); if (tempFile.renameTo(permFile)) { notifyListeners(permFile); } else { throw new IOException("Could not rename file"); origin: alibaba/canal ·
🌐
Jenkov
jenkov.com › tutorials › java-io › inputstream.html
Java InputStream
The Java InputStream class contains a method called readAllBytes() (since Java 9). This method reads all the bytes available in the InputStream and returns a single byte array with the bytes in. This method is useful if you need to read all bytes from a file via a FileInputStream into a byte array.
Find elsewhere
🌐
Usc
physics.usc.edu › java › api › java.io.InputStream.html
Class java.io.InputStream
InputStream() available() Returns the number of bytes that can be read without blocking. close() Closes the input stream. mark(int) Marks the current position in the input stream. markSupported() Returns a boolean indicating whether or not this stream type supports mark/reset.
🌐
Android Developers
developer.android.com › api reference › inputstream
InputStream | API reference | Android Developers
February 13, 2026 - Skip to main content · English · Deutsch · Español – América Latina · Français · Indonesia · Polski · Português – Brasil · Tiếng Việt · 中文 – 简体
🌐
Oracle
docs.oracle.com › en › java › javase › 17 › docs › api › java.base › java › io › InputStream.html
InputStream (Java SE 17 & JDK 17)
January 20, 2026 - It is never correct to use the ... if this input stream has been closed by invoking the close() method. The available method of InputStream always returns 0....
🌐
Programiz
programiz.com › java-programming › inputstream
Java InputStream (With Example)
In order to create an InputStream, we must import the java.io.InputStream package first.
🌐
Coderanch
coderanch.com › t › 612110 › java › method-returning
available() method returning zero (Sockets and Internet Protocols forum at Coderanch)
Hi I am the first time on the forum and I am having a problem with retrieving the data from the InputStream. I have a Client program that sends the data to the ISeries Server and get a response and display it on a Web page. The problem is that when I run the program interactively, the available method returns zero, but when run in a debug mode, the available() returns the expected number of bytes.
🌐
Oracle
docs.oracle.com › javase › 10 › docs › api › java › io › InputStream.html
InputStream (Java SE 10 & JDK 10 )
It is never correct to use the ... an IOException if this input stream has been closed by invoking the close() method. The available method for class InputStream always returns 0....
🌐
Tutorialspoint
tutorialspoint.com › home › java/io › java inputstream available method
Java - InputStream available() method
February 13, 2026 - Reads one byte, then calls available() again to show the remaining bytes. The following example shows the usage of Java InputStream available() method.
🌐
GeeksforGeeks
geeksforgeeks.org › java › fileinputstream-available-method-in-java-with-examples
FileInputStream available() Method in Java with Examples - GeeksforGeeks
December 29, 2021 - The available() method of FileInputStream class is used to return the estimated number of remaining bytes that can be read from the input stream without blocking. This method returns the number of bytes remaining to read from the file.
🌐
GitHub
github.com › AdoptOpenJDK › openjdk-jdk11 › blob › master › src › java.base › share › classes › java › io › InputStream.java
openjdk-jdk11/src/java.base/share/classes/java/io/InputStream.java at master · AdoptOpenJDK/openjdk-jdk11
return new InputStream() { private volatile boolean closed; · private void ensureOpen() throws IOException { if (closed) { throw new IOException("Stream closed"); } } · @Override · public int available () throws IOException { ensureOpen(); return 0; } ·
Author   AdoptOpenJDK
🌐
Chasethedevil
chasethedevil.github.io › post › non-blocking-with-traditional-java-io---on-the-use-of-inputstream.available-and-thread.sleep
Non Blocking with Traditional Java IO - On the Use of InputStream.available() and Thread.sleep() · Chase the Devil
Thu, Jun 8, 2006 Some time ago I did quite a lot of IO in Java and I yet did not see this way of reading a InputStream from a Socket: This comes from a piece of code from an example of JSch , a good ssh client in java. A work collegue had the bad idea to remove the Thread.sleep call and was struggling with why it would randomly work.The way I would have done it is the following: This has the advantage of being more readable and having less secret spices in it. In the first code, the call to available() is non blocking, meaning that without the Thread.sleep(), there will never be the time for the socket buffer to fill up.
🌐
Studytonight
studytonight.com › java-file-io › java-fileinputstream-available-method
Java FileInputStream available() Method - Studytonight
Java FileInputStream available() method: This method is used to return the number of remaining bytes to be read from the current input stream, without blocking