Fixes reported offset when length is unknown

This commit is contained in:
Yury Delendik 2012-08-20 17:53:26 -05:00
parent df4fadeaf5
commit 077b19fca1

View File

@ -127,27 +127,29 @@ function getLocalizedString(strings, id, property) {
function PdfDataListener(length) { function PdfDataListener(length) {
this.length = length; // less than 0, if length is unknown this.length = length; // less than 0, if length is unknown
this.data = new Uint8Array(length >= 0 ? length : 0x10000); this.data = new Uint8Array(length >= 0 ? length : 0x10000);
this.loaded = 0;
} }
PdfDataListener.prototype = { PdfDataListener.prototype = {
set: function PdfDataListener_set(chunk, offset) { set: function PdfDataListener_set(chunk, offset) {
var loaded = offset + chunk.length; if (this.length < 0) {
if (this.length < 0 && this.data.length < loaded) { var willBeLoaded = this.loaded + chunk.length;
// data length is unknown and new chunk will not fit in the existing // data length is unknown and new chunk will not fit in the existing
// buffer, resizing the buffer by doubling the last its length // buffer, resizing the buffer by doubling the its last length
var newLength = this.data.length; if (this.data.length < willBeLoaded) {
for (; newLength < loaded; newLength *= 2) {} var newLength = this.data.length;
var newData = new Uint8Array(newLength); for (; newLength < willBeLoaded; newLength *= 2) {}
newData.set(this.data); var newData = new Uint8Array(newLength);
this.data = newData; newData.set(this.data);
this.data = newData;
}
this.data.set(chunk, this.loaded);
this.loaded = willBeLoaded;
} else {
this.data.set(chunk, offset);
this.loaded = offset + chunk.length;
this.onprogress(this.loaded, this.length);
} }
this.data.set(chunk, offset);
this.loaded = loaded;
// not reporting the progress if data length is unknown
if (this.length >= 0)
this.onprogress(loaded, this.length);
}, },
getData: function PdfDataListener_getData() { getData: function PdfDataListener_getData() {
var data = this.length >= 0 ? this.data : var data = this.length >= 0 ? this.data :