UNPKG

4.51 kBJavaScriptView Raw
1/**
2 * @licstart The following is the entire license notice for the
3 * JavaScript code in this page
4 *
5 * Copyright 2022 Mozilla Foundation
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * @licend The above is the entire license notice for the
20 * JavaScript code in this page
21 */
22"use strict";
23
24Object.defineProperty(exports, "__esModule", {
25 value: true
26});
27exports.StreamsSequenceStream = exports.DecodeStream = void 0;
28
29var _base_stream = require("./base_stream.js");
30
31var _stream = require("./stream.js");
32
33const emptyBuffer = new Uint8Array(0);
34
35class DecodeStream extends _base_stream.BaseStream {
36 constructor(maybeMinBufferLength) {
37 super();
38 this._rawMinBufferLength = maybeMinBufferLength || 0;
39 this.pos = 0;
40 this.bufferLength = 0;
41 this.eof = false;
42 this.buffer = emptyBuffer;
43 this.minBufferLength = 512;
44
45 if (maybeMinBufferLength) {
46 while (this.minBufferLength < maybeMinBufferLength) {
47 this.minBufferLength *= 2;
48 }
49 }
50 }
51
52 get isEmpty() {
53 while (!this.eof && this.bufferLength === 0) {
54 this.readBlock();
55 }
56
57 return this.bufferLength === 0;
58 }
59
60 ensureBuffer(requested) {
61 const buffer = this.buffer;
62
63 if (requested <= buffer.byteLength) {
64 return buffer;
65 }
66
67 let size = this.minBufferLength;
68
69 while (size < requested) {
70 size *= 2;
71 }
72
73 const buffer2 = new Uint8Array(size);
74 buffer2.set(buffer);
75 return this.buffer = buffer2;
76 }
77
78 getByte() {
79 const pos = this.pos;
80
81 while (this.bufferLength <= pos) {
82 if (this.eof) {
83 return -1;
84 }
85
86 this.readBlock();
87 }
88
89 return this.buffer[this.pos++];
90 }
91
92 getBytes(length) {
93 const pos = this.pos;
94 let end;
95
96 if (length) {
97 this.ensureBuffer(pos + length);
98 end = pos + length;
99
100 while (!this.eof && this.bufferLength < end) {
101 this.readBlock();
102 }
103
104 const bufEnd = this.bufferLength;
105
106 if (end > bufEnd) {
107 end = bufEnd;
108 }
109 } else {
110 while (!this.eof) {
111 this.readBlock();
112 }
113
114 end = this.bufferLength;
115 }
116
117 this.pos = end;
118 return this.buffer.subarray(pos, end);
119 }
120
121 reset() {
122 this.pos = 0;
123 }
124
125 makeSubStream(start, length, dict = null) {
126 if (length === undefined) {
127 while (!this.eof) {
128 this.readBlock();
129 }
130 } else {
131 const end = start + length;
132
133 while (this.bufferLength <= end && !this.eof) {
134 this.readBlock();
135 }
136 }
137
138 return new _stream.Stream(this.buffer, start, length, dict);
139 }
140
141 getBaseStreams() {
142 return this.str ? this.str.getBaseStreams() : null;
143 }
144
145}
146
147exports.DecodeStream = DecodeStream;
148
149class StreamsSequenceStream extends DecodeStream {
150 constructor(streams, onError = null) {
151 let maybeLength = 0;
152
153 for (const stream of streams) {
154 maybeLength += stream instanceof DecodeStream ? stream._rawMinBufferLength : stream.length;
155 }
156
157 super(maybeLength);
158 this.streams = streams;
159 this._onError = onError;
160 }
161
162 readBlock() {
163 const streams = this.streams;
164
165 if (streams.length === 0) {
166 this.eof = true;
167 return;
168 }
169
170 const stream = streams.shift();
171 let chunk;
172
173 try {
174 chunk = stream.getBytes();
175 } catch (reason) {
176 if (this._onError) {
177 this._onError(reason, stream.dict && stream.dict.objId);
178
179 return;
180 }
181
182 throw reason;
183 }
184
185 const bufferLength = this.bufferLength;
186 const newLength = bufferLength + chunk.length;
187 const buffer = this.ensureBuffer(newLength);
188 buffer.set(chunk, bufferLength);
189 this.bufferLength = newLength;
190 }
191
192 getBaseStreams() {
193 const baseStreamsBuf = [];
194
195 for (const stream of this.streams) {
196 const baseStreams = stream.getBaseStreams();
197
198 if (baseStreams) {
199 baseStreamsBuf.push(...baseStreams);
200 }
201 }
202
203 return baseStreamsBuf.length > 0 ? baseStreamsBuf : null;
204 }
205
206}
207
208exports.StreamsSequenceStream = StreamsSequenceStream;
\No newline at end of file