Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 516591 of io/byte-streams

  • Revision slug: Mozilla/Add-ons/SDK/Low-Level_APIs/io_byte-streams
  • Revision title: io/byte-streams
  • Revision id: 516591
  • Created:
  • Creator: backy0175
  • Is current revision? No
  • Comment Add sample code

Revision Content

Experimental

Provides streams for reading and writing bytes.

Globals

Constructors

ByteReader(inputStream)

Creates a binary input stream that reads bytes from a backing stream.

Parameters

inputStream : stream
The backing stream, an nsIInputStream.

ByteWriter(outputStream)

Creates a binary output stream that writes bytes to a backing stream.

Parameters

outputStream : stream
The backing stream, an nsIOutputStream.

Functions

ByteReader

Methods

close()

Closes both the stream and its backing stream. If the stream is already closed, an exception is thrown.

read(numBytes)

Reads a string from the stream. If the stream is closed, an exception is thrown.

Parameters

numBytes : number
The number of bytes to read. If not given, the remainder of the entire stream is read.

Returns

string : A string containing the bytes read. If the stream is at the end, returns the empty string.

Properties

closed

True if the stream is closed.

Sample

function readBinaryDataFromFile (filename) {

  var fileIO = require("sdk/io/file");

  var data = null;

  if (fileIO.exists(filename)) {
    var ByteReader = fileIO.open(filename, "rb");
    if (!ByteReader.closed) {
      data = ByteReader.read();
      ByteReader.close();
    }
  }

  return data;
};

ByteWriter

Methods

close()

Closes both the stream and its backing stream. If the stream is already closed, an exception is thrown.

write(str)

Writes a string to the stream. If the stream is closed, an exception is thrown.

Parameters

str : string
The string to write.

Properties

closed

True if the stream is closed.

Sample

function writeBinaryDataToFile (data, filename) {

  var fileIO = require("sdk/io/file");

  var ByteWriter = fileIO.open(filename, "wb");
  if (!ByteWriter.closed) {
    ByteWriter.write(data);
    ByteWriter.close();
  }

};

Revision Source

<div class="note">
 <p>Experimental</p>
</div>
<p><span class="seoSummary">Provides streams for reading and writing bytes.</span></p>
<h2 id="Globals">Globals</h2>
<h3 id="Constructors">Constructors</h3>
<h4 class="addon-sdk-api-name" id="ByteReader(inputStream)"><code>ByteReader(inputStream)</code></h4>
<p>Creates a binary input stream that reads bytes from a backing stream.</p>
<h5 id="Parameters">Parameters</h5>
<p><strong>inputStream : stream</strong><br />
 The backing stream, an <a href="https://mxr.mozilla.org/mozilla-central/
source/xpcom/io/nsIInputStream.idl"><code>nsIInputStream</code></a>.</p>
<h4 class="addon-sdk-api-name" id="ByteWriter(outputStream)"><code>ByteWriter(outputStream)</code></h4>
<p>Creates a binary output stream that writes bytes to a backing stream.</p>
<h5 id="Parameters">Parameters</h5>
<p><strong>outputStream : stream</strong><br />
 The backing stream, an <a href="https://mxr.mozilla.org/mozilla-central/
source/xpcom/io/nsIOutputStream.idl"><code>nsIOutputStream</code></a>.</p>
<h3 id="Functions">Functions</h3>
<h2 id="ByteReader">ByteReader</h2>
<h3 id="Methods">Methods</h3>
<h4 class="addon-sdk-api-name" id="close()"><code>close()</code></h4>
<p>Closes both the stream and its backing stream. If the stream is already closed, an exception is thrown.</p>
<h4 class="addon-sdk-api-name" id="read(numBytes)"><code>read(numBytes)</code></h4>
<p>Reads a string from the stream. If the stream is closed, an exception is thrown.</p>
<h5 id="Parameters">Parameters</h5>
<p><strong>numBytes : number</strong><br />
 The number of bytes to read. If not given, the remainder of the entire stream is read.</p>
<h5 id="Returns">Returns</h5>
<p><strong>string</strong> : A string containing the bytes read. If the stream is at the end, returns the empty string.</p>
<h3 id="Properties">Properties</h3>
<h4 class="addon-sdk-api-name" id="closed"><code>closed</code></h4>
<p>True if the stream is closed.</p>
<h3 id="Sample">Sample</h3>
<pre class="brush: js">
function readBinaryDataFromFile (filename) {

  var fileIO = require("sdk/io/file");

  var data = null;

  if (fileIO.exists(filename)) {
    var ByteReader = fileIO.open(filename, "rb");
    if (!ByteReader.closed) {
      data = ByteReader.read();
      ByteReader.close();
    }
  }

  return data;
};</pre>
<h2 id="ByteWriter">ByteWriter</h2>
<h3 id="Methods">Methods</h3>
<h4 class="addon-sdk-api-name" id="close()"><code>close()</code></h4>
<p>Closes both the stream and its backing stream. If the stream is already closed, an exception is thrown.</p>
<h4 class="addon-sdk-api-name" id="write(str)"><code>write(str)</code></h4>
<p>Writes a string to the stream. If the stream is closed, an exception is thrown.</p>
<h5 id="Parameters">Parameters</h5>
<p><strong>str : string</strong><br />
 The string to write.</p>
<h3 id="Properties">Properties</h3>
<h4 class="addon-sdk-api-name" id="closed"><code>closed</code></h4>
<p>True if the stream is closed.</p>
<h3 id="Sample">Sample</h3>
<pre class="brush: js">
function writeBinaryDataToFile (data, filename) {

  var fileIO = require("sdk/io/file");

  var ByteWriter = fileIO.open(filename, "wb");
  if (!ByteWriter.closed) {
    ByteWriter.write(data);
    ByteWriter.close();
  }

};</pre>
Revert to this revision