1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-26 10:08: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,10 +13,13 @@
* limitations under the License.
*/
import { BaseStream } from "./base_stream.js";
import { stringToBytes } from "../shared/util.js";
class Stream {
class Stream extends BaseStream {
constructor(arrayBuffer, start, length, dict) {
super();
this.bytes =
arrayBuffer instanceof Uint8Array
? arrayBuffer
@ -42,24 +45,6 @@ class Stream {
return this.bytes[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;
}
// Returns subarray of original buffer, should only be read.
getBytes(length, forceClamped = false) {
const bytes = this.bytes;
const pos = this.pos;
@ -80,20 +65,6 @@ class Stream {
return forceClamped ? new Uint8ClampedArray(subarray) : 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;
}
getByteRange(begin, end) {
if (begin < 0) {
begin = 0;
@ -104,13 +75,6 @@ class Stream {
return this.bytes.subarray(begin, end);
}
skip(n) {
if (!n) {
n = 1;
}
this.pos += n;
}
reset() {
this.pos = this.start;
}