Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
res7cxbi@verizon.net - 29 Jan 2006 10:00 GMT
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."
ozgwei - 29 Jan 2006 12:07 GMT
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.
Roedy Green - 29 Jan 2006 12:17 GMT
>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.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Jim Korman - 29 Jan 2006 22:13 GMT
>>And what does available()'s method description in inputstream's javadoc
>>mean?: "Returns the number of bytes that can be read (or skipped over)
[quoted text clipped - 4 lines]
>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
John C. Bollinger - 29 Jan 2006 21:18 GMT
> 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.

Signature

John Bollinger
jobollin@indiana.edu

Darryl L. Pierce - 30 Jan 2006 14:50 GMT
> 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.

Signature

Darryl L. Pierce <mcpierce@gmail.com>
Homepage: http://mcpierce.multiply.com/
"McVeigh's lawyer got him the death penalty, which, quite frankly,
I could have done." - Jon Stewart



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.