Re: wrong length from nsIInputStream ??
Darin Fisher <[email protected]> Tue, 25 May 2004 09:25:04 -0700
| Newsgroups | gmane.comp.mozilla.devel.netlib,gmane.comp.mozilla.devel.xpcom |
|---|---|
| Message-ID | <[email protected]> |
Marek Lewczuk wrote: > I want to download file (synchronously) and something is wrong with > nsIInputStream.available() - it returns wrong values. > > > var nsIIOService = > Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService); > > var channel = nsIIOService.newChannelFromURI(nsIURI); > var iStream = channel.open(); > var nsIScriptableInputStream = > Components.classes["@mozilla.org/scriptableinputstream;1"].createInstance(Components.interfaces.nsIScriptableInputStream); > > nsIScriptableInputStream.init(iStream); > alert(nsIScriptableInputStream.available()) - return 3450 > alert(nsIScriptableInputStream.available()) - return 12345 > > What is wrong ? > > Thanks in advance. The stream returned by nsIChannel::open implements blocking i/o, but under the hood the data is being buffered as it arrives. So, at first there is no data available in the stream, then as more data arrives the amount available in the buffer increases. In this particular example, the stream actually blocks until it can report a non-zero amount available or some error occurs. You cannot use available as a measure of the total stream length. For that you should use nsIChannel::contentLength, but that is often -1 (for HTTP chunked data) or the wrong number (for HTTP content encoded data). -Darin