-1

Directly from the API:

public int available() throws IOException

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.

Note that while some implementations of InputStream will return the total number of bytes in the stream, many will not. It is never correct to use the return value of this method to allocate a buffer intended to hold all data in this stream.

A subclass' implementation of this method may choose to throw an IOException if this input stream has been closed by invoking the close() method.

The available method for class InputStream always returns 0.

This method should be overridden by subclasses.

I cannot quite grasp the concept of a possible usage of this method. Can anybody make a real life example about it? Thanks in advance.

Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186
Rollerball
  • 12,618
  • 23
  • 92
  • 161
  • I don't use it in general as it's exact behaviour is dependant on context and OS and other things you can't always control. If you can avoid using I would. – Peter Lawrey Jun 21 '13 at 12:09

1 Answers1

1

I've been searching for a real life example for this for 20+ years.

How it works depends on the stream. For some streams, it doesn't work at all. For buffered streams, it works by returning the amount unread in the buffer plus the available() of the nested stream. For sockets and files, it executes a system call.

user207421
  • 305,947
  • 44
  • 307
  • 483