Merge pull request #20373 from calixteman/get_original_stream

Use stream for whatever substrem in stream classes
This commit is contained in:
calixteman 2025-10-18 14:53:49 +02:00 committed by GitHub
commit b0e8c39f31
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 30 additions and 26 deletions

View File

@ -25,7 +25,7 @@ class Ascii85Stream extends DecodeStream {
} }
super(maybeLength); super(maybeLength);
this.str = str; this.stream = str;
this.dict = str.dict; this.dict = str.dict;
this.input = new Uint8Array(5); this.input = new Uint8Array(5);
} }
@ -35,7 +35,7 @@ class Ascii85Stream extends DecodeStream {
const Z_LOWER_CHAR = 0x7a; // 'z' const Z_LOWER_CHAR = 0x7a; // 'z'
const EOF = -1; const EOF = -1;
const str = this.str; const str = this.stream;
let c = str.getByte(); let c = str.getByte();
while (isWhiteSpace(c)) { while (isWhiteSpace(c)) {

View File

@ -24,7 +24,7 @@ class AsciiHexStream extends DecodeStream {
} }
super(maybeLength); super(maybeLength);
this.str = str; this.stream = str;
this.dict = str.dict; this.dict = str.dict;
this.firstDigit = -1; this.firstDigit = -1;
@ -32,7 +32,7 @@ class AsciiHexStream extends DecodeStream {
readBlock() { readBlock() {
const UPSTREAM_BLOCK_SIZE = 8000; const UPSTREAM_BLOCK_SIZE = 8000;
const bytes = this.str.getBytes(UPSTREAM_BLOCK_SIZE); const bytes = this.stream.getBytes(UPSTREAM_BLOCK_SIZE);
if (!bytes.length) { if (!bytes.length) {
this.eof = true; this.eof = true;
return; return;

View File

@ -137,6 +137,10 @@ class BaseStream {
getBaseStreams() { getBaseStreams() {
return null; return null;
} }
getOriginalStream() {
return this.stream?.getOriginalStream() || this;
}
} }
export { BaseStream }; export { BaseStream };

View File

@ -21,7 +21,7 @@ class CCITTFaxStream extends DecodeStream {
constructor(str, maybeLength, params) { constructor(str, maybeLength, params) {
super(maybeLength); super(maybeLength);
this.str = str; this.stream = str;
this.dict = str.dict; this.dict = str.dict;
if (!(params instanceof Dict)) { if (!(params instanceof Dict)) {

View File

@ -129,7 +129,7 @@ class DecodeStream extends BaseStream {
} }
getBaseStreams() { getBaseStreams() {
return this.str ? this.str.getBaseStreams() : null; return this.stream ? this.stream.getBaseStreams() : null;
} }
} }

View File

@ -21,7 +21,7 @@ class DecryptStream extends DecodeStream {
constructor(str, maybeLength, decrypt) { constructor(str, maybeLength, decrypt) {
super(maybeLength); super(maybeLength);
this.str = str; this.stream = str;
this.dict = str.dict; this.dict = str.dict;
this.decrypt = decrypt; this.decrypt = decrypt;
this.nextChunk = null; this.nextChunk = null;
@ -33,14 +33,14 @@ class DecryptStream extends DecodeStream {
if (this.initialized) { if (this.initialized) {
chunk = this.nextChunk; chunk = this.nextChunk;
} else { } else {
chunk = this.str.getBytes(chunkSize); chunk = this.stream.getBytes(chunkSize);
this.initialized = true; this.initialized = true;
} }
if (!chunk?.length) { if (!chunk?.length) {
this.eof = true; this.eof = true;
return; return;
} }
this.nextChunk = this.str.getBytes(chunkSize); this.nextChunk = this.stream.getBytes(chunkSize);
const hasMoreData = this.nextChunk?.length > 0; const hasMoreData = this.nextChunk?.length > 0;
const decrypt = this.decrypt; const decrypt = this.decrypt;

View File

@ -125,7 +125,7 @@ class FlateStream extends DecodeStream {
constructor(str, maybeLength) { constructor(str, maybeLength) {
super(maybeLength); super(maybeLength);
this.str = str; this.stream = str;
this.dict = str.dict; this.dict = str.dict;
const cmf = str.getByte(); const cmf = str.getByte();
@ -161,8 +161,8 @@ class FlateStream extends DecodeStream {
} }
async asyncGetBytes() { async asyncGetBytes() {
this.str.reset(); this.stream.reset();
const bytes = this.str.getBytes(); const bytes = this.stream.getBytes();
try { try {
const { readable, writable } = new DecompressionStream("deflate"); const { readable, writable } = new DecompressionStream("deflate");
@ -200,11 +200,11 @@ class FlateStream extends DecodeStream {
// decoder. // decoder.
// We already get the bytes from the underlying stream, so we just reuse // We already get the bytes from the underlying stream, so we just reuse
// them to avoid get them again. // them to avoid get them again.
this.str = new Stream( this.stream = new Stream(
bytes, bytes,
2 /* = header size (see ctor) */, 2 /* = header size (see ctor) */,
bytes.length, bytes.length,
this.str.dict this.stream.dict
); );
this.reset(); this.reset();
return null; return null;
@ -216,7 +216,7 @@ class FlateStream extends DecodeStream {
} }
getBits(bits) { getBits(bits) {
const str = this.str; const str = this.stream;
let codeSize = this.codeSize; let codeSize = this.codeSize;
let codeBuf = this.codeBuf; let codeBuf = this.codeBuf;
@ -236,7 +236,7 @@ class FlateStream extends DecodeStream {
} }
getCode(table) { getCode(table) {
const str = this.str; const str = this.stream;
const codes = table[0]; const codes = table[0];
const maxLen = table[1]; const maxLen = table[1];
let codeSize = this.codeSize; let codeSize = this.codeSize;
@ -312,7 +312,7 @@ class FlateStream extends DecodeStream {
readBlock() { readBlock() {
let buffer, hdr, len; let buffer, hdr, len;
const str = this.str; const str = this.stream;
// read block header // read block header
try { try {
hdr = this.getBits(3); hdr = this.getBits(3);

View File

@ -19,7 +19,7 @@ class LZWStream extends DecodeStream {
constructor(str, maybeLength, earlyChange) { constructor(str, maybeLength, earlyChange) {
super(maybeLength); super(maybeLength);
this.str = str; this.stream = str;
this.dict = str.dict; this.dict = str.dict;
this.cachedData = 0; this.cachedData = 0;
this.bitsCached = 0; this.bitsCached = 0;
@ -46,7 +46,7 @@ class LZWStream extends DecodeStream {
let bitsCached = this.bitsCached; let bitsCached = this.bitsCached;
let cachedData = this.cachedData; let cachedData = this.cachedData;
while (bitsCached < n) { while (bitsCached < n) {
const c = this.str.getByte(); const c = this.stream.getByte();
if (c === -1) { if (c === -1) {
this.eof = true; this.eof = true;
return null; return null;

View File

@ -35,7 +35,7 @@ class PredictorStream extends DecodeStream {
this.readBlock = predictor === 2 ? this.readBlockTiff : this.readBlockPng; this.readBlock = predictor === 2 ? this.readBlockTiff : this.readBlockPng;
this.str = str; this.stream = str;
this.dict = str.dict; this.dict = str.dict;
const colors = (this.colors = params.get("Colors") || 1); const colors = (this.colors = params.get("Colors") || 1);
@ -57,7 +57,7 @@ class PredictorStream extends DecodeStream {
const bits = this.bits; const bits = this.bits;
const colors = this.colors; const colors = this.colors;
const rawBytes = this.str.getBytes(rowBytes); const rawBytes = this.stream.getBytes(rowBytes);
this.eof = !rawBytes.length; this.eof = !rawBytes.length;
if (this.eof) { if (this.eof) {
return; return;
@ -138,8 +138,8 @@ class PredictorStream extends DecodeStream {
const rowBytes = this.rowBytes; const rowBytes = this.rowBytes;
const pixBytes = this.pixBytes; const pixBytes = this.pixBytes;
const predictor = this.str.getByte(); const predictor = this.stream.getByte();
const rawBytes = this.str.getBytes(rowBytes); const rawBytes = this.stream.getBytes(rowBytes);
this.eof = !rawBytes.length; this.eof = !rawBytes.length;
if (this.eof) { if (this.eof) {
return; return;

View File

@ -19,7 +19,7 @@ class RunLengthStream extends DecodeStream {
constructor(str, maybeLength) { constructor(str, maybeLength) {
super(maybeLength); super(maybeLength);
this.str = str; this.stream = str;
this.dict = str.dict; 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 // 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 - // (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. // 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) { if (!repeatHeader || repeatHeader.length < 2 || repeatHeader[0] === 128) {
this.eof = true; this.eof = true;
return; return;
@ -42,7 +42,7 @@ class RunLengthStream extends DecodeStream {
buffer = this.ensureBuffer(bufferLength + n + 1); buffer = this.ensureBuffer(bufferLength + n + 1);
buffer[bufferLength++] = repeatHeader[1]; buffer[bufferLength++] = repeatHeader[1];
if (n > 0) { if (n > 0) {
const source = this.str.getBytes(n); const source = this.stream.getBytes(n);
buffer.set(source, bufferLength); buffer.set(source, bufferLength);
bufferLength += n; bufferLength += n;
} }