The NetUtil.jsm
JavaScript code module provides easy-to-use APIs for performing common network related tasks.
To use these utilities, you first need to import the code module into your JavaScript scope:
Components.utils.import("resource://gre/modules/NetUtil.jsm");
Once you've imported the module, you can then use its methods.
|
void asyncFetch(aSource, aCallback) |
|
|
String readInputStreamToString(aInputStream, aCount, aOptions) |
Attribute | Type | Description |
ioService |
nsIIOService |
Returns a reference to nsIIOService . |
The asyncCopy()
method performs a simple asynchronous copy of data from a source input stream to a destination output stream. Both streams are automatically closed when the copy operation is completed.
nsIAsyncStreamCopier asyncCopy( aSource, aSink, aCallback );
aSource
nsIInputStream
based object.aSink
nsIOutputStream
based object.aCallback
nsresult
status code for the copy operation.Returns the nsIAsyncStreamCopier
object that is handling the copy.
This method throws an exception with the message "Must have a source and a sink" if either aSource
or aSink
is null.
This example writes a string to a file; it comes from the test suite for the NetUtil module.
var file = Cc["@mozilla.org/file/directory_service;1"]. getService(Ci.nsIProperties). get("TmpD", Ci.nsIFile); file.append("test-file.tmp"); file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); // Then, we need an output stream to our output file. var ostream = Cc["@mozilla.org/network/file-output-stream;1"]. createInstance(Ci.nsIFileOutputStream); ostream.init(file, -1, -1, 0); // Finally, we need an input stream to take data from. const TEST_DATA = "this is a test string"; let istream = Cc["@mozilla.org/io/string-input-stream;1"]. createInstance(Ci.nsIStringInputStream); istream.setData(TEST_DATA, TEST_DATA.length); NetUtil.asyncCopy(istream, ostream, function(aResult) { if (!Components.isSuccessCode(aResult)) { // an error occurred! } })
The asyncFetch()
method opens an input source, asynchronously, and gives an nsIInputStream
containing the data obtained to the callback provided. The callback may then consume data from the input stream passed to the callback.
Note: Prior to Gecko 2, the input source was required to be specified as an nsIChannel
.
Note: Support for specifying an nsIInputStream
as the input source was added in Gecko 5.
asyncFetch( aSource, aCallback );
aSource
nsIURI
, nsIFile
, nsIChannel
, nsIInputStream
or as a string specifying the URL to open.aCallback
nsIInputStream
), an nsresult
status code for the open operation, and a reference to the nsIRequest
object.This method throws an exception with the message "Must have a source and a callback" if either aSource
or aCallback
is null.
This example shows the basic code consumers would need to implement to be correct.
NetUtil.asyncFetch(channel, function(aInputStream, aResult) { // Check that we had success. if (!Components.isSuccessCode(aResult)) { // Handle error return; } // Consume the input stream. });
nsIChannel
as the input source, and its notification callbacks have already been set, callers are responsible for implementing nsIBadCertListener
and nsISSLErrorListener
.Creates a new channel for the specified URI string, nsIURI
, or nsIFile
.
nsIChannel newChannel( aWhatToLoad, [optional] aOriginCharset, [optional] aBaseURI );
An nsIChannel
allowing access to the specified data source.
aWhatToLoad
nsIURI
or nsIFile
object. The returned method will allow access to the specified data.aOriginCharset
aWhatToLoad
as a string; this is ignored if aWhatToLoad
is not a string. Defaults to UTF-8 if not provided.aBaseURI
nsIURI
. Only used if aWhatToLoad
is a string.The newURI()
method creates a new nsIURI
. This method wraps the nsIIOService.newURI()
.
Note: Starting in Gecko 2, this method supports specifying an nsIFile
as the target of the URI. In previous versions of Gecko, you must use a string to specify the URI.
nsIURI newURI( aTarget, [optional] aOriginCharset, [optional] aBaseURI );
aTarget
nsIFile
.aOriginCharset
aBaseURI
nsIURI
.Returns the nsIURI
object representing the specified target.
This method throws an exception with the message "Must have a non-null string spec or nsIFile object" and result code NS_ERROR_INVALID_ARG
if aSpec
is null.
Reads a given number of bytes from an input stream, returning a string containing those bytes. This works even if some of those bytes are zeros, instead of terminating the string at the first zero byte.
String readInputStreamToString( aInputStream, aCount, aOptions );
aInputStream
aCount
nsIInputStream.available()
to get the number of bytes currently available.aOptions
The aOptions
parameter is a JavaScript object which can have any or all of the following fields:
Field name | Description |
charset |
The character encoding to use when interpreting the input stream. If you don't provide this, the input won't be decoded at all and the raw bytes will be returned. |
replacement |
A character with which to replace any invalid byte sequences in the input stream. If you don't specify a replacement character, any invalid sequences will result in NS_ERROR_ILLEGAL_INPUT being thrown. This option only has effect if charset is as well. |
A string containing the bytes read from the input stream.
NS_ERROR_INVALID_ARG
nsIInputStream
.NS_BASE_STREAM_WOULD_BLOCK
NS_ERROR_FAILURE
aCount
bytes available to read from the stream.NS_ERROR_ILLEGAL_INPUT
aOptions.replacement
.