nsIInputStream instance.
nsISupports
Last changed in Gecko 2.0 (Firefox 4 / Thunderbird 3.3 / SeaMonkey 2.1)unsigned long available(); |
void close(); |
void init(in nsIInputStream aInputStream); |
string read(in unsigned long aCount); |
ACString readBytes(in unsigned long aCount); |
Return the number of bytes currently available in the stream.
unsigned long available();
None.
The number of bytes.
NS_BASE_STREAM_CLOSEDCloses the stream.
void close();
None.
Wrap the given nsIInputStream with this nsIScriptableInputStream.
nsIScriptableInputStream instance to be reused.void init( in nsIInputStream aInputStream );
aInputStreamnsIInputStream to be wrapped.Read data from the stream.
Warning: If the data contains a null byte, then this method will return a truncated string.
string read( in unsigned long aCount );
aCountThe data read as a string, which will be an empty string if the stream is at EOF.
NS_ERROR_NOT_INITIALIZEDinit() was not called.NS_BASE_STREAM_CLOSEDNS_BASE_STREAM_WOULD_BLOCKnsIInputStream.isNonBlocking() returns true.Read data from the stream, including null bytes.
ACString readBytes( in unsigned long aCount );
aCountThe data from the stream, which will be an empty string if EOF has been reached.
NS_ERROR_FAILURENS_BASE_STREAM_WOULD_BLOCKThis interface provides JavaScript with a way to read ASCII text from a nsIInputStream. However, it does not address the problem of reading arbitrary binary data from a stream. For binary input see nsIBinaryInputStream.
Note: Starting in Gecko 2.0, you can use the NetUtils.jsm JavaScript code module and its readInputStreamToString() method to read arbitrary binary data into a JavaScript string.
The nsScriptableInputStream component implements this interface.
This interface was frozen for Gecko 1.2. From Gecko 2.0 interfaces are no longer frozen.
This example here uses the read function. It takes a input stream from the zip reader and outputs the text in it. Full example using the zip interfaces is seen here: List contents of XPI and read file contents. Remember: The nsIScriptableInputStream has a contract where `init` should only be called once, and should always be closed. Excerpt of the stream for this stream article is here, this is only an excerpt so cannot copy paste this code into scratchpad.
var {classes: Cc, interfaces: Ci, results: Cr, Constructor: CC, utils: Cu } = Components;
var ScriptableInputStream = CC("@mozilla.org/scriptableinputstream;1", "nsIScriptableInputStream", "init");
let entry = this.getEntry(name);
let stream = new ScriptableInputStream(this.getInputStream(name));
try {
// Use readBytes to get binary data, read to read a (null-terminated) string
let contents = stream.readBytes(entry.realSize);
}
finally {
stream.close();
}
Remember: The nsIScriptableInputStream has a contract where `init` should only be called once, and should always be closed.
var ScriptableInputStream = CC("@mozilla.org/scriptableinputstream;1", "nsIScriptableInputStream", "init");
var blah = {
data: '',
onStartRequest: function (aRequest, aContext) {
this.data = '';
},
onDataAvailable: function(request, context, inputStream, offset, count) {
var scriptStream = new ScriptableInputStream(inputStream);
this.data += scriptStream.read(count);
scriptStream.close();
}
};