www

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | Submodules | README | LICENSE

commit e69f9d156f5eca300f80fd0a0c2ef1b8f12b963d
parent 330f8697f8f9a880a0ecc7eb9a35ad8daec325a8
Author: Dan Stillman <dstillman@zotero.org>
Date:   Mon,  6 Sep 2010 19:10:15 +0000

Fixes #1721, Zotero.File.getContents() method ignores small values of maxLength

- Modified version of Frank's patch -- among other things, requested bytes aren't guaranteed by readString()
- Unit tests


Diffstat:
Mchrome/content/zotero/xpcom/file.js | 29+++++++++--------------------
1 file changed, 9 insertions(+), 20 deletions(-)

diff --git a/chrome/content/zotero/xpcom/file.js b/chrome/content/zotero/xpcom/file.js @@ -63,21 +63,8 @@ Zotero.File = new function(){ /* * Get the first 128 bytes of the file as a string (multibyte-safe) */ - function getSample(file){ - var fis = Components.classes["@mozilla.org/network/file-input-stream;1"]. - createInstance(Components.interfaces.nsIFileInputStream); - fis.init(file, 0x01, 0664, 0); - - const replacementChar - = Components.interfaces.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER; - var is = Components.classes["@mozilla.org/intl/converter-input-stream;1"] - .createInstance(Components.interfaces.nsIConverterInputStream); - is.init(fis, "UTF-8", 128, replacementChar); - var str = {}; - var numChars = is.readString(128, str); - is.close(); - - return str.value; + function getSample(file) { + return this.getContents(file, null, 128); } @@ -115,17 +102,19 @@ Zotero.File = new function(){ var is = Components.classes["@mozilla.org/intl/converter-input-stream;1"] .createInstance(Components.interfaces.nsIConverterInputStream); is.init(fis, charset, 4096, replacementChar); - var chars = 4096; + var chars = 0; var contents = [], str = {}; while (is.readString(4096, str) != 0) { + var strLen = str.value.length; + if (maxLength) { - chars += 4096; - if (chars >= maxLength) { - Zotero.debug('Stopping at ' + (chars - 4096) - + ' characters in File.getContents()'); + if ((chars + strLen) > maxLength) { + var remainder = maxLength - chars; + contents.push(str.value.slice(0, remainder)); break; } + chars += strLen; } contents.push(str.value);