Re: nsIInputStream::available

Darin Fisher <[email protected]> Sun, 09 May 2004 17:00:22 -0700
Newsgroups gmane.comp.mozilla.devel.netlib,gmane.comp.mozilla.devel.xpcom
Message-ID <[email protected]>
Christian Biesinger wrote:

> Darin Fisher wrote:
>
>> Call, Read and check what it returns.  If it returns NS_OK and zero 
>> bytes read, then that means EOF.
>
>
> What if I want to find that out without blocking the calling thread?
>
> mind you, I specifically asked about blocking streams :)
> _______________________________________________
> Mozilla-netlib mailing list
> [email protected]
> http://mail.mozilla.org/listinfo/mozilla-netlib


Oh, I missed that detail.  The answer is that you cannot determine if 
EOF has been reached without actually reading from the file (or see 
below regarding nsIAsyncInputStream).

The nsIInputStream interface is designed to behave like the standard 
POSIX API for reading from a file descriptor.  Take a look at "man 2 
read", and you'll see what I mean.

In general, when working with streams it is best to perform the 
operation you want to do (like read or write), and then handle error 
cases that occur.  Believe me: this programming model greatly simplifies 
the implementation of the stream and the code using the stream in cases 
where the stream's data results from some asynchronous operation 
occuring on a background thread.

If the nsIInputStream also implements nsIAsyncInputStream, then you can 
use the AsyncWait method to essentially "poll" the stream to determine 
its state.  I.e., when your OnInputStreamReady method is called, you'll 
know that the stream either has data or has reached an error condition.  
So, calling Read is then guaranteed not to block.

Why do you want to know if the blocking stream has reached end-of-file 
anyways?  What's the application where that is useful?

-Darin