From 05f368056d46dcb12dbb234ceb63015b5b95ef2b Mon Sep 17 00:00:00 2001 From: Calixte Denizet Date: Fri, 17 Oct 2025 18:05:37 +0200 Subject: [PATCH] Use stream for whatever substrem in stream classes and add a method in order to get the original stream. When writing an existing stream it'll help to have the original one instead of the filtered one. --- src/core/ascii_85_stream.js | 4 ++-- src/core/ascii_hex_stream.js | 4 ++-- src/core/base_stream.js | 4 ++++ src/core/ccitt_stream.js | 2 +- src/core/decode_stream.js | 2 +- src/core/decrypt_stream.js | 6 +++--- src/core/flate_stream.js | 16 ++++++++-------- src/core/lzw_stream.js | 4 ++-- src/core/predictor_stream.js | 8 ++++---- src/core/run_length_stream.js | 6 +++--- 10 files changed, 30 insertions(+), 26 deletions(-) diff --git a/src/core/ascii_85_stream.js b/src/core/ascii_85_stream.js index 9cabbc430..0bbcec999 100644 --- a/src/core/ascii_85_stream.js +++ b/src/core/ascii_85_stream.js @@ -25,7 +25,7 @@ class Ascii85Stream extends DecodeStream { } super(maybeLength); - this.str = str; + this.stream = str; this.dict = str.dict; this.input = new Uint8Array(5); } @@ -35,7 +35,7 @@ class Ascii85Stream extends DecodeStream { const Z_LOWER_CHAR = 0x7a; // 'z' const EOF = -1; - const str = this.str; + const str = this.stream; let c = str.getByte(); while (isWhiteSpace(c)) { diff --git a/src/core/ascii_hex_stream.js b/src/core/ascii_hex_stream.js index 60fe007e6..4dec730a6 100644 --- a/src/core/ascii_hex_stream.js +++ b/src/core/ascii_hex_stream.js @@ -24,7 +24,7 @@ class AsciiHexStream extends DecodeStream { } super(maybeLength); - this.str = str; + this.stream = str; this.dict = str.dict; this.firstDigit = -1; @@ -32,7 +32,7 @@ class AsciiHexStream extends DecodeStream { readBlock() { const UPSTREAM_BLOCK_SIZE = 8000; - const bytes = this.str.getBytes(UPSTREAM_BLOCK_SIZE); + const bytes = this.stream.getBytes(UPSTREAM_BLOCK_SIZE); if (!bytes.length) { this.eof = true; return; diff --git a/src/core/base_stream.js b/src/core/base_stream.js index 0266449a1..a5a0eb1b5 100644 --- a/src/core/base_stream.js +++ b/src/core/base_stream.js @@ -137,6 +137,10 @@ class BaseStream { getBaseStreams() { return null; } + + getOriginalStream() { + return this.stream?.getOriginalStream() || this; + } } export { BaseStream }; diff --git a/src/core/ccitt_stream.js b/src/core/ccitt_stream.js index 0e3b1cea3..2bb80f3af 100644 --- a/src/core/ccitt_stream.js +++ b/src/core/ccitt_stream.js @@ -21,7 +21,7 @@ class CCITTFaxStream extends DecodeStream { constructor(str, maybeLength, params) { super(maybeLength); - this.str = str; + this.stream = str; this.dict = str.dict; if (!(params instanceof Dict)) { diff --git a/src/core/decode_stream.js b/src/core/decode_stream.js index ef2bab40b..80bdcebd0 100644 --- a/src/core/decode_stream.js +++ b/src/core/decode_stream.js @@ -129,7 +129,7 @@ class DecodeStream extends BaseStream { } getBaseStreams() { - return this.str ? this.str.getBaseStreams() : null; + return this.stream ? this.stream.getBaseStreams() : null; } } diff --git a/src/core/decrypt_stream.js b/src/core/decrypt_stream.js index ee2fff21f..8e93b9f86 100644 --- a/src/core/decrypt_stream.js +++ b/src/core/decrypt_stream.js @@ -21,7 +21,7 @@ class DecryptStream extends DecodeStream { constructor(str, maybeLength, decrypt) { super(maybeLength); - this.str = str; + this.stream = str; this.dict = str.dict; this.decrypt = decrypt; this.nextChunk = null; @@ -33,14 +33,14 @@ class DecryptStream extends DecodeStream { if (this.initialized) { chunk = this.nextChunk; } else { - chunk = this.str.getBytes(chunkSize); + chunk = this.stream.getBytes(chunkSize); this.initialized = true; } if (!chunk?.length) { this.eof = true; return; } - this.nextChunk = this.str.getBytes(chunkSize); + this.nextChunk = this.stream.getBytes(chunkSize); const hasMoreData = this.nextChunk?.length > 0; const decrypt = this.decrypt; diff --git a/src/core/flate_stream.js b/src/core/flate_stream.js index bc98ae1ae..bdb9d223d 100644 --- a/src/core/flate_stream.js +++ b/src/core/flate_stream.js @@ -125,7 +125,7 @@ class FlateStream extends DecodeStream { constructor(str, maybeLength) { super(maybeLength); - this.str = str; + this.stream = str; this.dict = str.dict; const cmf = str.getByte(); @@ -161,8 +161,8 @@ class FlateStream extends DecodeStream { } async asyncGetBytes() { - this.str.reset(); - const bytes = this.str.getBytes(); + this.stream.reset(); + const bytes = this.stream.getBytes(); try { const { readable, writable } = new DecompressionStream("deflate"); @@ -200,11 +200,11 @@ class FlateStream extends DecodeStream { // decoder. // We already get the bytes from the underlying stream, so we just reuse // them to avoid get them again. - this.str = new Stream( + this.stream = new Stream( bytes, 2 /* = header size (see ctor) */, bytes.length, - this.str.dict + this.stream.dict ); this.reset(); return null; @@ -216,7 +216,7 @@ class FlateStream extends DecodeStream { } getBits(bits) { - const str = this.str; + const str = this.stream; let codeSize = this.codeSize; let codeBuf = this.codeBuf; @@ -236,7 +236,7 @@ class FlateStream extends DecodeStream { } getCode(table) { - const str = this.str; + const str = this.stream; const codes = table[0]; const maxLen = table[1]; let codeSize = this.codeSize; @@ -312,7 +312,7 @@ class FlateStream extends DecodeStream { readBlock() { let buffer, hdr, len; - const str = this.str; + const str = this.stream; // read block header try { hdr = this.getBits(3); diff --git a/src/core/lzw_stream.js b/src/core/lzw_stream.js index 9a0ae7448..7971aeb26 100644 --- a/src/core/lzw_stream.js +++ b/src/core/lzw_stream.js @@ -19,7 +19,7 @@ class LZWStream extends DecodeStream { constructor(str, maybeLength, earlyChange) { super(maybeLength); - this.str = str; + this.stream = str; this.dict = str.dict; this.cachedData = 0; this.bitsCached = 0; @@ -46,7 +46,7 @@ class LZWStream extends DecodeStream { let bitsCached = this.bitsCached; let cachedData = this.cachedData; while (bitsCached < n) { - const c = this.str.getByte(); + const c = this.stream.getByte(); if (c === -1) { this.eof = true; return null; diff --git a/src/core/predictor_stream.js b/src/core/predictor_stream.js index ac807a3b3..9623901da 100644 --- a/src/core/predictor_stream.js +++ b/src/core/predictor_stream.js @@ -35,7 +35,7 @@ class PredictorStream extends DecodeStream { this.readBlock = predictor === 2 ? this.readBlockTiff : this.readBlockPng; - this.str = str; + this.stream = str; this.dict = str.dict; const colors = (this.colors = params.get("Colors") || 1); @@ -57,7 +57,7 @@ class PredictorStream extends DecodeStream { const bits = this.bits; const colors = this.colors; - const rawBytes = this.str.getBytes(rowBytes); + const rawBytes = this.stream.getBytes(rowBytes); this.eof = !rawBytes.length; if (this.eof) { return; @@ -138,8 +138,8 @@ class PredictorStream extends DecodeStream { const rowBytes = this.rowBytes; const pixBytes = this.pixBytes; - const predictor = this.str.getByte(); - const rawBytes = this.str.getBytes(rowBytes); + const predictor = this.stream.getByte(); + const rawBytes = this.stream.getBytes(rowBytes); this.eof = !rawBytes.length; if (this.eof) { return; diff --git a/src/core/run_length_stream.js b/src/core/run_length_stream.js index 72e66daa2..db664b733 100644 --- a/src/core/run_length_stream.js +++ b/src/core/run_length_stream.js @@ -19,7 +19,7 @@ class RunLengthStream extends DecodeStream { constructor(str, maybeLength) { super(maybeLength); - this.str = str; + this.stream = str; this.dict = str.dict; } @@ -28,7 +28,7 @@ class RunLengthStream extends DecodeStream { // and amount of bytes to repeat/copy: n = 0 through 127 - copy next n bytes // (in addition to the second byte from the header), n = 129 through 255 - // duplicate the second byte from the header (257 - n) times, n = 128 - end. - const repeatHeader = this.str.getBytes(2); + const repeatHeader = this.stream.getBytes(2); if (!repeatHeader || repeatHeader.length < 2 || repeatHeader[0] === 128) { this.eof = true; return; @@ -42,7 +42,7 @@ class RunLengthStream extends DecodeStream { buffer = this.ensureBuffer(bufferLength + n + 1); buffer[bufferLength++] = repeatHeader[1]; if (n > 0) { - const source = this.str.getBytes(n); + const source = this.stream.getBytes(n); buffer.set(source, bufferLength); bufferLength += n; }