1 /* -*- indent-tabs-mode: nil; js-indent-level: 4 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 function write_atomic(file, str) {
7 var stream = Cc["@mozilla.org/network/atomic-file-output-stream;1"]
8 .createInstance(Ci.nsIFileOutputStream);
9 stream.init(file, -1, -1, 0);
11 var written = stream.write(str, str.length);
12 if (written == str.length)
14 str = str.substring(written);
16 stream.QueryInterface(Ci.nsISafeOutputStream).finish();
20 function write(file, str) {
21 var stream = Cc["@mozilla.org/network/safe-file-output-stream;1"]
22 .createInstance(Ci.nsIFileOutputStream);
23 stream.init(file, -1, -1, 0);
25 var written = stream.write(str, str.length);
26 if (written == str.length)
28 str = str.substring(written);
30 stream.QueryInterface(Ci.nsISafeOutputStream).finish();
34 function checkFile(file, str) {
35 var stream = Cc["@mozilla.org/network/file-input-stream;1"]
36 .createInstance(Ci.nsIFileInputStream);
37 stream.init(file, -1, -1, 0);
39 var scriptStream = Cc["@mozilla.org/scriptableinputstream;1"]
40 .createInstance(Ci.nsIScriptableInputStream);
41 scriptStream.init(stream);
43 do_check_eq(scriptStream.read(scriptStream.available()), str);
49 var filename = "\u0913";
50 var file = Cc["@mozilla.org/file/directory_service;1"]
51 .getService(Ci.nsIProperties)
52 .get("TmpD", Ci.nsIFile);
53 file.append(filename);
55 write(file, "First write");
56 checkFile(file, "First write");
58 write(file, "Second write");
59 checkFile(file, "Second write");
61 write_atomic(file, "First write: Atomic");
62 checkFile(file, "First write: Atomic");
64 write_atomic(file, "Second write: Atomic");
65 checkFile(file, "Second write: Atomic");