/* This Source Code Form is subject to the terms of the Mozilla Public *License,v.2.0.IfacopyoftheMPLwasnotdistributedwiththis
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/** *Checksthatgettinganinputstreamfromastoragestreamtowhich0bytesof *datahavebeenexplicitlywrittendoesn'tthrowanexception.
*/ function test2() { var ss = Cc["@mozilla.org/storagestream;1"].createInstance(
Ci.nsIStorageStream
);
ss.init(1024, 1024, null);
var out = ss.getOutputStream(0);
out.write("", 0); try {
ss.newInputStream(0);
} catch (e) {
do_throw("shouldn't throw exception when new input stream created");
}
}
/** *Checksthatreadinganynon-zeroamountofdatafromastoragestream *whichhashad0byteswrittentoitexplicitlyworkscorrectly.
*/ function test3() { var ss = Cc["@mozilla.org/storagestream;1"].createInstance(
Ci.nsIStorageStream
);
ss.init(1024, 1024, null);
var out = ss.getOutputStream(0);
out.write("", 0); try { var inp = ss.newInputStream(0);
} catch (e) {
do_throw("newInputStream(0) shouldn't throw if write() is called: " + e);
}
Assert.ok(inp.isNonBlocking(), "next test expects a non-blocking stream");
try { var threw = false; var bis = BIS(inp);
bis.readByteArray(5);
} catch (e) { if (e.result != Cr.NS_BASE_STREAM_WOULD_BLOCK) {
do_throw("wrong error thrown: " + e);
}
threw = true;
} Assert.ok(threw, "should have thrown (nsStorageInputStream is nonblocking)");
}
/** *Basicfunctionalitytestforstoragestream:writedatatoit,getaninput *stream,andreadthedatabacktoseethatitmatches.
*/ function test4() { var bytes = [65, 66, 67, 68, 69, 70, 71, 72, 73, 74];
var ss = Cc["@mozilla.org/storagestream;1"].createInstance(
Ci.nsIStorageStream
);
ss.init(1024, 1024, null);
var outStream = ss.getOutputStream(0);
var bos = Cc["@mozilla.org/binaryoutputstream;1"].createInstance(
Ci.nsIBinaryOutputStream
);
bos.setOutputStream(outStream);
bos.writeByteArray(bytes);
bos.close();
var inp = ss.newInputStream(0); var bis = BIS(inp);
var count = 0; while (count < bytes.length) { var data = bis.read8(1); Assert.equal(data, bytes[count++]);
}
var threw = false; try {
data = bis.read8(1);
} catch (e) { if (e.result != Cr.NS_ERROR_FAILURE) {
do_throw("wrong error thrown: " + e);
}
threw = true;
} if (!threw) {
do_throw("should have thrown but instead returned: '" + data + "'");
}
}
function BIS(input) { var bis = Cc["@mozilla.org/binaryinputstream;1"].createInstance(
Ci.nsIBinaryInputStream
);
bis.setInputStream(input); return bis;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.