netwerk/test/unit/test_safeoutputstream.js

changeset 1
ca08bd8f51b2
equal deleted inserted replaced
-1:000000000000 0:f2a341a572a7
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 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/. */
5
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);
10 do {
11 var written = stream.write(str, str.length);
12 if (written == str.length)
13 break;
14 str = str.substring(written);
15 } while (1);
16 stream.QueryInterface(Ci.nsISafeOutputStream).finish();
17 stream.close();
18 }
19
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);
24 do {
25 var written = stream.write(str, str.length);
26 if (written == str.length)
27 break;
28 str = str.substring(written);
29 } while (1);
30 stream.QueryInterface(Ci.nsISafeOutputStream).finish();
31 stream.close();
32 }
33
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);
38
39 var scriptStream = Cc["@mozilla.org/scriptableinputstream;1"]
40 .createInstance(Ci.nsIScriptableInputStream);
41 scriptStream.init(stream);
42
43 do_check_eq(scriptStream.read(scriptStream.available()), str);
44 scriptStream.close();
45 }
46
47 function run_test()
48 {
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);
54
55 write(file, "First write");
56 checkFile(file, "First write");
57
58 write(file, "Second write");
59 checkFile(file, "Second write");
60
61 write_atomic(file, "First write: Atomic");
62 checkFile(file, "First write: Atomic");
63
64 write_atomic(file, "Second write: Atomic");
65 checkFile(file, "Second write: Atomic");
66 }

mercurial