@nuintun/buffer 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/Binary.cjs +1 -1
- package/cjs/Encoding/UTF8.cjs +1 -1
- package/cjs/Encoding/Unicode.cjs +1 -1
- package/cjs/Encoding/index.cjs +1 -1
- package/cjs/errors.cjs +1 -1
- package/cjs/index.cjs +157 -202
- package/cjs/utils.cjs +1 -1
- package/esm/Binary.js +1 -1
- package/esm/Encoding/UTF8.js +1 -1
- package/esm/Encoding/Unicode.js +1 -1
- package/esm/Encoding/index.js +1 -1
- package/esm/errors.js +1 -1
- package/esm/index.js +158 -215
- package/esm/utils.js +1 -1
- package/package.json +1 -1
package/esm/Binary.js
CHANGED
package/esm/Encoding/UTF8.js
CHANGED
package/esm/Encoding/Unicode.js
CHANGED
package/esm/Encoding/index.js
CHANGED
package/esm/errors.js
CHANGED
package/esm/index.js
CHANGED
|
@@ -1,31 +1,20 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @package @nuintun/buffer
|
|
3
3
|
* @license MIT
|
|
4
|
-
* @version 0.
|
|
4
|
+
* @version 0.2.0
|
|
5
5
|
* @author nuintun <nuintun@qq.com>
|
|
6
6
|
* @description A buffer tool for javascript.
|
|
7
7
|
* @see https://github.com/nuintun/Buffer#readme
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
-
import { __classPrivateFieldSet, __classPrivateFieldGet } from 'tslib';
|
|
11
10
|
import { calcBufferLength } from './utils.js';
|
|
12
11
|
import { mapping } from './Binary.js';
|
|
13
|
-
import { unknownEndianness, offsetInvalid, offsetOverflow, lengthInvalid
|
|
12
|
+
import { unknownEndianness, readOverflow, offsetInvalid, offsetOverflow, lengthInvalid } from './errors.js';
|
|
14
13
|
import { encode, decode } from './Encoding/index.js';
|
|
15
14
|
|
|
16
15
|
/**
|
|
17
16
|
* @module Buffer
|
|
18
17
|
*/
|
|
19
|
-
var _Buffer_instances,
|
|
20
|
-
_Buffer_pageSize,
|
|
21
|
-
_Buffer_bytes,
|
|
22
|
-
_Buffer_dataView,
|
|
23
|
-
_Buffer_offset,
|
|
24
|
-
_Buffer_length,
|
|
25
|
-
_Buffer_grow,
|
|
26
|
-
_Buffer_seek,
|
|
27
|
-
_Buffer_assertRead,
|
|
28
|
-
_Buffer_alloc;
|
|
29
18
|
// 字节序类型
|
|
30
19
|
var Endian;
|
|
31
20
|
(function (Endian) {
|
|
@@ -52,29 +41,73 @@ function endianness() {
|
|
|
52
41
|
* @classdesc Buffer 类提供用于优化读取,写入以及处理二进制数据的方法和属性
|
|
53
42
|
*/
|
|
54
43
|
class Buffer {
|
|
44
|
+
// 缓冲区页大小
|
|
45
|
+
// 容量不足时按页大小增长
|
|
46
|
+
#pageSize;
|
|
47
|
+
// 缓冲区数据
|
|
48
|
+
#bytes;
|
|
49
|
+
// 缓冲区视图
|
|
50
|
+
#dataView;
|
|
51
|
+
// 读写指针位置
|
|
52
|
+
#offset = 0;
|
|
53
|
+
// 已使用字节长度
|
|
54
|
+
#length = 0;
|
|
55
55
|
constructor(input = 0, pageSize = 4096) {
|
|
56
|
-
|
|
57
|
-
// 缓冲区页大小
|
|
58
|
-
// 容量不足时按页大小增长
|
|
59
|
-
_Buffer_pageSize.set(this, void 0);
|
|
60
|
-
// 缓冲区数据
|
|
61
|
-
_Buffer_bytes.set(this, void 0);
|
|
62
|
-
// 缓冲区视图
|
|
63
|
-
_Buffer_dataView.set(this, void 0);
|
|
64
|
-
// 读写指针位置
|
|
65
|
-
_Buffer_offset.set(this, 0);
|
|
66
|
-
// 已使用字节长度
|
|
67
|
-
_Buffer_length.set(this, 0);
|
|
68
|
-
__classPrivateFieldSet(this, _Buffer_pageSize, pageSize, 'f');
|
|
56
|
+
this.#pageSize = pageSize;
|
|
69
57
|
if (input instanceof Uint8Array) {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
58
|
+
this.#bytes = input;
|
|
59
|
+
this.#length = input.length;
|
|
60
|
+
this.#dataView = new DataView(input.buffer);
|
|
73
61
|
} else {
|
|
74
62
|
const bytes = new Uint8Array(calcBufferLength(input, pageSize));
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
63
|
+
this.#bytes = bytes;
|
|
64
|
+
this.#length = input;
|
|
65
|
+
this.#dataView = new DataView(bytes.buffer);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* @private
|
|
70
|
+
* @method grow
|
|
71
|
+
* @description 增加长度
|
|
72
|
+
* @param {number} length 长度增加量
|
|
73
|
+
*/
|
|
74
|
+
#grow(length) {
|
|
75
|
+
this.#length += length;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* @private
|
|
79
|
+
* @method seek
|
|
80
|
+
* @description 移动读写指针
|
|
81
|
+
* @param {number} offset 指针偏移量
|
|
82
|
+
*/
|
|
83
|
+
#seek(offset) {
|
|
84
|
+
this.#offset += offset;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* @private
|
|
88
|
+
* @method assertRead
|
|
89
|
+
* @description 读取断言,防止越界读取
|
|
90
|
+
* @param {number} length 断言字节长度
|
|
91
|
+
*/
|
|
92
|
+
#assertRead(length) {
|
|
93
|
+
if (length < 0 || this.#offset + length > this.#length) {
|
|
94
|
+
throw new RangeError(readOverflow);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* @private
|
|
99
|
+
* @method alloc
|
|
100
|
+
* @description 分配指定长度的缓冲区大小,如果缓冲区溢出则刷新缓冲区
|
|
101
|
+
* @param {number} length 分配字节长度
|
|
102
|
+
*/
|
|
103
|
+
#alloc(length) {
|
|
104
|
+
length += this.#offset;
|
|
105
|
+
const bytes = this.#bytes;
|
|
106
|
+
if (length > bytes.length) {
|
|
107
|
+
const newBytes = new Uint8Array(calcBufferLength(length, this.#pageSize));
|
|
108
|
+
newBytes.set(bytes);
|
|
109
|
+
this.#bytes = newBytes;
|
|
110
|
+
this.#dataView = new DataView(newBytes.buffer);
|
|
78
111
|
}
|
|
79
112
|
}
|
|
80
113
|
/**
|
|
@@ -87,10 +120,10 @@ class Buffer {
|
|
|
87
120
|
if (offset < 0) {
|
|
88
121
|
throw new RangeError(offsetInvalid);
|
|
89
122
|
}
|
|
90
|
-
if (offset >
|
|
123
|
+
if (offset > this.#length) {
|
|
91
124
|
throw new RangeError(offsetOverflow);
|
|
92
125
|
}
|
|
93
|
-
|
|
126
|
+
this.#offset = offset;
|
|
94
127
|
}
|
|
95
128
|
/**
|
|
96
129
|
* @public
|
|
@@ -99,7 +132,7 @@ class Buffer {
|
|
|
99
132
|
* @returns {number}
|
|
100
133
|
*/
|
|
101
134
|
get offset() {
|
|
102
|
-
return
|
|
135
|
+
return this.#offset;
|
|
103
136
|
}
|
|
104
137
|
/**
|
|
105
138
|
* @public
|
|
@@ -112,16 +145,16 @@ class Buffer {
|
|
|
112
145
|
if (length < 0) {
|
|
113
146
|
throw new RangeError(lengthInvalid);
|
|
114
147
|
}
|
|
115
|
-
const currentLength =
|
|
148
|
+
const currentLength = this.#length;
|
|
116
149
|
if (length > currentLength) {
|
|
117
|
-
|
|
150
|
+
this.#alloc(length - currentLength);
|
|
118
151
|
} else {
|
|
119
|
-
|
|
152
|
+
this.#length = length;
|
|
120
153
|
// 重置多余字节
|
|
121
|
-
|
|
154
|
+
this.#bytes.fill(0, length);
|
|
122
155
|
}
|
|
123
|
-
if (
|
|
124
|
-
|
|
156
|
+
if (this.#offset > length) {
|
|
157
|
+
this.#offset = length;
|
|
125
158
|
}
|
|
126
159
|
}
|
|
127
160
|
/**
|
|
@@ -131,7 +164,7 @@ class Buffer {
|
|
|
131
164
|
* @returns {number}
|
|
132
165
|
*/
|
|
133
166
|
get length() {
|
|
134
|
-
return
|
|
167
|
+
return this.#length;
|
|
135
168
|
}
|
|
136
169
|
/**
|
|
137
170
|
* @public
|
|
@@ -140,7 +173,7 @@ class Buffer {
|
|
|
140
173
|
* @returns {ArrayBuffer}
|
|
141
174
|
*/
|
|
142
175
|
get buffer() {
|
|
143
|
-
return
|
|
176
|
+
return this.#bytes.buffer.slice(0, this.#length);
|
|
144
177
|
}
|
|
145
178
|
/**
|
|
146
179
|
* @public
|
|
@@ -149,7 +182,7 @@ class Buffer {
|
|
|
149
182
|
* @returns {Uint8Array}
|
|
150
183
|
*/
|
|
151
184
|
get bytes() {
|
|
152
|
-
return
|
|
185
|
+
return this.#bytes.slice(0, this.#length);
|
|
153
186
|
}
|
|
154
187
|
/**
|
|
155
188
|
* @public
|
|
@@ -158,10 +191,10 @@ class Buffer {
|
|
|
158
191
|
* @param {number} value 介于 -128 和 127 之间的整数
|
|
159
192
|
*/
|
|
160
193
|
writeInt8(value) {
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
194
|
+
this.#alloc(1 /* SizeOf.INT8 */);
|
|
195
|
+
this.#dataView.setInt8(this.#offset, value);
|
|
196
|
+
this.#grow(1 /* SizeOf.INT8 */);
|
|
197
|
+
this.#seek(1 /* SizeOf.INT8 */);
|
|
165
198
|
}
|
|
166
199
|
/**
|
|
167
200
|
* @public
|
|
@@ -170,10 +203,10 @@ class Buffer {
|
|
|
170
203
|
* @param {number} value 介于 0 和 255 之间的整数
|
|
171
204
|
*/
|
|
172
205
|
writeUint8(value) {
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
206
|
+
this.#alloc(1 /* SizeOf.UINT8 */);
|
|
207
|
+
this.#dataView.setUint8(this.#offset, value);
|
|
208
|
+
this.#grow(1 /* SizeOf.UINT8 */);
|
|
209
|
+
this.#seek(1 /* SizeOf.UINT8 */);
|
|
177
210
|
}
|
|
178
211
|
/**
|
|
179
212
|
* @method writeBoolean
|
|
@@ -190,14 +223,10 @@ class Buffer {
|
|
|
190
223
|
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
191
224
|
*/
|
|
192
225
|
writeInt16(value, littleEndian) {
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
littleEndian
|
|
198
|
-
);
|
|
199
|
-
__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_grow).call(this, 2 /* SizeOf.INT16 */);
|
|
200
|
-
__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, 2 /* SizeOf.INT16 */);
|
|
226
|
+
this.#alloc(2 /* SizeOf.INT16 */);
|
|
227
|
+
this.#dataView.setInt16(this.#offset, value, littleEndian);
|
|
228
|
+
this.#grow(2 /* SizeOf.INT16 */);
|
|
229
|
+
this.#seek(2 /* SizeOf.INT16 */);
|
|
201
230
|
}
|
|
202
231
|
/**
|
|
203
232
|
* @method writeUint16
|
|
@@ -206,14 +235,10 @@ class Buffer {
|
|
|
206
235
|
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
207
236
|
*/
|
|
208
237
|
writeUint16(value, littleEndian) {
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
littleEndian
|
|
214
|
-
);
|
|
215
|
-
__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_grow).call(this, 2 /* SizeOf.UINT16 */);
|
|
216
|
-
__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, 2 /* SizeOf.UINT16 */);
|
|
238
|
+
this.#alloc(2 /* SizeOf.UINT16 */);
|
|
239
|
+
this.#dataView.setUint16(this.#offset, value, littleEndian);
|
|
240
|
+
this.#grow(2 /* SizeOf.UINT16 */);
|
|
241
|
+
this.#seek(2 /* SizeOf.UINT16 */);
|
|
217
242
|
}
|
|
218
243
|
/**
|
|
219
244
|
* @method writeInt32
|
|
@@ -222,14 +247,10 @@ class Buffer {
|
|
|
222
247
|
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
223
248
|
*/
|
|
224
249
|
writeInt32(value, littleEndian) {
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
littleEndian
|
|
230
|
-
);
|
|
231
|
-
__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_grow).call(this, 4 /* SizeOf.INT32 */);
|
|
232
|
-
__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, 4 /* SizeOf.INT32 */);
|
|
250
|
+
this.#alloc(4 /* SizeOf.INT32 */);
|
|
251
|
+
this.#dataView.setInt32(this.#offset, value, littleEndian);
|
|
252
|
+
this.#grow(4 /* SizeOf.INT32 */);
|
|
253
|
+
this.#seek(4 /* SizeOf.INT32 */);
|
|
233
254
|
}
|
|
234
255
|
/**
|
|
235
256
|
* @method writeUint32
|
|
@@ -238,14 +259,10 @@ class Buffer {
|
|
|
238
259
|
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
239
260
|
*/
|
|
240
261
|
writeUint32(value, littleEndian) {
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
littleEndian
|
|
246
|
-
);
|
|
247
|
-
__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_grow).call(this, 4 /* SizeOf.UINT32 */);
|
|
248
|
-
__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, 4 /* SizeOf.UINT32 */);
|
|
262
|
+
this.#alloc(4 /* SizeOf.UINT32 */);
|
|
263
|
+
this.#dataView.setUint32(this.#offset, value, littleEndian);
|
|
264
|
+
this.#grow(4 /* SizeOf.UINT32 */);
|
|
265
|
+
this.#seek(4 /* SizeOf.UINT32 */);
|
|
249
266
|
}
|
|
250
267
|
/**
|
|
251
268
|
* @method writeInt64
|
|
@@ -254,14 +271,10 @@ class Buffer {
|
|
|
254
271
|
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
255
272
|
*/
|
|
256
273
|
writeInt64(value, littleEndian) {
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
littleEndian
|
|
262
|
-
);
|
|
263
|
-
__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_grow).call(this, 8 /* SizeOf.INI64 */);
|
|
264
|
-
__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, 8 /* SizeOf.INI64 */);
|
|
274
|
+
this.#alloc(8 /* SizeOf.INI64 */);
|
|
275
|
+
this.#dataView.setBigInt64(this.#offset, value, littleEndian);
|
|
276
|
+
this.#grow(8 /* SizeOf.INI64 */);
|
|
277
|
+
this.#seek(8 /* SizeOf.INI64 */);
|
|
265
278
|
}
|
|
266
279
|
/**
|
|
267
280
|
* @method writeUint64
|
|
@@ -270,14 +283,10 @@ class Buffer {
|
|
|
270
283
|
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
271
284
|
*/
|
|
272
285
|
writeUint64(value, littleEndian) {
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
littleEndian
|
|
278
|
-
);
|
|
279
|
-
__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_grow).call(this, 8 /* SizeOf.UINT64 */);
|
|
280
|
-
__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, 8 /* SizeOf.UINT64 */);
|
|
286
|
+
this.#alloc(8 /* SizeOf.UINT64 */);
|
|
287
|
+
this.#dataView.setBigUint64(this.#offset, value, littleEndian);
|
|
288
|
+
this.#grow(8 /* SizeOf.UINT64 */);
|
|
289
|
+
this.#seek(8 /* SizeOf.UINT64 */);
|
|
281
290
|
}
|
|
282
291
|
/**
|
|
283
292
|
* @method writeFloat32
|
|
@@ -286,14 +295,10 @@ class Buffer {
|
|
|
286
295
|
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
287
296
|
*/
|
|
288
297
|
writeFloat32(value, littleEndian) {
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
littleEndian
|
|
294
|
-
);
|
|
295
|
-
__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_grow).call(this, 4 /* SizeOf.FLOAT32 */);
|
|
296
|
-
__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, 4 /* SizeOf.FLOAT32 */);
|
|
298
|
+
this.#alloc(4 /* SizeOf.FLOAT32 */);
|
|
299
|
+
this.#dataView.setFloat32(this.#offset, value, littleEndian);
|
|
300
|
+
this.#grow(4 /* SizeOf.FLOAT32 */);
|
|
301
|
+
this.#seek(4 /* SizeOf.FLOAT32 */);
|
|
297
302
|
}
|
|
298
303
|
/**
|
|
299
304
|
* @method writeFloat64
|
|
@@ -302,14 +307,10 @@ class Buffer {
|
|
|
302
307
|
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
303
308
|
*/
|
|
304
309
|
writeFloat64(value, littleEndian) {
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
littleEndian
|
|
310
|
-
);
|
|
311
|
-
__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_grow).call(this, 8 /* SizeOf.FLOAT64 */);
|
|
312
|
-
__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, 8 /* SizeOf.FLOAT64 */);
|
|
310
|
+
this.#alloc(8 /* SizeOf.FLOAT64 */);
|
|
311
|
+
this.#dataView.setFloat64(this.#offset, value, littleEndian);
|
|
312
|
+
this.#grow(8 /* SizeOf.FLOAT64 */);
|
|
313
|
+
this.#seek(8 /* SizeOf.FLOAT64 */);
|
|
313
314
|
}
|
|
314
315
|
write(input, start, end) {
|
|
315
316
|
let bytes;
|
|
@@ -320,10 +321,10 @@ class Buffer {
|
|
|
320
321
|
}
|
|
321
322
|
const { length } = bytes;
|
|
322
323
|
if (length > 0) {
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
324
|
+
this.#alloc(length);
|
|
325
|
+
this.#bytes.set(bytes, this.#offset);
|
|
326
|
+
this.#grow(length);
|
|
327
|
+
this.#seek(length);
|
|
327
328
|
}
|
|
328
329
|
}
|
|
329
330
|
/**
|
|
@@ -332,11 +333,9 @@ class Buffer {
|
|
|
332
333
|
* @returns {number} 介于 -128 和 127 之间的整数
|
|
333
334
|
*/
|
|
334
335
|
readInt8() {
|
|
335
|
-
|
|
336
|
-
const value =
|
|
337
|
-
|
|
338
|
-
);
|
|
339
|
-
__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, 1 /* SizeOf.INT8 */);
|
|
336
|
+
this.#assertRead(1 /* SizeOf.INT8 */);
|
|
337
|
+
const value = this.#dataView.getInt8(this.#offset);
|
|
338
|
+
this.#seek(1 /* SizeOf.INT8 */);
|
|
340
339
|
return value;
|
|
341
340
|
}
|
|
342
341
|
/**
|
|
@@ -345,11 +344,9 @@ class Buffer {
|
|
|
345
344
|
* @returns {number} 介于 0 和 255 之间的无符号整数
|
|
346
345
|
*/
|
|
347
346
|
readUint8() {
|
|
348
|
-
|
|
349
|
-
const value =
|
|
350
|
-
|
|
351
|
-
);
|
|
352
|
-
__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, 1 /* SizeOf.UINT8 */);
|
|
347
|
+
this.#assertRead(1 /* SizeOf.UINT8 */);
|
|
348
|
+
const value = this.#dataView.getUint8(this.#offset);
|
|
349
|
+
this.#seek(1 /* SizeOf.UINT8 */);
|
|
353
350
|
return value;
|
|
354
351
|
}
|
|
355
352
|
/**
|
|
@@ -367,12 +364,9 @@ class Buffer {
|
|
|
367
364
|
* @returns {number} 介于 -32768 和 32767 之间的 16 位有符号整数
|
|
368
365
|
*/
|
|
369
366
|
readInt16(littleEndian) {
|
|
370
|
-
|
|
371
|
-
const value =
|
|
372
|
-
|
|
373
|
-
littleEndian
|
|
374
|
-
);
|
|
375
|
-
__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, 2 /* SizeOf.INT16 */);
|
|
367
|
+
this.#assertRead(2 /* SizeOf.INT16 */);
|
|
368
|
+
const value = this.#dataView.getInt16(this.#offset, littleEndian);
|
|
369
|
+
this.#seek(2 /* SizeOf.INT16 */);
|
|
376
370
|
return value;
|
|
377
371
|
}
|
|
378
372
|
/**
|
|
@@ -382,12 +376,9 @@ class Buffer {
|
|
|
382
376
|
* @returns {number} 介于 0 和 65535 之间的 16 位无符号整数
|
|
383
377
|
*/
|
|
384
378
|
readUint16(littleEndian) {
|
|
385
|
-
|
|
386
|
-
const value =
|
|
387
|
-
|
|
388
|
-
littleEndian
|
|
389
|
-
);
|
|
390
|
-
__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, 2 /* SizeOf.UINT16 */);
|
|
379
|
+
this.#assertRead(2 /* SizeOf.UINT16 */);
|
|
380
|
+
const value = this.#dataView.getUint16(this.#offset, littleEndian);
|
|
381
|
+
this.#seek(2 /* SizeOf.UINT16 */);
|
|
391
382
|
return value;
|
|
392
383
|
}
|
|
393
384
|
/**
|
|
@@ -397,12 +388,9 @@ class Buffer {
|
|
|
397
388
|
* @returns {number} 介于 -2147483648 和 2147483647 之间的 32 位有符号整数
|
|
398
389
|
*/
|
|
399
390
|
readInt32(littleEndian) {
|
|
400
|
-
|
|
401
|
-
const value =
|
|
402
|
-
|
|
403
|
-
littleEndian
|
|
404
|
-
);
|
|
405
|
-
__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, 4 /* SizeOf.INT32 */);
|
|
391
|
+
this.#assertRead(4 /* SizeOf.INT32 */);
|
|
392
|
+
const value = this.#dataView.getInt32(this.#offset, littleEndian);
|
|
393
|
+
this.#seek(4 /* SizeOf.INT32 */);
|
|
406
394
|
return value;
|
|
407
395
|
}
|
|
408
396
|
/**
|
|
@@ -412,12 +400,9 @@ class Buffer {
|
|
|
412
400
|
* @returns {number} 介于 0 和 4294967295 之间的 32 位无符号整数
|
|
413
401
|
*/
|
|
414
402
|
readUint32(littleEndian) {
|
|
415
|
-
|
|
416
|
-
const value =
|
|
417
|
-
|
|
418
|
-
littleEndian
|
|
419
|
-
);
|
|
420
|
-
__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, 4 /* SizeOf.UINT32 */);
|
|
403
|
+
this.#assertRead(4 /* SizeOf.UINT32 */);
|
|
404
|
+
const value = this.#dataView.getUint32(this.#offset, littleEndian);
|
|
405
|
+
this.#seek(4 /* SizeOf.UINT32 */);
|
|
421
406
|
return value;
|
|
422
407
|
}
|
|
423
408
|
/**
|
|
@@ -427,12 +412,9 @@ class Buffer {
|
|
|
427
412
|
* @returns {bigint} 介于 -9223372036854775808 和 9223372036854775807 之间的 64 位有符号整数
|
|
428
413
|
*/
|
|
429
414
|
readInt64(littleEndian) {
|
|
430
|
-
|
|
431
|
-
const value =
|
|
432
|
-
|
|
433
|
-
littleEndian
|
|
434
|
-
);
|
|
435
|
-
__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, 8 /* SizeOf.INI64 */);
|
|
415
|
+
this.#assertRead(8 /* SizeOf.INI64 */);
|
|
416
|
+
const value = this.#dataView.getBigInt64(this.#offset, littleEndian);
|
|
417
|
+
this.#seek(8 /* SizeOf.INI64 */);
|
|
436
418
|
return value;
|
|
437
419
|
}
|
|
438
420
|
/**
|
|
@@ -442,12 +424,9 @@ class Buffer {
|
|
|
442
424
|
* @returns {bigint} 介于 0 和 18446744073709551615 之间的 64 位无符号整数
|
|
443
425
|
*/
|
|
444
426
|
readUint64(littleEndian) {
|
|
445
|
-
|
|
446
|
-
const value =
|
|
447
|
-
|
|
448
|
-
littleEndian
|
|
449
|
-
);
|
|
450
|
-
__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, 8 /* SizeOf.UINT64 */);
|
|
427
|
+
this.#assertRead(8 /* SizeOf.UINT64 */);
|
|
428
|
+
const value = this.#dataView.getBigUint64(this.#offset, littleEndian);
|
|
429
|
+
this.#seek(8 /* SizeOf.UINT64 */);
|
|
451
430
|
return value;
|
|
452
431
|
}
|
|
453
432
|
/**
|
|
@@ -457,12 +436,9 @@ class Buffer {
|
|
|
457
436
|
* @returns {number} 单精度 32 位浮点数
|
|
458
437
|
*/
|
|
459
438
|
readFloat32(littleEndian) {
|
|
460
|
-
|
|
461
|
-
const value =
|
|
462
|
-
|
|
463
|
-
littleEndian
|
|
464
|
-
);
|
|
465
|
-
__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, 4 /* SizeOf.FLOAT32 */);
|
|
439
|
+
this.#assertRead(4 /* SizeOf.FLOAT32 */);
|
|
440
|
+
const value = this.#dataView.getFloat32(this.#offset, littleEndian);
|
|
441
|
+
this.#seek(4 /* SizeOf.FLOAT32 */);
|
|
466
442
|
return value;
|
|
467
443
|
}
|
|
468
444
|
/**
|
|
@@ -472,19 +448,16 @@ class Buffer {
|
|
|
472
448
|
* @returns {number} 双精度 64 位浮点数
|
|
473
449
|
*/
|
|
474
450
|
readFloat64(littleEndian) {
|
|
475
|
-
|
|
476
|
-
const value =
|
|
477
|
-
|
|
478
|
-
littleEndian
|
|
479
|
-
);
|
|
480
|
-
__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, 8 /* SizeOf.FLOAT64 */);
|
|
451
|
+
this.#assertRead(8 /* SizeOf.FLOAT64 */);
|
|
452
|
+
const value = this.#dataView.getFloat64(this.#offset, littleEndian);
|
|
453
|
+
this.#seek(8 /* SizeOf.FLOAT64 */);
|
|
481
454
|
return value;
|
|
482
455
|
}
|
|
483
456
|
read(length, encoding) {
|
|
484
|
-
|
|
485
|
-
const offset =
|
|
486
|
-
const bytes =
|
|
487
|
-
|
|
457
|
+
this.#assertRead(length);
|
|
458
|
+
const offset = this.#offset;
|
|
459
|
+
const bytes = this.#bytes.slice(offset, offset + length);
|
|
460
|
+
this.#seek(length);
|
|
488
461
|
if (arguments.length >= 2) {
|
|
489
462
|
return decode(bytes, encoding);
|
|
490
463
|
}
|
|
@@ -499,8 +472,8 @@ class Buffer {
|
|
|
499
472
|
* @returns {Buffer}
|
|
500
473
|
*/
|
|
501
474
|
slice(start, end) {
|
|
502
|
-
const bytes =
|
|
503
|
-
return new Buffer(bytes,
|
|
475
|
+
const bytes = this.#bytes.slice(start, end);
|
|
476
|
+
return new Buffer(bytes, this.#pageSize);
|
|
504
477
|
}
|
|
505
478
|
/**
|
|
506
479
|
* @public
|
|
@@ -512,7 +485,7 @@ class Buffer {
|
|
|
512
485
|
* @returns {this}
|
|
513
486
|
*/
|
|
514
487
|
copyWithin(target, start, end) {
|
|
515
|
-
|
|
488
|
+
this.#bytes.copyWithin(target, start, end);
|
|
516
489
|
return this;
|
|
517
490
|
}
|
|
518
491
|
/**
|
|
@@ -534,35 +507,5 @@ class Buffer {
|
|
|
534
507
|
return binary;
|
|
535
508
|
}
|
|
536
509
|
}
|
|
537
|
-
(_Buffer_pageSize = new WeakMap()),
|
|
538
|
-
(_Buffer_bytes = new WeakMap()),
|
|
539
|
-
(_Buffer_dataView = new WeakMap()),
|
|
540
|
-
(_Buffer_offset = new WeakMap()),
|
|
541
|
-
(_Buffer_length = new WeakMap()),
|
|
542
|
-
(_Buffer_instances = new WeakSet()),
|
|
543
|
-
(_Buffer_grow = function _Buffer_grow(length) {
|
|
544
|
-
__classPrivateFieldSet(this, _Buffer_length, __classPrivateFieldGet(this, _Buffer_length, 'f') + length, 'f');
|
|
545
|
-
}),
|
|
546
|
-
(_Buffer_seek = function _Buffer_seek(offset) {
|
|
547
|
-
__classPrivateFieldSet(this, _Buffer_offset, __classPrivateFieldGet(this, _Buffer_offset, 'f') + offset, 'f');
|
|
548
|
-
}),
|
|
549
|
-
(_Buffer_assertRead = function _Buffer_assertRead(length) {
|
|
550
|
-
if (
|
|
551
|
-
length < 0 ||
|
|
552
|
-
__classPrivateFieldGet(this, _Buffer_offset, 'f') + length > __classPrivateFieldGet(this, _Buffer_length, 'f')
|
|
553
|
-
) {
|
|
554
|
-
throw new RangeError(readOverflow);
|
|
555
|
-
}
|
|
556
|
-
}),
|
|
557
|
-
(_Buffer_alloc = function _Buffer_alloc(length) {
|
|
558
|
-
length += __classPrivateFieldGet(this, _Buffer_offset, 'f');
|
|
559
|
-
const bytes = __classPrivateFieldGet(this, _Buffer_bytes, 'f');
|
|
560
|
-
if (length > bytes.length) {
|
|
561
|
-
const newBytes = new Uint8Array(calcBufferLength(length, __classPrivateFieldGet(this, _Buffer_pageSize, 'f')));
|
|
562
|
-
newBytes.set(bytes);
|
|
563
|
-
__classPrivateFieldSet(this, _Buffer_bytes, newBytes, 'f');
|
|
564
|
-
__classPrivateFieldSet(this, _Buffer_dataView, new DataView(newBytes.buffer), 'f');
|
|
565
|
-
}
|
|
566
|
-
});
|
|
567
510
|
|
|
568
511
|
export { Buffer, Endian, endianness };
|