From 9a41659ae7eddb92eaa09626db84b672947a6a43 Mon Sep 17 00:00:00 2001 From: fkaelberer Date: Wed, 25 Jun 2014 20:11:49 +0200 Subject: [PATCH 1/2] Faster chunkedStream_getByte() --- src/core/chunked_stream.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/core/chunked_stream.js b/src/core/chunked_stream.js index cf0e6b700..5eeb3f082 100644 --- a/src/core/chunked_stream.js +++ b/src/core/chunked_stream.js @@ -155,8 +155,15 @@ var ChunkedStream = (function ChunkedStreamClosure() { if (pos >= this.end) { return -1; } - this.ensureByte(pos); - return this.bytes[this.pos++]; + var byte = this.bytes[pos]; + if (byte === 0) { + // |byte| might be zero, because the corresponding chunk has not been + // loaded yet. In this case, this.ensureByte(pos) will throw an + // exception and nothing is returned. + this.ensureByte(pos); + } + this.pos++; + return byte; }, getUint16: function ChunkedStream_getUint16() { From fc73e2e17354d5e5682f956da35ad73ccfa3d710 Mon Sep 17 00:00:00 2001 From: fkaelberer Date: Fri, 27 Jun 2014 09:09:54 +0200 Subject: [PATCH 2/2] use getBytes() instead of looping over getByte() --- src/core/core.js | 10 +++------- src/core/stream.js | 10 ++++------ 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/src/core/core.js b/src/core/core.js index e3a821028..f012b271c 100644 --- a/src/core/core.js +++ b/src/core/core.js @@ -14,8 +14,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -/* globals assert, calculateMD5, Catalog, Dict, error, info, isArray, - isArrayBuffer, isName, isStream, isString, createPromiseCapability, +/* globals assert, bytesToString, calculateMD5, Catalog, Dict, error, info, + isArray, isArrayBuffer, isName, isStream, isString, Linearization, NullStream, PartialEvaluator, shadow, Stream, Lexer, StreamsSequenceStream, stringToPDFString, stringToBytes, Util, XRef, MissingDataException, Promise, Annotation, ObjectLoader, OperatorList @@ -301,14 +301,10 @@ var PDFDocument = (function PDFDocumentClosure() { function find(stream, needle, limit, backwards) { var pos = stream.pos; var end = stream.end; - var strBuf = []; if (pos + limit > end) { limit = end - pos; } - for (var n = 0; n < limit; ++n) { - strBuf.push(String.fromCharCode(stream.getByte())); - } - var str = strBuf.join(''); + var str = bytesToString(stream.getBytes(limit)); stream.pos = pos; var index = backwards ? str.lastIndexOf(needle) : str.indexOf(needle); if (index == -1) { diff --git a/src/core/stream.js b/src/core/stream.js index 406d4d908..6a17214ae 100644 --- a/src/core/stream.js +++ b/src/core/stream.js @@ -539,12 +539,10 @@ var FlateStream = (function FlateStreamClosure() { this.eof = true; } } else { - for (var n = bufferLength; n < end; ++n) { - if ((b = str.getByte()) === -1) { - this.eof = true; - break; - } - buffer[n] = b; + var block = str.getBytes(blockLen); + buffer.set(block, bufferLength); + if (block.length < blockLen) { + this.eof = true; } } return;