@nuintun/buffer 0.1.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/index.cjs ADDED
@@ -0,0 +1,559 @@
1
+ /**
2
+ * @package @nuintun/buffer
3
+ * @license MIT
4
+ * @version 0.1.0
5
+ * @author nuintun <nuintun@qq.com>
6
+ * @description A buffer tool for javascript.
7
+ * @see https://github.com/nuintun/Buffer#readme
8
+ */
9
+
10
+ 'use strict';
11
+
12
+ const tslib = require('tslib');
13
+ const utils = require('./utils.cjs');
14
+ const Binary = require('./Binary.cjs');
15
+ const errors = require('./errors.cjs');
16
+ const index = require('./Encoding/index.cjs');
17
+
18
+ /**
19
+ * @module Buffer
20
+ */
21
+ var _Buffer_instances,
22
+ _Buffer_pageSize,
23
+ _Buffer_bytes,
24
+ _Buffer_dataView,
25
+ _Buffer_offset,
26
+ _Buffer_length,
27
+ _Buffer_grow,
28
+ _Buffer_seek,
29
+ _Buffer_assertRead,
30
+ _Buffer_alloc;
31
+ // 字节序类型
32
+ exports.Endian = void 0;
33
+ (function (Endian) {
34
+ Endian[(Endian['Big'] = 0)] = 'Big';
35
+ Endian[(Endian['Little'] = 1)] = 'Little';
36
+ })(exports.Endian || (exports.Endian = {}));
37
+ /**
38
+ * @function endianness
39
+ * @description 获取系统默认字节序
40
+ * @returns {Endian}
41
+ */
42
+ function endianness() {
43
+ switch (new Uint8Array(new Uint32Array([0x12345678]))[0]) {
44
+ case 0x12:
45
+ return exports.Endian.Big;
46
+ case 0x78:
47
+ return exports.Endian.Little;
48
+ default:
49
+ throw new TypeError(errors.unknownEndianness);
50
+ }
51
+ }
52
+ /**
53
+ * @class Buffer
54
+ * @classdesc Buffer 类提供用于优化读取,写入以及处理二进制数据的方法和属性
55
+ */
56
+ class Buffer {
57
+ constructor(input = 0, pageSize = 4096) {
58
+ _Buffer_instances.add(this);
59
+ // 缓冲区页大小
60
+ // 容量不足时按页大小增长
61
+ _Buffer_pageSize.set(this, void 0);
62
+ // 缓冲区数据
63
+ _Buffer_bytes.set(this, void 0);
64
+ // 缓冲区视图
65
+ _Buffer_dataView.set(this, void 0);
66
+ // 读写指针位置
67
+ _Buffer_offset.set(this, 0);
68
+ // 已使用字节长度
69
+ _Buffer_length.set(this, 0);
70
+ tslib.__classPrivateFieldSet(this, _Buffer_pageSize, pageSize, 'f');
71
+ if (input instanceof Uint8Array) {
72
+ tslib.__classPrivateFieldSet(this, _Buffer_bytes, input, 'f');
73
+ tslib.__classPrivateFieldSet(this, _Buffer_length, input.length, 'f');
74
+ tslib.__classPrivateFieldSet(this, _Buffer_dataView, new DataView(input.buffer), 'f');
75
+ } else {
76
+ const bytes = new Uint8Array(utils.calcBufferLength(input, pageSize));
77
+ tslib.__classPrivateFieldSet(this, _Buffer_bytes, bytes, 'f');
78
+ tslib.__classPrivateFieldSet(this, _Buffer_length, input, 'f');
79
+ tslib.__classPrivateFieldSet(this, _Buffer_dataView, new DataView(bytes.buffer), 'f');
80
+ }
81
+ }
82
+ /**
83
+ * @public
84
+ * @property {number} offset
85
+ * @description 设置读写指针位置,以字节为单位
86
+ * @description 下一次调用读写方法时将在此位置开始读写
87
+ */
88
+ set offset(offset) {
89
+ if (offset < 0) {
90
+ throw new RangeError(errors.offsetInvalid);
91
+ }
92
+ if (offset > tslib.__classPrivateFieldGet(this, _Buffer_length, 'f')) {
93
+ throw new RangeError(errors.offsetOverflow);
94
+ }
95
+ tslib.__classPrivateFieldSet(this, _Buffer_offset, offset, 'f');
96
+ }
97
+ /**
98
+ * @public
99
+ * @property {number} offset
100
+ * @description 获取读写指针的位置
101
+ * @returns {number}
102
+ */
103
+ get offset() {
104
+ return tslib.__classPrivateFieldGet(this, _Buffer_offset, 'f');
105
+ }
106
+ /**
107
+ * @public
108
+ * @property {number} length
109
+ * @description 设置 Buffer 长度
110
+ * @description 如果将长度设置为小于当前长度的值,将会截断该字节数组
111
+ * @description 如果将长度设置为大于当前长度的值,则用零填充字节数组的右侧
112
+ */
113
+ set length(length) {
114
+ if (length < 0) {
115
+ throw new RangeError(errors.lengthInvalid);
116
+ }
117
+ const currentLength = tslib.__classPrivateFieldGet(this, _Buffer_length, 'f');
118
+ if (length > currentLength) {
119
+ tslib.__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_alloc).call(this, length - currentLength);
120
+ } else {
121
+ tslib.__classPrivateFieldSet(this, _Buffer_length, length, 'f');
122
+ // 重置多余字节
123
+ tslib.__classPrivateFieldGet(this, _Buffer_bytes, 'f').fill(0, length);
124
+ }
125
+ if (tslib.__classPrivateFieldGet(this, _Buffer_offset, 'f') > length) {
126
+ tslib.__classPrivateFieldSet(this, _Buffer_offset, length, 'f');
127
+ }
128
+ }
129
+ /**
130
+ * @public
131
+ * @property {number} length
132
+ * @description 获取 Buffer 长度
133
+ * @returns {number}
134
+ */
135
+ get length() {
136
+ return tslib.__classPrivateFieldGet(this, _Buffer_length, 'f');
137
+ }
138
+ /**
139
+ * @public
140
+ * @property {ArrayBuffer} buffer
141
+ * @description 获取 ArrayBuffer 缓冲区
142
+ * @returns {ArrayBuffer}
143
+ */
144
+ get buffer() {
145
+ return tslib
146
+ .__classPrivateFieldGet(this, _Buffer_bytes, 'f')
147
+ .buffer.slice(0, tslib.__classPrivateFieldGet(this, _Buffer_length, 'f'));
148
+ }
149
+ /**
150
+ * @public
151
+ * @property {Uint8Array} bytes
152
+ * @description 获取 Uint8Array 缓冲区
153
+ * @returns {Uint8Array}
154
+ */
155
+ get bytes() {
156
+ return tslib
157
+ .__classPrivateFieldGet(this, _Buffer_bytes, 'f')
158
+ .slice(0, tslib.__classPrivateFieldGet(this, _Buffer_length, 'f'));
159
+ }
160
+ /**
161
+ * @public
162
+ * @method writeInt8
163
+ * @description 在缓冲区中写入一个有符号整数
164
+ * @param {number} value 介于 -128 和 127 之间的整数
165
+ */
166
+ writeInt8(value) {
167
+ tslib.__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_alloc).call(this, 1 /* SizeOf.INT8 */);
168
+ tslib
169
+ .__classPrivateFieldGet(this, _Buffer_dataView, 'f')
170
+ .setInt8(tslib.__classPrivateFieldGet(this, _Buffer_offset, 'f'), value);
171
+ tslib.__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_grow).call(this, 1 /* SizeOf.INT8 */);
172
+ tslib.__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, 1 /* SizeOf.INT8 */);
173
+ }
174
+ /**
175
+ * @public
176
+ * @method writeUint8
177
+ * @description 在缓冲区中写入一个无符号整数
178
+ * @param {number} value 介于 0 和 255 之间的整数
179
+ */
180
+ writeUint8(value) {
181
+ tslib.__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_alloc).call(this, 1 /* SizeOf.UINT8 */);
182
+ tslib
183
+ .__classPrivateFieldGet(this, _Buffer_dataView, 'f')
184
+ .setUint8(tslib.__classPrivateFieldGet(this, _Buffer_offset, 'f'), value);
185
+ tslib.__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_grow).call(this, 1 /* SizeOf.UINT8 */);
186
+ tslib.__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, 1 /* SizeOf.UINT8 */);
187
+ }
188
+ /**
189
+ * @method writeBoolean
190
+ * @description 在缓冲区中写入布尔值,true 写 1,false写 0
191
+ * @param {boolean} value 布尔值
192
+ */
193
+ writeBoolean(value) {
194
+ this.writeUint8(value ? 1 : 0);
195
+ }
196
+ /**
197
+ * @method writeInt16
198
+ * @description 在缓冲区中写入一个 16 位有符号整数
199
+ * @param {number} value 要写入的 16 位有符号整数
200
+ * @param {boolean} [littleEndian] 是否为小端字节序
201
+ */
202
+ writeInt16(value, littleEndian) {
203
+ tslib.__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_alloc).call(this, 2 /* SizeOf.INT16 */);
204
+ tslib
205
+ .__classPrivateFieldGet(this, _Buffer_dataView, 'f')
206
+ .setInt16(tslib.__classPrivateFieldGet(this, _Buffer_offset, 'f'), value, littleEndian);
207
+ tslib.__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_grow).call(this, 2 /* SizeOf.INT16 */);
208
+ tslib.__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, 2 /* SizeOf.INT16 */);
209
+ }
210
+ /**
211
+ * @method writeUint16
212
+ * @description 在缓冲区中写入一个 16 位无符号整数
213
+ * @param {number} value 要写入的 16 位无符号整数
214
+ * @param {boolean} [littleEndian] 是否为小端字节序
215
+ */
216
+ writeUint16(value, littleEndian) {
217
+ tslib.__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_alloc).call(this, 2 /* SizeOf.UINT16 */);
218
+ tslib
219
+ .__classPrivateFieldGet(this, _Buffer_dataView, 'f')
220
+ .setUint16(tslib.__classPrivateFieldGet(this, _Buffer_offset, 'f'), value, littleEndian);
221
+ tslib.__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_grow).call(this, 2 /* SizeOf.UINT16 */);
222
+ tslib.__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, 2 /* SizeOf.UINT16 */);
223
+ }
224
+ /**
225
+ * @method writeInt32
226
+ * @description 在缓冲区中写入一个有符号的 32 位有符号整数
227
+ * @param {number} value 要写入的 32 位有符号整数
228
+ * @param {boolean} [littleEndian] 是否为小端字节序
229
+ */
230
+ writeInt32(value, littleEndian) {
231
+ tslib.__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_alloc).call(this, 4 /* SizeOf.INT32 */);
232
+ tslib
233
+ .__classPrivateFieldGet(this, _Buffer_dataView, 'f')
234
+ .setInt32(tslib.__classPrivateFieldGet(this, _Buffer_offset, 'f'), value, littleEndian);
235
+ tslib.__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_grow).call(this, 4 /* SizeOf.INT32 */);
236
+ tslib.__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, 4 /* SizeOf.INT32 */);
237
+ }
238
+ /**
239
+ * @method writeUint32
240
+ * @description 在缓冲区中写入一个无符号的 32 位无符号整数
241
+ * @param {number} value 要写入的 32 位无符号整数
242
+ * @param {boolean} [littleEndian] 是否为小端字节序
243
+ */
244
+ writeUint32(value, littleEndian) {
245
+ tslib.__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_alloc).call(this, 4 /* SizeOf.UINT32 */);
246
+ tslib
247
+ .__classPrivateFieldGet(this, _Buffer_dataView, 'f')
248
+ .setUint32(tslib.__classPrivateFieldGet(this, _Buffer_offset, 'f'), value, littleEndian);
249
+ tslib.__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_grow).call(this, 4 /* SizeOf.UINT32 */);
250
+ tslib.__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, 4 /* SizeOf.UINT32 */);
251
+ }
252
+ /**
253
+ * @method writeInt64
254
+ * @description 在缓冲区中写入一个无符号的 64 位有符号整数
255
+ * @param {bigint} value 要写入的 32 位有符号整数
256
+ * @param {boolean} [littleEndian] 是否为小端字节序
257
+ */
258
+ writeInt64(value, littleEndian) {
259
+ tslib.__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_alloc).call(this, 8 /* SizeOf.INI64 */);
260
+ tslib
261
+ .__classPrivateFieldGet(this, _Buffer_dataView, 'f')
262
+ .setBigInt64(tslib.__classPrivateFieldGet(this, _Buffer_offset, 'f'), value, littleEndian);
263
+ tslib.__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_grow).call(this, 8 /* SizeOf.INI64 */);
264
+ tslib.__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, 8 /* SizeOf.INI64 */);
265
+ }
266
+ /**
267
+ * @method writeUint64
268
+ * @description 在缓冲区中写入一个无符号的 64 位无符号整数
269
+ * @param {bigint} value 要写入的 64 位无符号整数
270
+ * @param {boolean} [littleEndian] 是否为小端字节序
271
+ */
272
+ writeUint64(value, littleEndian) {
273
+ tslib.__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_alloc).call(this, 8 /* SizeOf.UINT64 */);
274
+ tslib
275
+ .__classPrivateFieldGet(this, _Buffer_dataView, 'f')
276
+ .setBigUint64(tslib.__classPrivateFieldGet(this, _Buffer_offset, 'f'), value, littleEndian);
277
+ tslib.__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_grow).call(this, 8 /* SizeOf.UINT64 */);
278
+ tslib.__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, 8 /* SizeOf.UINT64 */);
279
+ }
280
+ /**
281
+ * @method writeFloat32
282
+ * @description 在缓冲区中写入一个 IEEE 754 单精度 32 位浮点数
283
+ * @param {number} value 单精度 32 位浮点数
284
+ * @param {boolean} [littleEndian] 是否为小端字节序
285
+ */
286
+ writeFloat32(value, littleEndian) {
287
+ tslib.__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_alloc).call(this, 4 /* SizeOf.FLOAT32 */);
288
+ tslib
289
+ .__classPrivateFieldGet(this, _Buffer_dataView, 'f')
290
+ .setFloat32(tslib.__classPrivateFieldGet(this, _Buffer_offset, 'f'), value, littleEndian);
291
+ tslib.__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_grow).call(this, 4 /* SizeOf.FLOAT32 */);
292
+ tslib.__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, 4 /* SizeOf.FLOAT32 */);
293
+ }
294
+ /**
295
+ * @method writeFloat64
296
+ * @description 在缓冲区中写入一个 IEEE 754 双精度 64 位浮点数
297
+ * @param {number} value 双精度 64 位浮点数
298
+ * @param {boolean} [littleEndian] 是否为小端字节序
299
+ */
300
+ writeFloat64(value, littleEndian) {
301
+ tslib.__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_alloc).call(this, 8 /* SizeOf.FLOAT64 */);
302
+ tslib
303
+ .__classPrivateFieldGet(this, _Buffer_dataView, 'f')
304
+ .setFloat64(tslib.__classPrivateFieldGet(this, _Buffer_offset, 'f'), value, littleEndian);
305
+ tslib.__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_grow).call(this, 8 /* SizeOf.FLOAT64 */);
306
+ tslib.__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, 8 /* SizeOf.FLOAT64 */);
307
+ }
308
+ write(input, start, end) {
309
+ let bytes;
310
+ if (input instanceof Uint8Array) {
311
+ bytes = input.subarray(start, end);
312
+ } else {
313
+ bytes = index.encode(input, start);
314
+ }
315
+ const { length } = bytes;
316
+ if (length > 0) {
317
+ tslib.__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_alloc).call(this, length);
318
+ tslib
319
+ .__classPrivateFieldGet(this, _Buffer_bytes, 'f')
320
+ .set(bytes, tslib.__classPrivateFieldGet(this, _Buffer_offset, 'f'));
321
+ tslib.__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_grow).call(this, length);
322
+ tslib.__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, length);
323
+ }
324
+ }
325
+ /**
326
+ * @method readInt8
327
+ * @description 从缓冲区中读取有符号的整数
328
+ * @returns {number} 介于 -128 和 127 之间的整数
329
+ */
330
+ readInt8() {
331
+ tslib.__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_assertRead).call(this, 1 /* SizeOf.INT8 */);
332
+ const value = tslib
333
+ .__classPrivateFieldGet(this, _Buffer_dataView, 'f')
334
+ .getInt8(tslib.__classPrivateFieldGet(this, _Buffer_offset, 'f'));
335
+ tslib.__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, 1 /* SizeOf.INT8 */);
336
+ return value;
337
+ }
338
+ /**
339
+ * @method readUint8
340
+ * @description 从缓冲区中读取无符号的整数
341
+ * @returns {number} 介于 0 和 255 之间的无符号整数
342
+ */
343
+ readUint8() {
344
+ tslib.__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_assertRead).call(this, 1 /* SizeOf.UINT8 */);
345
+ const value = tslib
346
+ .__classPrivateFieldGet(this, _Buffer_dataView, 'f')
347
+ .getUint8(tslib.__classPrivateFieldGet(this, _Buffer_offset, 'f'));
348
+ tslib.__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, 1 /* SizeOf.UINT8 */);
349
+ return value;
350
+ }
351
+ /**
352
+ * @method readBoolean
353
+ * @description 从缓冲区中读取布尔值
354
+ * @returns {boolean} 如果字节非零,则返回 true,否则返回 false
355
+ */
356
+ readBoolean() {
357
+ return Boolean(this.readUint8());
358
+ }
359
+ /**
360
+ * @method readInt16
361
+ * @description 从缓冲区中读取一个 16 位有符号整数
362
+ * @param {boolean} [littleEndian] 是否为小端字节序
363
+ * @returns {number} 介于 -32768 和 32767 之间的 16 位有符号整数
364
+ */
365
+ readInt16(littleEndian) {
366
+ tslib.__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_assertRead).call(this, 2 /* SizeOf.INT16 */);
367
+ const value = tslib
368
+ .__classPrivateFieldGet(this, _Buffer_dataView, 'f')
369
+ .getInt16(tslib.__classPrivateFieldGet(this, _Buffer_offset, 'f'), littleEndian);
370
+ tslib.__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, 2 /* SizeOf.INT16 */);
371
+ return value;
372
+ }
373
+ /**
374
+ * @method readUint16
375
+ * @description 从缓冲区中读取一个 16 位无符号整数
376
+ * @param {boolean} [littleEndian] 是否为小端字节序
377
+ * @returns {number} 介于 0 和 65535 之间的 16 位无符号整数
378
+ */
379
+ readUint16(littleEndian) {
380
+ tslib.__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_assertRead).call(this, 2 /* SizeOf.UINT16 */);
381
+ const value = tslib
382
+ .__classPrivateFieldGet(this, _Buffer_dataView, 'f')
383
+ .getUint16(tslib.__classPrivateFieldGet(this, _Buffer_offset, 'f'), littleEndian);
384
+ tslib.__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, 2 /* SizeOf.UINT16 */);
385
+ return value;
386
+ }
387
+ /**
388
+ * @method readInt32
389
+ * @description 从缓冲区中读取一个 32 位有符号整数
390
+ * @param {boolean} [littleEndian] 是否为小端字节序
391
+ * @returns {number} 介于 -2147483648 和 2147483647 之间的 32 位有符号整数
392
+ */
393
+ readInt32(littleEndian) {
394
+ tslib.__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_assertRead).call(this, 4 /* SizeOf.INT32 */);
395
+ const value = tslib
396
+ .__classPrivateFieldGet(this, _Buffer_dataView, 'f')
397
+ .getInt32(tslib.__classPrivateFieldGet(this, _Buffer_offset, 'f'), littleEndian);
398
+ tslib.__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, 4 /* SizeOf.INT32 */);
399
+ return value;
400
+ }
401
+ /**
402
+ * @method readUint32
403
+ * @description 从缓冲区中读取一个 32 位无符号整数
404
+ * @param {boolean} [littleEndian] 是否为小端字节序
405
+ * @returns {number} 介于 0 和 4294967295 之间的 32 位无符号整数
406
+ */
407
+ readUint32(littleEndian) {
408
+ tslib.__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_assertRead).call(this, 4 /* SizeOf.UINT32 */);
409
+ const value = tslib
410
+ .__classPrivateFieldGet(this, _Buffer_dataView, 'f')
411
+ .getUint32(tslib.__classPrivateFieldGet(this, _Buffer_offset, 'f'), littleEndian);
412
+ tslib.__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, 4 /* SizeOf.UINT32 */);
413
+ return value;
414
+ }
415
+ /**
416
+ * @method readInt64
417
+ * @description 从缓冲区中读取一个 64 位有符号整数
418
+ * @param {boolean} [littleEndian] 是否为小端字节序
419
+ * @returns {bigint} 介于 -9223372036854775808 和 9223372036854775807 之间的 64 位有符号整数
420
+ */
421
+ readInt64(littleEndian) {
422
+ tslib.__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_assertRead).call(this, 8 /* SizeOf.INI64 */);
423
+ const value = tslib
424
+ .__classPrivateFieldGet(this, _Buffer_dataView, 'f')
425
+ .getBigInt64(tslib.__classPrivateFieldGet(this, _Buffer_offset, 'f'), littleEndian);
426
+ tslib.__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, 8 /* SizeOf.INI64 */);
427
+ return value;
428
+ }
429
+ /**
430
+ * @method readUint64
431
+ * @description 从缓冲区中读取一个 64 位无符号整数
432
+ * @param {boolean} [littleEndian] 是否为小端字节序
433
+ * @returns {bigint} 介于 0 和 18446744073709551615 之间的 64 位无符号整数
434
+ */
435
+ readUint64(littleEndian) {
436
+ tslib.__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_assertRead).call(this, 8 /* SizeOf.UINT64 */);
437
+ const value = tslib
438
+ .__classPrivateFieldGet(this, _Buffer_dataView, 'f')
439
+ .getBigUint64(tslib.__classPrivateFieldGet(this, _Buffer_offset, 'f'), littleEndian);
440
+ tslib.__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, 8 /* SizeOf.UINT64 */);
441
+ return value;
442
+ }
443
+ /**
444
+ * @method readFloat32
445
+ * @description 从缓冲区中读取一个 IEEE 754 单精度 32 位浮点数
446
+ * @param {boolean} [littleEndian] 是否为小端字节序
447
+ * @returns {number} 单精度 32 位浮点数
448
+ */
449
+ readFloat32(littleEndian) {
450
+ tslib.__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_assertRead).call(this, 4 /* SizeOf.FLOAT32 */);
451
+ const value = tslib
452
+ .__classPrivateFieldGet(this, _Buffer_dataView, 'f')
453
+ .getFloat32(tslib.__classPrivateFieldGet(this, _Buffer_offset, 'f'), littleEndian);
454
+ tslib.__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, 4 /* SizeOf.FLOAT32 */);
455
+ return value;
456
+ }
457
+ /**
458
+ * @method readFloat64
459
+ * @description 从缓冲区中读取一个 IEEE 754 双精度 64 位浮点数
460
+ * @param {boolean} [littleEndian] 是否为小端字节序
461
+ * @returns {number} 双精度 64 位浮点数
462
+ */
463
+ readFloat64(littleEndian) {
464
+ tslib.__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_assertRead).call(this, 8 /* SizeOf.FLOAT64 */);
465
+ const value = tslib
466
+ .__classPrivateFieldGet(this, _Buffer_dataView, 'f')
467
+ .getFloat64(tslib.__classPrivateFieldGet(this, _Buffer_offset, 'f'), littleEndian);
468
+ tslib.__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, 8 /* SizeOf.FLOAT64 */);
469
+ return value;
470
+ }
471
+ read(length, encoding) {
472
+ tslib.__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_assertRead).call(this, length);
473
+ const offset = tslib.__classPrivateFieldGet(this, _Buffer_offset, 'f');
474
+ const bytes = tslib.__classPrivateFieldGet(this, _Buffer_bytes, 'f').slice(offset, offset + length);
475
+ tslib.__classPrivateFieldGet(this, _Buffer_instances, 'm', _Buffer_seek).call(this, length);
476
+ if (arguments.length >= 2) {
477
+ return index.decode(bytes, encoding);
478
+ }
479
+ return bytes;
480
+ }
481
+ /**
482
+ * @public
483
+ * @method slice
484
+ * @description 从指定开始和结束位置索引截取并返回新的 Buffer 对象
485
+ * @param {number} [start] 截取开始位置索引
486
+ * @param {number} [end] 截取结束位置索引
487
+ * @returns {Buffer}
488
+ */
489
+ slice(start, end) {
490
+ const bytes = tslib.__classPrivateFieldGet(this, _Buffer_bytes, 'f').slice(start, end);
491
+ return new Buffer(bytes, tslib.__classPrivateFieldGet(this, _Buffer_pageSize, 'f'));
492
+ }
493
+ /**
494
+ * @public
495
+ * @method copyWithin
496
+ * @description 从 Buffer 对象中将指定位置的数据复制到以 target 起始的位置
497
+ * @param {number} target 粘贴开始位置索引
498
+ * @param {number} start 复制开始位置索引
499
+ * @param {number} [end] 复制结束位置索引
500
+ * @returns {this}
501
+ */
502
+ copyWithin(target, start, end) {
503
+ tslib.__classPrivateFieldGet(this, _Buffer_bytes, 'f').copyWithin(target, start, end);
504
+ return this;
505
+ }
506
+ /**
507
+ * @override
508
+ * @method toString
509
+ * @description 获取 Buffer 对象二进制编码字符串
510
+ * @returns {string}
511
+ */
512
+ toString() {
513
+ // 二进制编码字符串
514
+ let binary = '';
515
+ // 提前获取 bytes,防止重复计算
516
+ const bytes = this.bytes;
517
+ // 获取二进制编码
518
+ for (const byte of bytes) {
519
+ binary += Binary.mapping[byte];
520
+ }
521
+ // 返回二进制编码
522
+ return binary;
523
+ }
524
+ }
525
+ (_Buffer_pageSize = new WeakMap()),
526
+ (_Buffer_bytes = new WeakMap()),
527
+ (_Buffer_dataView = new WeakMap()),
528
+ (_Buffer_offset = new WeakMap()),
529
+ (_Buffer_length = new WeakMap()),
530
+ (_Buffer_instances = new WeakSet()),
531
+ (_Buffer_grow = function _Buffer_grow(length) {
532
+ tslib.__classPrivateFieldSet(this, _Buffer_length, tslib.__classPrivateFieldGet(this, _Buffer_length, 'f') + length, 'f');
533
+ }),
534
+ (_Buffer_seek = function _Buffer_seek(offset) {
535
+ tslib.__classPrivateFieldSet(this, _Buffer_offset, tslib.__classPrivateFieldGet(this, _Buffer_offset, 'f') + offset, 'f');
536
+ }),
537
+ (_Buffer_assertRead = function _Buffer_assertRead(length) {
538
+ if (
539
+ length < 0 ||
540
+ tslib.__classPrivateFieldGet(this, _Buffer_offset, 'f') + length > tslib.__classPrivateFieldGet(this, _Buffer_length, 'f')
541
+ ) {
542
+ throw new RangeError(errors.readOverflow);
543
+ }
544
+ }),
545
+ (_Buffer_alloc = function _Buffer_alloc(length) {
546
+ length += tslib.__classPrivateFieldGet(this, _Buffer_offset, 'f');
547
+ const bytes = tslib.__classPrivateFieldGet(this, _Buffer_bytes, 'f');
548
+ if (length > bytes.length) {
549
+ const newBytes = new Uint8Array(
550
+ utils.calcBufferLength(length, tslib.__classPrivateFieldGet(this, _Buffer_pageSize, 'f'))
551
+ );
552
+ newBytes.set(bytes);
553
+ tslib.__classPrivateFieldSet(this, _Buffer_bytes, newBytes, 'f');
554
+ tslib.__classPrivateFieldSet(this, _Buffer_dataView, new DataView(newBytes.buffer), 'f');
555
+ }
556
+ });
557
+
558
+ exports.Buffer = Buffer;
559
+ exports.endianness = endianness;