commit 5171f8c091fc206aa67998f0b5bcc9e30bfc0da2
parent 5f45009a18030708b75706ac82d0152d6f5e7bec
Author: Dan Stillman <dstillman@zotero.org>
Date: Tue, 22 Jul 2008 05:38:05 +0000
Save Unicode files with Zotero.File.putContents()
Diffstat:
1 file changed, 11 insertions(+), 7 deletions(-)
diff --git a/chrome/content/zotero/xpcom/file.js b/chrome/content/zotero/xpcom/file.js
@@ -134,18 +134,22 @@ Zotero.File = new function(){
/*
* Write string to a file, overwriting existing file if necessary
- *
- * Note: Can only handle ASCII text!
*/
function putContents(file, str) {
if (file.exists()) {
file.remove(null);
}
- var foStream = Components.classes["@mozilla.org/network/file-output-stream;1"]
- .createInstance(Components.interfaces.nsIFileOutputStream);
- foStream.init(file, 0x02 | 0x08 | 0x20, 0664, 0); // write, create, truncate
- foStream.write(str, str.length);
- foStream.close();
+ var fos = Components.classes["@mozilla.org/network/file-output-stream;1"].
+ createInstance(Components.interfaces.nsIFileOutputStream);
+ fos.init(file, 0x02 | 0x08 | 0x20, 0664, 0); // write, create, truncate
+
+ var os = Components.classes["@mozilla.org/intl/converter-output-stream;1"]
+ .createInstance(Components.interfaces.nsIConverterOutputStream);
+ os.init(fos, "UTF-8", 4096, "?".charCodeAt(0));
+ os.writeString(str);
+ os.close();
+
+ fos.close();
}