netwerk/test/unit/test_safeoutputstream.js

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 function write_atomic(file, str) {
michael@0 7 var stream = Cc["@mozilla.org/network/atomic-file-output-stream;1"]
michael@0 8 .createInstance(Ci.nsIFileOutputStream);
michael@0 9 stream.init(file, -1, -1, 0);
michael@0 10 do {
michael@0 11 var written = stream.write(str, str.length);
michael@0 12 if (written == str.length)
michael@0 13 break;
michael@0 14 str = str.substring(written);
michael@0 15 } while (1);
michael@0 16 stream.QueryInterface(Ci.nsISafeOutputStream).finish();
michael@0 17 stream.close();
michael@0 18 }
michael@0 19
michael@0 20 function write(file, str) {
michael@0 21 var stream = Cc["@mozilla.org/network/safe-file-output-stream;1"]
michael@0 22 .createInstance(Ci.nsIFileOutputStream);
michael@0 23 stream.init(file, -1, -1, 0);
michael@0 24 do {
michael@0 25 var written = stream.write(str, str.length);
michael@0 26 if (written == str.length)
michael@0 27 break;
michael@0 28 str = str.substring(written);
michael@0 29 } while (1);
michael@0 30 stream.QueryInterface(Ci.nsISafeOutputStream).finish();
michael@0 31 stream.close();
michael@0 32 }
michael@0 33
michael@0 34 function checkFile(file, str) {
michael@0 35 var stream = Cc["@mozilla.org/network/file-input-stream;1"]
michael@0 36 .createInstance(Ci.nsIFileInputStream);
michael@0 37 stream.init(file, -1, -1, 0);
michael@0 38
michael@0 39 var scriptStream = Cc["@mozilla.org/scriptableinputstream;1"]
michael@0 40 .createInstance(Ci.nsIScriptableInputStream);
michael@0 41 scriptStream.init(stream);
michael@0 42
michael@0 43 do_check_eq(scriptStream.read(scriptStream.available()), str);
michael@0 44 scriptStream.close();
michael@0 45 }
michael@0 46
michael@0 47 function run_test()
michael@0 48 {
michael@0 49 var filename = "\u0913";
michael@0 50 var file = Cc["@mozilla.org/file/directory_service;1"]
michael@0 51 .getService(Ci.nsIProperties)
michael@0 52 .get("TmpD", Ci.nsIFile);
michael@0 53 file.append(filename);
michael@0 54
michael@0 55 write(file, "First write");
michael@0 56 checkFile(file, "First write");
michael@0 57
michael@0 58 write(file, "Second write");
michael@0 59 checkFile(file, "Second write");
michael@0 60
michael@0 61 write_atomic(file, "First write: Atomic");
michael@0 62 checkFile(file, "First write: Atomic");
michael@0 63
michael@0 64 write_atomic(file, "Second write: Atomic");
michael@0 65 checkFile(file, "Second write: Atomic");
michael@0 66 }

mercurial