modified Ascii85Stream to be more like other streams

This commit is contained in:
sbarman 2011-06-21 21:12:50 -07:00
parent 240fcf650b
commit 3603981581

100
pdf.js
View File

@ -757,76 +757,39 @@ var Ascii85Stream = (function() {
this.eof = false; this.eof = false;
this.pos = 0; this.pos = 0;
this.bufferLength = 0; this.bufferLength = 0;
this.buffer = new Uint8Array(4); this.buffer = null;
} }
constructor.prototype = { constructor.prototype = {
ensureBuffer: function(requested) {
var buffer = this.buffer;
var current = buffer ? buffer.byteLength : 0;
if (requested < current)
return buffer;
var size = 512;
while (size < requested)
size <<= 1;
var buffer2 = Uint8Array(size);
for (var i = 0; i < current; ++i)
buffer2[i] = buffer[i];
return this.buffer = buffer2;
},
getByte: function() { getByte: function() {
if (this.pos >= this.bufferLength) while (this.pos >= this.bufferLength)
this.readBlock(); this.readBlock();
return this.buffer[this.pos++]; return this.buffer[this.pos++];
}, },
getBytes: function(n) { getBytes: function(n) {
if (n) { if (n) {
var i, bytes; while (this.bufferLength < n || !this.eof)
bytes = new Uint8Array(n); this.readBlock();
for (i = 0; i < n; ++i) { return this.buffer.subarray(0, n);
if (this.pos >= this.bufferLength)
this.readBlock();
if (this.eof)
break;
bytes[i] = this.buffer[this.pos++];
}
return bytes;
} else { } else {
var length = 0; while (!this.eof)
var size = 1 << 8; this.readBlock();
var bytes = new Uint8Array(size); return this.buffer;
while (true) {
if (this.pos >= this.bufferLength)
this.readBlock();
if (this.eof)
break;
if (length == size) {
var oldSize = size;
size <<= 1;
var oldBytes = bytes;
bytes = new Uint8Array(size);
for (var i = 0; i < oldSize; ++i)
bytes[i] = oldBytes[i];
}
bytes[length++] = this.buffer[this.pos++];
}
return bytes.subarray(0, length);
} }
}, },
getChar : function() {
return String.fromCharCode(this.getByte());
},
lookChar : function() {
if (this.pos >= this.bufferLength)
this.readRow();
return String.fromCharCode(this.currentRow[this.pos]);
},
skip : function(n) {
var i;
if (!n) {
n = 1;
}
while (n > this.bufferLength - this.pos) {
n -= this.bufferLength - this.pos;
this.readBlock();
if (this.bufferLength === 0) break;
}
this.pos += n;
},
readBlock: function() { readBlock: function() {
if (this.eof) {
this.bufferLength = 0;
this.buffer = [];
this.pos = 0;
return;
}
const tildaCode = "~".charCodeAt(0); const tildaCode = "~".charCodeAt(0);
const zCode = "z".charCodeAt(0); const zCode = "z".charCodeAt(0);
var str = this.str; var str = this.str;
@ -839,14 +802,17 @@ var Ascii85Stream = (function() {
return; return;
} }
var buffer = this.buffer; var bufferLength = this.bufferLength;
// special code for z // special code for z
if (c == zCode) { if (c == zCode) {
buffer[0] = 0; this.ensureBuffer(bufferLength + 4);
buffer[1] = 0; var buffer = this.buffer;
buffer[2] = 0; buffer[bufferLength++] = 0;
buffer[3] = 0; buffer[bufferLength++] = 0;
this.bufferLength = 4; buffer[bufferLength++] = 0;
buffer[bufferLength++] = 0;
this.bufferLength += 4;
} else { } else {
var input = new Uint8Array(5); var input = new Uint8Array(5);
input[0] = c; input[0] = c;
@ -860,7 +826,9 @@ var Ascii85Stream = (function() {
if (!c || c == tildaCode) if (!c || c == tildaCode)
break; break;
} }
this.bufferLength = i - 1; this.ensureBuffer(bufferLength + i - 1);
var buffer = this.buffer;
this.bufferLength += i - 1;
// partial ending; // partial ending;
if (i < 5) { if (i < 5) {
for (++i; i < 5; ++i) for (++i; i < 5; ++i)
@ -872,7 +840,7 @@ var Ascii85Stream = (function() {
t = t * 85 + (input[i] - 0x21); t = t * 85 + (input[i] - 0x21);
for (var i = 3; i >= 0; --i){ for (var i = 3; i >= 0; --i){
buffer[i] = t & 0xFF; buffer[bufferLength++] = t & 0xFF;
t >>= 8; t >>= 8;
} }
} }