From 1f6bfa08908ad3c5324accce52419db3fe8675af Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Wed, 29 Apr 2026 13:02:15 +0200 Subject: [PATCH] Add an abstract `readBlock` method in the `DecodeStream` class This avoids having to "duplicate" dummy `readBlock` methods in a couple of image-stream classes. Also, move a few `DecodeStream` field definitions to (ever so slightly) shorten the code. --- src/core/ccitt_stream.js | 6 +----- src/core/decode_stream.js | 20 +++++++++++++++----- src/core/jbig2_stream.js | 6 +----- src/core/jpx_stream.js | 6 +----- 4 files changed, 18 insertions(+), 20 deletions(-) diff --git a/src/core/ccitt_stream.js b/src/core/ccitt_stream.js index 5279d038c..8bdb08b1e 100644 --- a/src/core/ccitt_stream.js +++ b/src/core/ccitt_stream.js @@ -13,10 +13,10 @@ * limitations under the License. */ -import { shadow, unreachable } from "../shared/util.js"; import { DecodeStream } from "./decode_stream.js"; import { Dict } from "./primitives.js"; import { JBig2CCITTFaxImage } from "./jbig2_ccittFax.js"; +import { shadow } from "../shared/util.js"; class CCITTFaxStream extends DecodeStream { constructor(str, maybeLength, params) { @@ -46,10 +46,6 @@ class CCITTFaxStream extends DecodeStream { return shadow(this, "bytes", this.stream.getBytes(this.maybeLength)); } - readBlock() { - unreachable("CCITTFaxStream.readBlock"); - } - get isImageStream() { return true; } diff --git a/src/core/decode_stream.js b/src/core/decode_stream.js index 88b1957ce..c8edbff22 100644 --- a/src/core/decode_stream.js +++ b/src/core/decode_stream.js @@ -15,6 +15,7 @@ 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 @@ -24,15 +25,20 @@ const emptyBuffer = new Uint8Array(0); // Super class for the decoding streams. class DecodeStream extends BaseStream { + buffer = emptyBuffer; + + bufferLength = 0; + + eof = false; + + minBufferLength = 512; + + pos = 0; + constructor(maybeMinBufferLength) { super(); this._rawMinBufferLength = maybeMinBufferLength || 0; - this.pos = 0; - this.bufferLength = 0; - this.eof = false; - this.buffer = emptyBuffer; - this.minBufferLength = 512; if (maybeMinBufferLength) { // Compute the first power of two that is as big as maybeMinBufferLength. while (this.minBufferLength < maybeMinBufferLength) { @@ -41,6 +47,10 @@ class DecodeStream extends BaseStream { } } + readBlock() { + unreachable("Abstract method `readBlock` called"); + } + get isEmpty() { while (!this.eof && this.bufferLength === 0) { this.readBlock(); diff --git a/src/core/jbig2_stream.js b/src/core/jbig2_stream.js index fa6c2301a..75d94c9fc 100644 --- a/src/core/jbig2_stream.js +++ b/src/core/jbig2_stream.js @@ -13,11 +13,11 @@ * limitations under the License. */ -import { shadow, unreachable } from "../shared/util.js"; import { BaseStream } from "./base_stream.js"; import { DecodeStream } from "./decode_stream.js"; import { Dict } from "./primitives.js"; import { JBig2CCITTFaxImage } from "./jbig2_ccittFax.js"; +import { shadow } from "../shared/util.js"; /** * For JBIG2's we use a library to decode these images and @@ -43,10 +43,6 @@ class Jbig2Stream extends DecodeStream { // directly insert all of its data into `this.buffer`. } - readBlock() { - unreachable("Jbig2Stream.readBlock"); - } - get isAsyncDecoder() { return true; } diff --git a/src/core/jpx_stream.js b/src/core/jpx_stream.js index 15708f000..ded33062b 100644 --- a/src/core/jpx_stream.js +++ b/src/core/jpx_stream.js @@ -13,9 +13,9 @@ * limitations under the License. */ -import { shadow, unreachable } from "../shared/util.js"; import { DecodeStream } from "./decode_stream.js"; import { JpxImage } from "./jpx.js"; +import { shadow } from "../shared/util.js"; /** * For JPEG 2000's we use a library to decode these images and @@ -41,10 +41,6 @@ class JpxStream extends DecodeStream { // directly insert all of its data into `this.buffer`. } - readBlock(decoderOptions) { - unreachable("JpxStream.readBlock"); - } - get isAsyncDecoder() { return true; }