Skip to content

Commit 05f3680

Browse files
committed
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.
1 parent 928a758 commit 05f3680

File tree

10 files changed

+30
-26
lines changed

10 files changed

+30
-26
lines changed

src/core/ascii_85_stream.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Ascii85Stream extends DecodeStream {
2525
}
2626
super(maybeLength);
2727

28-
this.str = str;
28+
this.stream = str;
2929
this.dict = str.dict;
3030
this.input = new Uint8Array(5);
3131
}
@@ -35,7 +35,7 @@ class Ascii85Stream extends DecodeStream {
3535
const Z_LOWER_CHAR = 0x7a; // 'z'
3636
const EOF = -1;
3737

38-
const str = this.str;
38+
const str = this.stream;
3939

4040
let c = str.getByte();
4141
while (isWhiteSpace(c)) {

src/core/ascii_hex_stream.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ class AsciiHexStream extends DecodeStream {
2424
}
2525
super(maybeLength);
2626

27-
this.str = str;
27+
this.stream = str;
2828
this.dict = str.dict;
2929

3030
this.firstDigit = -1;
3131
}
3232

3333
readBlock() {
3434
const UPSTREAM_BLOCK_SIZE = 8000;
35-
const bytes = this.str.getBytes(UPSTREAM_BLOCK_SIZE);
35+
const bytes = this.stream.getBytes(UPSTREAM_BLOCK_SIZE);
3636
if (!bytes.length) {
3737
this.eof = true;
3838
return;

src/core/base_stream.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ class BaseStream {
137137
getBaseStreams() {
138138
return null;
139139
}
140+
141+
getOriginalStream() {
142+
return this.stream?.getOriginalStream() || this;
143+
}
140144
}
141145

142146
export { BaseStream };

src/core/ccitt_stream.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class CCITTFaxStream extends DecodeStream {
2121
constructor(str, maybeLength, params) {
2222
super(maybeLength);
2323

24-
this.str = str;
24+
this.stream = str;
2525
this.dict = str.dict;
2626

2727
if (!(params instanceof Dict)) {

src/core/decode_stream.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ class DecodeStream extends BaseStream {
129129
}
130130

131131
getBaseStreams() {
132-
return this.str ? this.str.getBaseStreams() : null;
132+
return this.stream ? this.stream.getBaseStreams() : null;
133133
}
134134
}
135135

src/core/decrypt_stream.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class DecryptStream extends DecodeStream {
2121
constructor(str, maybeLength, decrypt) {
2222
super(maybeLength);
2323

24-
this.str = str;
24+
this.stream = str;
2525
this.dict = str.dict;
2626
this.decrypt = decrypt;
2727
this.nextChunk = null;
@@ -33,14 +33,14 @@ class DecryptStream extends DecodeStream {
3333
if (this.initialized) {
3434
chunk = this.nextChunk;
3535
} else {
36-
chunk = this.str.getBytes(chunkSize);
36+
chunk = this.stream.getBytes(chunkSize);
3737
this.initialized = true;
3838
}
3939
if (!chunk?.length) {
4040
this.eof = true;
4141
return;
4242
}
43-
this.nextChunk = this.str.getBytes(chunkSize);
43+
this.nextChunk = this.stream.getBytes(chunkSize);
4444
const hasMoreData = this.nextChunk?.length > 0;
4545

4646
const decrypt = this.decrypt;

src/core/flate_stream.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ class FlateStream extends DecodeStream {
125125
constructor(str, maybeLength) {
126126
super(maybeLength);
127127

128-
this.str = str;
128+
this.stream = str;
129129
this.dict = str.dict;
130130

131131
const cmf = str.getByte();
@@ -161,8 +161,8 @@ class FlateStream extends DecodeStream {
161161
}
162162

163163
async asyncGetBytes() {
164-
this.str.reset();
165-
const bytes = this.str.getBytes();
164+
this.stream.reset();
165+
const bytes = this.stream.getBytes();
166166

167167
try {
168168
const { readable, writable } = new DecompressionStream("deflate");
@@ -200,11 +200,11 @@ class FlateStream extends DecodeStream {
200200
// decoder.
201201
// We already get the bytes from the underlying stream, so we just reuse
202202
// them to avoid get them again.
203-
this.str = new Stream(
203+
this.stream = new Stream(
204204
bytes,
205205
2 /* = header size (see ctor) */,
206206
bytes.length,
207-
this.str.dict
207+
this.stream.dict
208208
);
209209
this.reset();
210210
return null;
@@ -216,7 +216,7 @@ class FlateStream extends DecodeStream {
216216
}
217217

218218
getBits(bits) {
219-
const str = this.str;
219+
const str = this.stream;
220220
let codeSize = this.codeSize;
221221
let codeBuf = this.codeBuf;
222222

@@ -236,7 +236,7 @@ class FlateStream extends DecodeStream {
236236
}
237237

238238
getCode(table) {
239-
const str = this.str;
239+
const str = this.stream;
240240
const codes = table[0];
241241
const maxLen = table[1];
242242
let codeSize = this.codeSize;
@@ -312,7 +312,7 @@ class FlateStream extends DecodeStream {
312312

313313
readBlock() {
314314
let buffer, hdr, len;
315-
const str = this.str;
315+
const str = this.stream;
316316
// read block header
317317
try {
318318
hdr = this.getBits(3);

src/core/lzw_stream.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class LZWStream extends DecodeStream {
1919
constructor(str, maybeLength, earlyChange) {
2020
super(maybeLength);
2121

22-
this.str = str;
22+
this.stream = str;
2323
this.dict = str.dict;
2424
this.cachedData = 0;
2525
this.bitsCached = 0;
@@ -46,7 +46,7 @@ class LZWStream extends DecodeStream {
4646
let bitsCached = this.bitsCached;
4747
let cachedData = this.cachedData;
4848
while (bitsCached < n) {
49-
const c = this.str.getByte();
49+
const c = this.stream.getByte();
5050
if (c === -1) {
5151
this.eof = true;
5252
return null;

src/core/predictor_stream.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class PredictorStream extends DecodeStream {
3535

3636
this.readBlock = predictor === 2 ? this.readBlockTiff : this.readBlockPng;
3737

38-
this.str = str;
38+
this.stream = str;
3939
this.dict = str.dict;
4040

4141
const colors = (this.colors = params.get("Colors") || 1);
@@ -57,7 +57,7 @@ class PredictorStream extends DecodeStream {
5757
const bits = this.bits;
5858
const colors = this.colors;
5959

60-
const rawBytes = this.str.getBytes(rowBytes);
60+
const rawBytes = this.stream.getBytes(rowBytes);
6161
this.eof = !rawBytes.length;
6262
if (this.eof) {
6363
return;
@@ -138,8 +138,8 @@ class PredictorStream extends DecodeStream {
138138
const rowBytes = this.rowBytes;
139139
const pixBytes = this.pixBytes;
140140

141-
const predictor = this.str.getByte();
142-
const rawBytes = this.str.getBytes(rowBytes);
141+
const predictor = this.stream.getByte();
142+
const rawBytes = this.stream.getBytes(rowBytes);
143143
this.eof = !rawBytes.length;
144144
if (this.eof) {
145145
return;

src/core/run_length_stream.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class RunLengthStream extends DecodeStream {
1919
constructor(str, maybeLength) {
2020
super(maybeLength);
2121

22-
this.str = str;
22+
this.stream = str;
2323
this.dict = str.dict;
2424
}
2525

@@ -28,7 +28,7 @@ class RunLengthStream extends DecodeStream {
2828
// and amount of bytes to repeat/copy: n = 0 through 127 - copy next n bytes
2929
// (in addition to the second byte from the header), n = 129 through 255 -
3030
// duplicate the second byte from the header (257 - n) times, n = 128 - end.
31-
const repeatHeader = this.str.getBytes(2);
31+
const repeatHeader = this.stream.getBytes(2);
3232
if (!repeatHeader || repeatHeader.length < 2 || repeatHeader[0] === 128) {
3333
this.eof = true;
3434
return;
@@ -42,7 +42,7 @@ class RunLengthStream extends DecodeStream {
4242
buffer = this.ensureBuffer(bufferLength + n + 1);
4343
buffer[bufferLength++] = repeatHeader[1];
4444
if (n > 0) {
45-
const source = this.str.getBytes(n);
45+
const source = this.stream.getBytes(n);
4646
buffer.set(source, bufferLength);
4747
bufferLength += n;
4848
}

0 commit comments

Comments
 (0)