Skip to content

Commit

Permalink
Refactors PdfDataLister buffering.
Browse files Browse the repository at this point in the history
  • Loading branch information
yurydelendik committed Oct 13, 2014
1 parent c8d729f commit 63444aa
Showing 1 changed file with 20 additions and 28 deletions.
48 changes: 20 additions & 28 deletions extensions/firefox/content/PdfStreamConverter.jsm
Expand Up @@ -177,46 +177,38 @@ function makeContentReadable(obj, window) {
// PDF data storage
function PdfDataListener(length) {
this.length = length; // less than 0, if length is unknown
this.data = new Uint8Array(length >= 0 ? length : 0x10000);
this.position = 0;
this.buffer = null;
this.loaded = 0;
}

PdfDataListener.prototype = {
append: function PdfDataListener_append(chunk) {
var willBeLoaded = this.loaded + chunk.length;
if (this.length >= 0 && this.length < willBeLoaded) {
this.length = -1; // reset the length, server is giving incorrect one
// 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;
}
if (this.length < 0 && this.data.length < willBeLoaded) {
// data length is unknown and new chunk will not fit in the existing
// buffer, resizing the buffer by doubling the its last length
var newLength = this.data.length;
for (; newLength < willBeLoaded; newLength *= 2) {}
var newData = new Uint8Array(newLength);
newData.set(this.data);
this.data = newData;
this.loaded += chunk.length;
if (this.length >= 0 && this.length < this.loaded) {
this.length = -1; // reset the length, server is giving incorrect one
}
this.data.set(chunk, this.loaded);
this.loaded = willBeLoaded;
this.onprogress(this.loaded, this.length >= 0 ? this.length : void(0));
},
readData: function PdfDataListener_readData() {
var data = this.data.subarray(this.position, this.loaded);
this.position = this.loaded;
return data;
},
getData: function PdfDataListener_getData() {
var data = this.data;
if (this.loaded != data.length)
data = data.subarray(0, this.loaded);
delete this.data; // releasing temporary storage
return data;
var result = this.buffer;
this.buffer = null;
return result;
},
finish: function PdfDataListener_finish() {
this.isDataReady = true;
if (this.oncompleteCallback) {
this.oncompleteCallback(this.getData());
this.oncompleteCallback(this.readData());
}
},
error: function PdfDataListener_error(errorCode) {
Expand All @@ -232,7 +224,7 @@ PdfDataListener.prototype = {
set oncomplete(value) {
this.oncompleteCallback = value;
if (this.isDataReady) {
value(this.getData());
value(this.readData());
}
if (this.errorCode) {
value(null, this.errorCode);
Expand Down Expand Up @@ -607,7 +599,7 @@ var RangedChromeActions = (function RangedChromeActionsClosure() {
if (!this.streamingEnabled) {
this.originalRequest.cancel(Cr.NS_BINDING_ABORTED);
this.originalRequest = null;
data = this.dataListener.getData();
data = this.dataListener.readData();
this.dataListener = null;
} else {
data = this.dataListener.readData();
Expand Down

0 comments on commit 63444aa

Please sign in to comment.