forked from Hopding/pdf-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStream.ts
132 lines (114 loc) · 2.89 KB
/
Stream.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
/*
* Copyright 2012 Mozilla Foundation
*
* The Stream class contained in this file is a TypeScript port of the
* JavaScript Stream class in Mozilla's pdf.js project, made available
* under the Apache 2.0 open source license.
*/
export interface StreamType {
isEmpty: boolean;
getByte(): number;
getUint16(): number;
getInt32(): number;
getBytes(
length: number,
forceClamped?: boolean,
): Uint8Array | Uint8ClampedArray;
peekByte(): number;
peekBytes(
length: number,
forceClamped?: boolean,
): Uint8Array | Uint8ClampedArray;
skip(n: number): void;
reset(): void;
makeSubStream(start: number, length: number): StreamType;
decode(): Uint8Array;
}
class Stream implements StreamType {
private bytes: Uint8Array;
private start: number;
private pos: number;
private end: number;
constructor(buffer: Uint8Array, start?: number, length?: number) {
this.bytes = buffer;
this.start = start || 0;
this.pos = this.start;
this.end = !!start && !!length ? start + length : this.bytes.length;
}
get length() {
return this.end - this.start;
}
get isEmpty() {
return this.length === 0;
}
getByte() {
if (this.pos >= this.end) {
return -1;
}
return this.bytes[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;
}
// Returns subarray of original buffer, should only be read.
getBytes(length: number, forceClamped = false) {
const bytes = this.bytes;
const pos = this.pos;
const strEnd = this.end;
if (!length) {
const subarray = bytes.subarray(pos, strEnd);
// `this.bytes` is always a `Uint8Array` here.
return forceClamped ? new Uint8ClampedArray(subarray) : subarray;
} else {
let end = pos + length;
if (end > strEnd) {
end = strEnd;
}
this.pos = end;
const subarray = bytes.subarray(pos, end);
// `this.bytes` is always a `Uint8Array` here.
return forceClamped ? 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 = this.start;
}
moveStart() {
this.start = this.pos;
}
makeSubStream(start: number, length: number) {
return new Stream(this.bytes, start, length);
}
decode(): Uint8Array {
return this.bytes;
}
}
export default Stream;