Skip to main content

InputStream.available() == 0 as a way to detect if stream is empty ?.

1 reply [Last post]
kumarjayanti
Offline
Joined: 2003-12-10
Points: 0

I need to determine if an InputStream is empty without actually reading the whole stream. Can i depend on available() returning 0.

The javadoc seems to indicate that would be fine, but i may be misinterpreting it

Returns:

an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking or 0 when it reaches the end of the input stream.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
jesdisciple
Offline
Joined: 2008-05-01
Points: 0

Hi Kumar. First off, note that I haven't programmed in Java since a long time. I'm here as a Linux geek to nurture this forum. If you have similar interest in the project, I'd appreciate your help - answering questions and petitioning the site for a moderator. (We have enough spam here to feed all the world's hungry.)

I have to agree with your interpretation; I see no alternative. Specifically, I think a return value of 0 is guaranteed to not be just an estimate.

Returns:

a) an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking, or

b) 0 when it reaches the end of the input stream.