What's the Importance of the avaliable() method in InputStream?

R

res7cxbi

What's the Importance of the avaliable() method in InputStream? Why
would i use it?

And what does available()'s method description in inputstream's javadoc
mean?: "Returns the number of bytes that can be read (or skipped over)
from this input stream without blocking by the next caller of a method
for this input stream."
 
O

ozgwei

Note that in the implementation of InputStream, this method always
return 0. It is intended for BufferedInputStream to return the
remaining bytes in the buffer.
 
R

Roedy Green

And what does available()'s method description in inputstream's javadoc
mean?: "Returns the number of bytes that can be read (or skipped over)
from this input stream without blocking by the next caller of a method
for this input stream."

you might decide it is not worth your while doing a read, you have
other fish to fry. You will come back later and try again when there
is a big enough batch worth the overhead of a read to the OS.
 
J

John C. Bollinger

What's the Importance of the avaliable() method in InputStream? Why
would i use it?

Not very important. You probably wouldn't want to use it. If you don't
understand well exactly what it does (and doesn't) do and when and why
you might want that, then save yourself grief and pretend that the
method doesn't exist. It is rarely used by people who do know and
understand all that.
And what does available()'s method description in inputstream's javadoc
mean?: "Returns the number of bytes that can be read (or skipped over)
from this input stream without blocking by the next caller of a method
for this input stream."

It means that the next read on the input stream will retrieve at least
that number of bytes, provided that that number or more are requested.
Furthermore, the read will not block if the number of bytes returned by
available() is greater than zero. Note, however, that by the time the
read is actually performed, there may be more bytes available, so a
return value of zero doesn't necessarily mean that the next read will
block. Note also that available() will, perforce, return zero when the
end of the stream has been detected, and that a read on the stream will
not block in that case, either. A common mistake is to assume that
available() returning zero is a definite signal of end-of-stream; that
assumption is false.

Contrary to ozgwei's comments, available is for more than just
BufferedInputStream, but he is right about how BufferedInputStream
implements the method. Note that java.io.InputStream itself is abstract.
 
J

Jim Korman

you might decide it is not worth your while doing a read, you have
other fish to fry. You will come back later and try again when there
is a big enough batch worth the overhead of a read to the OS.

One place that I found it very useful is when using a
SerialPortEventListener for "interrupt" driven serial I/O.
The event only tells you that data is available, not how much.
The Listener code can check InputStream.available(), read that
much and quickly exit.

Jim
 
D

Darryl L. Pierce

What's the Importance of the avaliable() method in InputStream? Why
would i use it?

It tells you the number of bytes in the current input buffer, as opposed to
the total number of bytes waiting in the stream itself.
And what does available()'s method description in inputstream's javadoc
mean?: "Returns the number of bytes that can be read (or skipped over)
from this input stream without blocking by the next caller of a method
for this input stream."

It means that available() tells you how many bytes are available for reading
without requiring the machine go to the network connection to fetch more
data. IOW, it's the number of bytes already available in the buffer.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top