UNPKG

1.97 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.RunLengthStream = void 0;
28
29var _decode_stream = require("./decode_stream.js");
30
31class RunLengthStream extends _decode_stream.DecodeStream {
32 constructor(str, maybeLength) {
33 super(maybeLength);
34 this.str = str;
35 this.dict = str.dict;
36 }
37
38 readBlock() {
39 const repeatHeader = this.str.getBytes(2);
40
41 if (!repeatHeader || repeatHeader.length < 2 || repeatHeader[0] === 128) {
42 this.eof = true;
43 return;
44 }
45
46 let buffer;
47 let bufferLength = this.bufferLength;
48 let n = repeatHeader[0];
49
50 if (n < 128) {
51 buffer = this.ensureBuffer(bufferLength + n + 1);
52 buffer[bufferLength++] = repeatHeader[1];
53
54 if (n > 0) {
55 const source = this.str.getBytes(n);
56 buffer.set(source, bufferLength);
57 bufferLength += n;
58 }
59 } else {
60 n = 257 - n;
61 const b = repeatHeader[1];
62 buffer = this.ensureBuffer(bufferLength + n + 1);
63
64 for (let i = 0; i < n; i++) {
65 buffer[bufferLength++] = b;
66 }
67 }
68
69 this.bufferLength = bufferLength;
70 }
71
72}
73
74exports.RunLengthStream = RunLengthStream;
\No newline at end of file