addon-sdk/source/lib/sdk/io/byte-streams.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 "use strict";
michael@0 6
michael@0 7 module.metadata = {
michael@0 8 "stability": "experimental"
michael@0 9 };
michael@0 10
michael@0 11 exports.ByteReader = ByteReader;
michael@0 12 exports.ByteWriter = ByteWriter;
michael@0 13
michael@0 14 const {Cc, Ci} = require("chrome");
michael@0 15
michael@0 16 // This just controls the maximum number of bytes we read in at one time.
michael@0 17 const BUFFER_BYTE_LEN = 0x8000;
michael@0 18
michael@0 19 function ByteReader(inputStream) {
michael@0 20 const self = this;
michael@0 21
michael@0 22 let stream = Cc["@mozilla.org/binaryinputstream;1"].
michael@0 23 createInstance(Ci.nsIBinaryInputStream);
michael@0 24 stream.setInputStream(inputStream);
michael@0 25
michael@0 26 let manager = new StreamManager(this, stream);
michael@0 27
michael@0 28 this.read = function ByteReader_read(numBytes) {
michael@0 29 manager.ensureOpened();
michael@0 30 if (typeof(numBytes) !== "number")
michael@0 31 numBytes = Infinity;
michael@0 32
michael@0 33 let data = "";
michael@0 34 let read = 0;
michael@0 35 try {
michael@0 36 while (true) {
michael@0 37 let avail = stream.available();
michael@0 38 let toRead = Math.min(numBytes - read, avail, BUFFER_BYTE_LEN);
michael@0 39 if (toRead <= 0)
michael@0 40 break;
michael@0 41 data += stream.readBytes(toRead);
michael@0 42 read += toRead;
michael@0 43 }
michael@0 44 }
michael@0 45 catch (err) {
michael@0 46 throw new Error("Error reading from stream: " + err);
michael@0 47 }
michael@0 48
michael@0 49 return data;
michael@0 50 };
michael@0 51 }
michael@0 52
michael@0 53 function ByteWriter(outputStream) {
michael@0 54 const self = this;
michael@0 55
michael@0 56 let stream = Cc["@mozilla.org/binaryoutputstream;1"].
michael@0 57 createInstance(Ci.nsIBinaryOutputStream);
michael@0 58 stream.setOutputStream(outputStream);
michael@0 59
michael@0 60 let manager = new StreamManager(this, stream);
michael@0 61
michael@0 62 this.write = function ByteWriter_write(str) {
michael@0 63 manager.ensureOpened();
michael@0 64 try {
michael@0 65 stream.writeBytes(str, str.length);
michael@0 66 }
michael@0 67 catch (err) {
michael@0 68 throw new Error("Error writing to stream: " + err);
michael@0 69 }
michael@0 70 };
michael@0 71 }
michael@0 72
michael@0 73
michael@0 74 // This manages the lifetime of stream, a ByteReader or ByteWriter. It defines
michael@0 75 // closed and close() on stream and registers an unload listener that closes
michael@0 76 // rawStream if it's still opened. It also provides ensureOpened(), which
michael@0 77 // throws an exception if the stream is closed.
michael@0 78 function StreamManager(stream, rawStream) {
michael@0 79 const self = this;
michael@0 80 this.rawStream = rawStream;
michael@0 81 this.opened = true;
michael@0 82
michael@0 83 stream.__defineGetter__("closed", function stream_closed() {
michael@0 84 return !self.opened;
michael@0 85 });
michael@0 86
michael@0 87 stream.close = function stream_close() {
michael@0 88 self.ensureOpened();
michael@0 89 self.unload();
michael@0 90 };
michael@0 91
michael@0 92 require("../system/unload").ensure(this);
michael@0 93 }
michael@0 94
michael@0 95 StreamManager.prototype = {
michael@0 96 ensureOpened: function StreamManager_ensureOpened() {
michael@0 97 if (!this.opened)
michael@0 98 throw new Error("The stream is closed and cannot be used.");
michael@0 99 },
michael@0 100 unload: function StreamManager_unload() {
michael@0 101 this.rawStream.close();
michael@0 102 this.opened = false;
michael@0 103 }
michael@0 104 };

mercurial