Accumulate streamed PDF data into array of buffers.

This commit is contained in:
Brendan Dahl 2017-11-08 20:32:29 -08:00
parent 012d075604
commit 61dd7d1c3a

View File

@ -154,7 +154,7 @@ function getLocalizedString(strings, id, property) {
// PDF data storage
function PdfDataListener(length) {
this.length = length; // less than 0, if length is unknown
this.buffer = null;
this.buffers = [];
this.loaded = 0;
}
@ -162,15 +162,7 @@ PdfDataListener.prototype = {
append: function PdfDataListener_append(chunk) {
// In most of the cases we will pass data as we receive it, but at the
// beginning of the loading we may accumulate some data.
if (!this.buffer) {
this.buffer = new Uint8Array(chunk);
} else {
var buffer = this.buffer;
var newBuffer = new Uint8Array(buffer.length + chunk.length);
newBuffer.set(buffer);
newBuffer.set(chunk, buffer.length);
this.buffer = newBuffer;
}
this.buffers.push(chunk);
this.loaded += chunk.length;
if (this.length >= 0 && this.length < this.loaded) {
this.length = -1; // reset the length, server is giving incorrect one
@ -178,9 +170,26 @@ PdfDataListener.prototype = {
this.onprogress(this.loaded, this.length >= 0 ? this.length : void 0);
},
readData: function PdfDataListener_readData() {
var result = this.buffer;
this.buffer = null;
return result;
if (this.buffers.length === 0) {
return null;
}
if (this.buffers.length === 1) {
return this.buffers.pop();
}
// There are multiple buffers that need to be combined into a single
// buffer.
let combinedLength = 0;
for (let buffer of this.buffers) {
combinedLength += buffer.length;
}
let combinedArray = new Uint8Array(combinedLength);
let writeOffset = 0;
while (this.buffers.length) {
let buffer = this.buffers.shift();
combinedArray.set(buffer, writeOffset);
writeOffset += buffer.length;
}
return combinedArray;
},
finish: function PdfDataListener_finish() {
this.isDataReady = true;
@ -888,8 +897,9 @@ PdfStreamConverter.prototype = {
var binaryStream = this.binaryStream;
binaryStream.setInputStream(aInputStream);
var chunk = binaryStream.readByteArray(aCount);
this.dataListener.append(chunk);
let chunk = new ArrayBuffer(aCount);
binaryStream.readArrayBuffer(aCount, chunk);
this.dataListener.append(new Uint8Array(chunk));
},
// nsIRequestObserver::onStartRequest