forked from Hopding/pdf-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDecodeStream.ts
170 lines (147 loc) · 4.04 KB
/
DecodeStream.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { MethodNotImplementedError } from 'src/core/errors';
import Stream, { StreamType } from 'src/core/streams/Stream';
/*
* Copyright 2012 Mozilla Foundation
*
* The DecodeStream class contained in this file is a TypeScript port of the
* JavaScript DecodeStream class in Mozilla's pdf.js project, made available
* under the Apache 2.0 open source license.
*/
// Lots of DecodeStreams are created whose buffers are never used. For these
// we share a single empty buffer. This is (a) space-efficient and (b) avoids
// having special cases that would be required if we used |null| for an empty
// buffer.
const emptyBuffer = new Uint8Array(0);
/**
* Super class for the decoding streams
*/
class DecodeStream implements StreamType {
protected bufferLength: number;
protected buffer: Uint8Array;
protected eof: boolean;
private pos: number;
private minBufferLength: number;
constructor(maybeMinBufferLength?: number) {
this.pos = 0;
this.bufferLength = 0;
this.eof = false;
this.buffer = emptyBuffer;
this.minBufferLength = 512;
if (maybeMinBufferLength) {
// Compute the first power of two that is as big as maybeMinBufferLength.
while (this.minBufferLength < maybeMinBufferLength) {
this.minBufferLength *= 2;
}
}
}
get isEmpty() {
while (!this.eof && this.bufferLength === 0) {
this.readBlock();
}
return this.bufferLength === 0;
}
getByte() {
const pos = this.pos;
while (this.bufferLength <= pos) {
if (this.eof) {
return -1;
}
this.readBlock();
}
return this.buffer[this.pos++];
}
getUint16() {
const b0 = this.getByte();
const b1 = this.getByte();
if (b0 === -1 || b1 === -1) {
return -1;
}
return (b0 << 8) + b1;
}
getInt32() {
const b0 = this.getByte();
const b1 = this.getByte();
const b2 = this.getByte();
const b3 = this.getByte();
return (b0 << 24) + (b1 << 16) + (b2 << 8) + b3;
}
getBytes(length: number, forceClamped = false) {
let end;
const pos = this.pos;
if (length) {
this.ensureBuffer(pos + length);
end = pos + length;
while (!this.eof && this.bufferLength < end) {
this.readBlock();
}
const bufEnd = this.bufferLength;
if (end > bufEnd) {
end = bufEnd;
}
} else {
while (!this.eof) {
this.readBlock();
}
end = this.bufferLength;
}
this.pos = end;
const subarray = this.buffer.subarray(pos, end);
// `this.buffer` is either a `Uint8Array` or `Uint8ClampedArray` here.
return forceClamped && !(subarray instanceof Uint8ClampedArray)
? new Uint8ClampedArray(subarray)
: subarray;
}
peekByte() {
const peekedByte = this.getByte();
this.pos--;
return peekedByte;
}
peekBytes(length: number, forceClamped = false) {
const bytes = this.getBytes(length, forceClamped);
this.pos -= bytes.length;
return bytes;
}
skip(n: number) {
if (!n) {
n = 1;
}
this.pos += n;
}
reset() {
this.pos = 0;
}
makeSubStream(start: number, length: number /* dict */) {
const end = start + length;
while (this.bufferLength <= end && !this.eof) {
this.readBlock();
}
return new Stream(this.buffer, start, length /* dict */);
}
decode(): Uint8Array {
while (!this.eof) this.readBlock();
return this.buffer.subarray(0, this.bufferLength);
}
protected readBlock(): void {
throw new MethodNotImplementedError(this.constructor.name, 'readBlock');
}
protected ensureBuffer(requested: number) {
const buffer = this.buffer;
if (requested <= buffer.byteLength) {
return buffer;
}
let size = this.minBufferLength;
while (size < requested) {
size *= 2;
}
const buffer2 = new Uint8Array(size);
buffer2.set(buffer);
return (this.buffer = buffer2);
}
// getBaseStreams() {
// if (this.str && this.str.getBaseStreams) {
// return this.str.getBaseStreams();
// }
// return [];
// }
}
export default DecodeStream;