commit 8fe02d8b7180540861f8b6e1bcdd3d969699dc2e
parent 89c569089995905b5cadc5abdcf6ed0d588ce4c3
Author: Dan Stillman <dstillman@zotero.org>
Date: Tue, 20 Aug 2013 12:42:07 -0400
Handle input streams and other sources in Zotero.File.getContentsAsync()
Diffstat:
1 file changed, 10 insertions(+), 9 deletions(-)
diff --git a/chrome/content/zotero/xpcom/file.js b/chrome/content/zotero/xpcom/file.js
@@ -159,23 +159,24 @@ Zotero.File = new function(){
/**
- * Get the contents of a file or input stream asynchronously
- * @param {nsIFile|nsIInputStream} file The file to read
+ * Get the contents of a text source asynchronously
+ *
+ * @param {nsIURI|nsIFile|string spec|nsIChannel|nsIInputStream} source The source to read
* @param {String} [charset] The character set; defaults to UTF-8
- * @param {Integer} [maxLength] The maximum number of characters to read
* @return {Promise} A Q promise that is resolved with the contents of the file
*/
- this.getContentsAsync = function getContentsAsync(file, charset, maxLength) {
- charset = charset ? Zotero.CharacterSets.getName(charset) : "UTF-8";
- var deferred = Q.defer(),
- channel = NetUtil.newChannel(file, charset);
- NetUtil.asyncFetch(channel, function(inputStream, status) {
+ this.getContentsAsync = function getContentsAsync(source, charset) {
+ var options = {
+ charset: charset ? Zotero.CharacterSets.getName(charset) : "UTF-8"
+ };
+ var deferred = Q.defer();
+ NetUtil.asyncFetch(source, function(inputStream, status) {
if (!Components.isSuccessCode(status)) {
deferred.reject(new Components.Exception("File read operation failed", status));
return;
}
- deferred.resolve(NetUtil.readInputStreamToString(inputStream, inputStream.available()));
+ deferred.resolve(NetUtil.readInputStreamToString(inputStream, inputStream.available(), options));
});
return deferred.promise;
};