commit 398cf765f584edd77e13e628a13268e165b58bbf
parent b3cb1dbe5e17f1af0963f064d7f06a288e93e332
Author: Dan Stillman <dstillman@zotero.org>
Date: Wed, 6 Mar 2013 15:48:00 -0500
Zotero.File.getBinaryContentsAsync()
Currently unused, but we might need it at some point
Diffstat:
1 file changed, 22 insertions(+), 2 deletions(-)
diff --git a/chrome/content/zotero/xpcom/file.js b/chrome/content/zotero/xpcom/file.js
@@ -149,7 +149,7 @@ Zotero.File = new function(){
* @param {nsIFile|nsIInputStream} file The file 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 promise that is resolved with the contents of the file
+ * @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";
@@ -167,6 +167,26 @@ Zotero.File = new function(){
};
+ /**
+ * Get the contents of a binary source asynchronously
+ *
+ * @param {nsIURI|nsIFile|string spec|nsIChannel|nsIInputStream} source The source to read
+ * @return {Promise} A Q promise that is resolved with the contents of the source
+ */
+ this.getBinaryContentsAsync = function (source) {
+ var deferred = Q.defer();
+ NetUtil.asyncFetch(source, function(inputStream, status) {
+ if (!Components.isSuccessCode(status)) {
+ deferred.reject(new Components.Exception("Source read operation failed", status));
+ return;
+ }
+
+ deferred.resolve(NetUtil.readInputStreamToString(inputStream, inputStream.available()));
+ });
+ return deferred.promise;
+ }
+
+
/*
* Return the contents of a URL as a string
*
@@ -208,7 +228,7 @@ Zotero.File = new function(){
* @param {String|nsIInputStream} data The string or nsIInputStream to write to the
* file
* @param {String} [charset] The character set; defaults to UTF-8
- * @return {Promise} A promise that is resolved when the file has been written
+ * @return {Promise} A Q promise that is resolved when the file has been written
*/
this.putContentsAsync = function putContentsAsync(file, data, charset) {
// Create a stream for async stream copying