OLD | NEW |
1 /* | 1 /* |
2 * This Source Code is subject to the terms of the Mozilla Public License | 2 * This Source Code is subject to the terms of the Mozilla Public License |
3 * version 2.0 (the "License"). You can obtain a copy of the License at | 3 * version 2.0 (the "License"). You can obtain a copy of the License at |
4 * http://mozilla.org/MPL/2.0/. | 4 * http://mozilla.org/MPL/2.0/. |
5 */ | 5 */ |
6 | 6 |
7 /** | 7 /** |
8 * @fileOverview Module containing file I/O helpers. | 8 * @fileOverview Module containing file I/O helpers. |
9 */ | 9 */ |
10 | 10 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 | 52 |
53 /** | 53 /** |
54 * Reads strings from a file asynchronously, calls listener.process() with | 54 * Reads strings from a file asynchronously, calls listener.process() with |
55 * each line read and with a null parameter once the read operation is done. | 55 * each line read and with a null parameter once the read operation is done. |
56 * The callback will be called when the operation is done. | 56 * The callback will be called when the operation is done. |
57 */ | 57 */ |
58 readFromFile: function(/**nsIFile|nsIURI*/ file, /**Boolean*/ decode, /**Objec
t*/ listener, /**Function*/ callback, /**String*/ timeLineID) | 58 readFromFile: function(/**nsIFile|nsIURI*/ file, /**Boolean*/ decode, /**Objec
t*/ listener, /**Function*/ callback, /**String*/ timeLineID) |
59 { | 59 { |
60 try | 60 try |
61 { | 61 { |
| 62 let buffer = ""; |
62 let uri = file instanceof Ci.nsIFile ? Services.io.newFileURI(file) : file
; | 63 let uri = file instanceof Ci.nsIFile ? Services.io.newFileURI(file) : file
; |
63 let channel = Services.io.newChannelFromURI(uri); | 64 let request = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance
(Ci.nsIXMLHttpRequest); |
64 channel.contentType = "text/plain"; | 65 request.open("GET", uri.spec); |
65 let converter = null; | 66 request.mozBackgroundRequest = true; |
66 if (decode) | 67 request.responseType = "moz-chunked-text"; |
| 68 request.overrideMimeType("text/plain" + (decode ? "? charset=utf-8" : ""))
; |
| 69 |
| 70 request.addEventListener("progress", function(event) |
67 { | 71 { |
68 converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"].createIns
tance(Ci.nsIScriptableUnicodeConverter); | 72 if (timeLineID) |
69 converter.charset = "utf-8"; | 73 { |
70 } | 74 TimeLine.asyncStart(timeLineID); |
| 75 } |
71 | 76 |
72 channel.asyncOpen({ | 77 let data = event.target.response; |
73 buffer: "", | 78 let index = Math.max(data.lastIndexOf("\n"), data.lastIndexOf("\r")); |
74 QueryInterface: XPCOMUtils.generateQI([Ci.nsIRequestObserver, Ci.nsIStre
amListener]), | 79 if (index >= 0) |
75 onStartRequest: function(request, context) {}, | |
76 onDataAvailable: function(request, context, stream, offset, count) | |
77 { | 80 { |
78 if (timeLineID) | 81 let oldBuffer = buffer; |
79 { | 82 buffer = data.substr(index + 1); |
80 TimeLine.asyncStart(timeLineID); | 83 data = data.substr(0, index + 1); |
81 } | 84 let lines = data.split(/[\r\n]+/); |
| 85 lines.pop(); |
| 86 lines[0] = oldBuffer + lines[0]; |
| 87 for (let i = 0; i < lines.length; i++) |
| 88 listener.process(lines[i]); |
| 89 } |
| 90 else |
| 91 buffer += data; |
82 | 92 |
83 let inputStream = Cc["@mozilla.org/scriptableinputstream;1"].createIns
tance(Ci.nsIScriptableInputStream); | 93 if (timeLineID) |
84 inputStream.init(stream); | 94 { |
| 95 TimeLine.asyncEnd(timeLineID); |
| 96 } |
| 97 }, false); |
85 | 98 |
86 let data = this.buffer + inputStream.readBytes(count); | 99 request.addEventListener("load", function(event) |
87 let index = Math.max(data.lastIndexOf("\n"), data.lastIndexOf("\r")); | 100 { |
88 if (index >= 0) | 101 if (timeLineID) |
89 { | 102 { |
90 this.buffer = data.substr(index + 1); | 103 TimeLine.asyncStart(timeLineID); |
91 data = data.substr(0, index + 1); | 104 } |
92 if (converter) | |
93 data = converter.ConvertToUnicode(data); | |
94 | 105 |
95 let lines = data.split(/[\r\n]+/); | 106 if (buffer !== "") |
96 lines.pop(); | 107 listener.process(buffer); |
97 for (let i = 0; i < lines.length; i++) | 108 listener.process(null); |
98 listener.process(lines[i]); | |
99 } | |
100 else | |
101 this.buffer = data; | |
102 | 109 |
103 if (timeLineID) | 110 if (timeLineID) |
104 { | |
105 TimeLine.asyncEnd(timeLineID); | |
106 } | |
107 }, | |
108 onStopRequest: function(request, context, result) | |
109 { | 111 { |
110 if (timeLineID) | 112 TimeLine.asyncEnd(timeLineID); |
111 { | 113 TimeLine.asyncDone(timeLineID); |
112 TimeLine.asyncStart(timeLineID); | 114 } |
113 } | |
114 | 115 |
115 if (Components.isSuccessCode(result) && this.buffer.length) | 116 callback(null); |
116 listener.process(this.buffer); | 117 }, false); |
117 listener.process(null); | |
118 | 118 |
119 if (timeLineID) | 119 request.addEventListener("error", function() |
120 { | 120 { |
121 TimeLine.asyncEnd(timeLineID); | 121 let e = Cc["@mozilla.org/js/xpc/Exception;1"].createInstance(Ci.nsIXPCEx
ception); |
122 TimeLine.asyncDone(timeLineID); | 122 e.initialize("File read operation failed", result, null, Components.stac
k, file, null); |
123 } | 123 callback(e); |
124 | 124 |
125 if (!Components.isSuccessCode(result)) | 125 if (timeLineID) |
126 { | 126 { |
127 let e = Cc["@mozilla.org/js/xpc/Exception;1"].createInstance(Ci.nsIX
PCException); | 127 TimeLine.asyncDone(timeLineID); |
128 e.initialize("File read operation failed", result, null, Components.
stack, file, null); | |
129 callback(e); | |
130 } | |
131 else | |
132 callback(null); | |
133 } | 128 } |
134 }, null); | 129 }, false); |
| 130 |
| 131 request.send(null); |
135 } | 132 } |
136 catch (e) | 133 catch (e) |
137 { | 134 { |
138 callback(e); | 135 callback(e); |
139 } | 136 } |
140 }, | 137 }, |
141 | |
142 /** | 138 /** |
143 * Writes string data to a file asynchronously, optionally encodes it into | 139 * Writes string data to a file asynchronously, optionally encodes it into |
144 * UTF-8 first. The callback will be called when the write operation is done. | 140 * UTF-8 first. The callback will be called when the write operation is done. |
145 */ | 141 */ |
146 writeToFile: function(/**nsIFile*/ file, /**Boolean*/ encode, /**Iterator*/ da
ta, /**Function*/ callback, /**String*/ timeLineID) | 142 writeToFile: function(/**nsIFile*/ file, /**Boolean*/ encode, /**Iterator*/ da
ta, /**Function*/ callback, /**String*/ timeLineID) |
147 { | 143 { |
148 try | 144 try |
149 { | 145 { |
150 let fileStream = FileUtils.openSafeFileOutputStream(file, FileUtils.MODE_W
RONLY | FileUtils.MODE_CREATE | FileUtils.MODE_TRUNCATE); | 146 let fileStream = FileUtils.openSafeFileOutputStream(file, FileUtils.MODE_W
RONLY | FileUtils.MODE_CREATE | FileUtils.MODE_TRUNCATE); |
151 | 147 |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
323 isFile: exists && file.isFile(), | 319 isFile: exists && file.isFile(), |
324 lastModified: exists ? file.lastModifiedTime : 0 | 320 lastModified: exists ? file.lastModifiedTime : 0 |
325 }); | 321 }); |
326 } | 322 } |
327 catch(e) | 323 catch(e) |
328 { | 324 { |
329 callback(e); | 325 callback(e); |
330 } | 326 } |
331 } | 327 } |
332 } | 328 } |
OLD | NEW |