1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-25 09:38:06 +02:00

Add an abstract base-class, which all the various Stream implementations inherit from

By having an abstract base-class, it becomes a lot clearer exactly which methods/getters are expected to exist on all Stream instances.
Furthermore, since a number of the methods are *identical* for all Stream implementations, this reduces unnecessary code duplication in the `Stream`, `DecodeStream`, and `ChunkedStream` classes.

For e.g. `gulp mozcentral`, the *built* `pdf.worker.js` files decreases from `1 619 329` to `1 616 115` bytes with this patch-series.
This commit is contained in:
Jonas Jenwald 2021-04-27 17:08:54 +02:00
parent 6151b4ecac
commit 67415bfabe
6 changed files with 138 additions and 158 deletions

View file

@ -13,8 +13,8 @@
* limitations under the License.
*/
import { BaseStream } from "./base_stream.js";
import { Stream } from "./stream.js";
import { unreachable } from "../shared/util.js";
// Lots of DecodeStreams are created whose buffers are never used. For these
// we share a single empty buffer. This is (a) space-efficient and (b) avoids
@ -23,8 +23,9 @@ import { unreachable } from "../shared/util.js";
const emptyBuffer = new Uint8Array(0);
// Super class for the decoding streams.
class DecodeStream {
class DecodeStream extends BaseStream {
constructor(maybeMinBufferLength) {
super();
this._rawMinBufferLength = maybeMinBufferLength || 0;
this.pos = 0;
@ -40,11 +41,6 @@ class DecodeStream {
}
}
// eslint-disable-next-line getter-return
get length() {
unreachable("Should not access DecodeStream.length");
}
get isEmpty() {
while (!this.eof && this.bufferLength === 0) {
this.readBlock();
@ -77,23 +73,6 @@ class DecodeStream {
return this.buffer[this.pos++];
}
getUint16() {
const b0 = this.getByte();
const b1 = this.getByte();
if (b0 === -1 || b1 === -1) {
return -1;
}
return (b0 << 8) + b1;
}
getInt32() {
const b0 = this.getByte();
const b1 = this.getByte();
const b2 = this.getByte();
const b3 = this.getByte();
return (b0 << 24) + (b1 << 16) + (b2 << 8) + b3;
}
getBytes(length, forceClamped = false) {
const pos = this.pos;
let end;
@ -124,18 +103,8 @@ class DecodeStream {
: subarray;
}
peekByte() {
const peekedByte = this.getByte();
if (peekedByte !== -1) {
this.pos--;
}
return peekedByte;
}
peekBytes(length, forceClamped = false) {
const bytes = this.getBytes(length, forceClamped);
this.pos -= bytes.length;
return bytes;
reset() {
this.pos = 0;
}
makeSubStream(start, length, dict = null) {
@ -152,21 +121,6 @@ class DecodeStream {
return new Stream(this.buffer, start, length, dict);
}
getByteRange(begin, end) {
unreachable("Should not call DecodeStream.getByteRange");
}
skip(n) {
if (!n) {
n = 1;
}
this.pos += n;
}
reset() {
this.pos = 0;
}
getBaseStreams() {
if (this.str && this.str.getBaseStreams) {
return this.str.getBaseStreams();