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 // PDF data storage
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.buffer = null; this.buffers = [];
this.loaded = 0; this.loaded = 0;
} }
@ -162,15 +162,7 @@ PdfDataListener.prototype = {
append: function PdfDataListener_append(chunk) { append: function PdfDataListener_append(chunk) {
// In most of the cases we will pass data as we receive it, but at the // 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. // beginning of the loading we may accumulate some data.
if (!this.buffer) { this.buffers.push(chunk);
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.loaded += chunk.length; this.loaded += chunk.length;
if (this.length >= 0 && this.length < this.loaded) { if (this.length >= 0 && this.length < this.loaded) {
this.length = -1; // reset the length, server is giving incorrect one 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); this.onprogress(this.loaded, this.length >= 0 ? this.length : void 0);
}, },
readData: function PdfDataListener_readData() { readData: function PdfDataListener_readData() {
var result = this.buffer; if (this.buffers.length === 0) {
this.buffer = null; return null;
return result; }
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() { finish: function PdfDataListener_finish() {
this.isDataReady = true; this.isDataReady = true;
@ -888,8 +897,9 @@ PdfStreamConverter.prototype = {
var binaryStream = this.binaryStream; var binaryStream = this.binaryStream;
binaryStream.setInputStream(aInputStream); binaryStream.setInputStream(aInputStream);
var chunk = binaryStream.readByteArray(aCount); let chunk = new ArrayBuffer(aCount);
this.dataListener.append(chunk); binaryStream.readArrayBuffer(aCount, chunk);
this.dataListener.append(new Uint8Array(chunk));
}, },
// nsIRequestObserver::onStartRequest // nsIRequestObserver::onStartRequest