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
This commit is contained in:
parent
330f8697f8
commit
e69f9d156f
1 changed files with 9 additions and 20 deletions
|
@ -63,21 +63,8 @@ Zotero.File = new function(){
|
||||||
/*
|
/*
|
||||||
* Get the first 128 bytes of the file as a string (multibyte-safe)
|
* Get the first 128 bytes of the file as a string (multibyte-safe)
|
||||||
*/
|
*/
|
||||||
function getSample(file){
|
function getSample(file) {
|
||||||
var fis = Components.classes["@mozilla.org/network/file-input-stream;1"].
|
return this.getContents(file, null, 128);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -115,17 +102,19 @@ Zotero.File = new function(){
|
||||||
var is = Components.classes["@mozilla.org/intl/converter-input-stream;1"]
|
var is = Components.classes["@mozilla.org/intl/converter-input-stream;1"]
|
||||||
.createInstance(Components.interfaces.nsIConverterInputStream);
|
.createInstance(Components.interfaces.nsIConverterInputStream);
|
||||||
is.init(fis, charset, 4096, replacementChar);
|
is.init(fis, charset, 4096, replacementChar);
|
||||||
var chars = 4096;
|
var chars = 0;
|
||||||
|
|
||||||
var contents = [], str = {};
|
var contents = [], str = {};
|
||||||
while (is.readString(4096, str) != 0) {
|
while (is.readString(4096, str) != 0) {
|
||||||
|
var strLen = str.value.length;
|
||||||
|
|
||||||
if (maxLength) {
|
if (maxLength) {
|
||||||
chars += 4096;
|
if ((chars + strLen) > maxLength) {
|
||||||
if (chars >= maxLength) {
|
var remainder = maxLength - chars;
|
||||||
Zotero.debug('Stopping at ' + (chars - 4096)
|
contents.push(str.value.slice(0, remainder));
|
||||||
+ ' characters in File.getContents()');
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
chars += strLen;
|
||||||
}
|
}
|
||||||
|
|
||||||
contents.push(str.value);
|
contents.push(str.value);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue