@nuintun/buffer 0.2.5 → 0.3.1
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/enum.cjs +20 -0
- package/cjs/enum.d.cts +4 -0
- package/cjs/errors.cjs +13 -9
- package/cjs/errors.d.cts +8 -2
- package/cjs/index.cjs +120 -96
- package/cjs/index.d.cts +2 -4
- package/cjs/utils.cjs +20 -12
- package/cjs/utils.d.cts +12 -5
- 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/enum.d.ts +4 -0
- package/esm/enum.js +20 -0
- package/esm/errors.d.ts +8 -2
- package/esm/errors.js +13 -9
- package/esm/index.d.ts +2 -4
- package/esm/index.js +113 -95
- package/esm/utils.d.ts +12 -5
- package/esm/utils.js +19 -12
- package/package.json +5 -5
package/cjs/utils.d.cts
CHANGED
|
@@ -2,10 +2,17 @@
|
|
|
2
2
|
* @module utils
|
|
3
3
|
*/
|
|
4
4
|
/**
|
|
5
|
-
* @function
|
|
6
|
-
* @description
|
|
7
|
-
* @param
|
|
5
|
+
* @function isNaturalNumber
|
|
6
|
+
* @description 判断是否为自然数
|
|
7
|
+
* @param value 待判断的值
|
|
8
|
+
* @returns {boolean}
|
|
9
|
+
*/
|
|
10
|
+
export declare function isNaturalNumber(value: number): boolean;
|
|
11
|
+
/**
|
|
12
|
+
* @function makeUint8Array
|
|
13
|
+
* @description 创建一个合适长度的 Uint8Array
|
|
14
|
+
* @param {number} byteLength 数据字节总大小
|
|
8
15
|
* @param {number} pageSize 缓冲区页大小
|
|
9
|
-
* @returns {
|
|
16
|
+
* @returns {Uint8Array}
|
|
10
17
|
*/
|
|
11
|
-
export declare function
|
|
18
|
+
export declare function makeUint8Array(byteLength: number, pageSize: number): Uint8Array;
|
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/enum.d.ts
CHANGED
package/esm/enum.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @package @nuintun/buffer
|
|
3
|
+
* @license MIT
|
|
4
|
+
* @version 0.3.1
|
|
5
|
+
* @author nuintun <nuintun@qq.com>
|
|
6
|
+
* @description A buffer tool for javascript.
|
|
7
|
+
* @see https://github.com/nuintun/Buffer#readme
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @module enum
|
|
12
|
+
*/
|
|
13
|
+
// 字节序类型
|
|
14
|
+
var Endian;
|
|
15
|
+
(function (Endian) {
|
|
16
|
+
Endian[(Endian['Big'] = 0)] = 'Big';
|
|
17
|
+
Endian[(Endian['Little'] = 1)] = 'Little';
|
|
18
|
+
})(Endian || (Endian = {}));
|
|
19
|
+
|
|
20
|
+
export { Endian };
|
package/esm/errors.d.ts
CHANGED
|
@@ -1,9 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @module errors
|
|
3
3
|
*/
|
|
4
|
+
/**
|
|
5
|
+
* @function encodingInvalid
|
|
6
|
+
* @description 未支持的编码格式
|
|
7
|
+
* @param encoding 编码格式
|
|
8
|
+
*/
|
|
4
9
|
export declare function encodingInvalid(encoding: string): string;
|
|
5
|
-
export declare const unknownEndianness = 'unknown endianness';
|
|
6
|
-
export declare const lengthInvalid = 'invalid buffer length';
|
|
7
10
|
export declare const offsetInvalid = 'invalid buffer offset';
|
|
11
|
+
export declare const lengthInvalid = 'invalid buffer length';
|
|
12
|
+
export declare const unknownEndianness = 'unknown endianness';
|
|
13
|
+
export declare const readLengthInvalid = 'invalid read length';
|
|
8
14
|
export declare const readOverflow = 'read is outside the bounds of the Buffer';
|
|
9
15
|
export declare const offsetOverflow = 'offset is outside the bounds of the Buffer';
|
package/esm/errors.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @package @nuintun/buffer
|
|
3
3
|
* @license MIT
|
|
4
|
-
* @version 0.
|
|
4
|
+
* @version 0.3.1
|
|
5
5
|
* @author nuintun <nuintun@qq.com>
|
|
6
6
|
* @description A buffer tool for javascript.
|
|
7
7
|
* @see https://github.com/nuintun/Buffer#readme
|
|
@@ -10,19 +10,23 @@
|
|
|
10
10
|
/**
|
|
11
11
|
* @module errors
|
|
12
12
|
*/
|
|
13
|
-
|
|
13
|
+
/**
|
|
14
|
+
* @function encodingInvalid
|
|
15
|
+
* @description 未支持的编码格式
|
|
16
|
+
* @param encoding 编码格式
|
|
17
|
+
*/
|
|
14
18
|
function encodingInvalid(encoding) {
|
|
15
19
|
return 'unsupported encoding ' + encoding;
|
|
16
20
|
}
|
|
17
|
-
// 未知字节序
|
|
18
|
-
const unknownEndianness = 'unknown endianness';
|
|
19
|
-
// 非法长度
|
|
20
|
-
const lengthInvalid = 'invalid buffer length';
|
|
21
21
|
// 非法读写指针
|
|
22
22
|
const offsetInvalid = 'invalid buffer offset';
|
|
23
|
+
// 非法长度
|
|
24
|
+
const lengthInvalid = 'invalid buffer length';
|
|
25
|
+
// 未知字节序
|
|
26
|
+
const unknownEndianness = 'unknown endianness';
|
|
27
|
+
// 数据读取长度非法
|
|
28
|
+
const readLengthInvalid = 'invalid read length';
|
|
23
29
|
// 数据读取溢出
|
|
24
30
|
const readOverflow = 'read is outside the bounds of the Buffer';
|
|
25
|
-
// 读写指针溢出
|
|
26
|
-
const offsetOverflow = 'offset is outside the bounds of the Buffer';
|
|
27
31
|
|
|
28
|
-
export { encodingInvalid, lengthInvalid, offsetInvalid,
|
|
32
|
+
export { encodingInvalid, lengthInvalid, offsetInvalid, readLengthInvalid, readOverflow, unknownEndianness };
|
package/esm/index.d.ts
CHANGED
package/esm/index.js
CHANGED
|
@@ -1,26 +1,21 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @package @nuintun/buffer
|
|
3
3
|
* @license MIT
|
|
4
|
-
* @version 0.
|
|
4
|
+
* @version 0.3.1
|
|
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 { calcBufferLength } from './utils.js';
|
|
11
10
|
import { mapping } from './Binary.js';
|
|
12
|
-
import { unknownEndianness, readOverflow, offsetInvalid,
|
|
11
|
+
import { unknownEndianness, readOverflow, offsetInvalid, lengthInvalid, readLengthInvalid } from './errors.js';
|
|
13
12
|
import { encode, decode } from './Encoding/index.js';
|
|
13
|
+
import { Endian } from './enum.js';
|
|
14
|
+
import { makeUint8Array, isNaturalNumber } from './utils.js';
|
|
14
15
|
|
|
15
16
|
/**
|
|
16
17
|
* @module Buffer
|
|
17
18
|
*/
|
|
18
|
-
// 字节序类型
|
|
19
|
-
var Endian;
|
|
20
|
-
(function (Endian) {
|
|
21
|
-
Endian[(Endian['Big'] = 0)] = 'Big';
|
|
22
|
-
Endian[(Endian['Little'] = 1)] = 'Little';
|
|
23
|
-
})(Endian || (Endian = {}));
|
|
24
19
|
/**
|
|
25
20
|
* @function endianness
|
|
26
21
|
* @description 获取系统默认字节序
|
|
@@ -52,45 +47,59 @@ class Buffer {
|
|
|
52
47
|
#offset = 0;
|
|
53
48
|
// 已使用字节长度
|
|
54
49
|
#length = 0;
|
|
50
|
+
/**
|
|
51
|
+
* @constructor
|
|
52
|
+
* @param {number | Uint8Array} input 缓冲区初始配置
|
|
53
|
+
* @param {number} pageSize 缓冲区分页大小,扩容时将按分页大小增加
|
|
54
|
+
*/
|
|
55
55
|
constructor(input = 0, pageSize = 4096) {
|
|
56
|
-
|
|
56
|
+
let length;
|
|
57
|
+
let bytes;
|
|
58
|
+
let dataView;
|
|
57
59
|
if (input instanceof Uint8Array) {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
60
|
+
length = input.length;
|
|
61
|
+
bytes = makeUint8Array(length, pageSize);
|
|
62
|
+
bytes.set(input);
|
|
63
|
+
dataView = new DataView(bytes.buffer);
|
|
61
64
|
} else {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
this.#dataView = new DataView(bytes.buffer);
|
|
65
|
+
length = input;
|
|
66
|
+
bytes = makeUint8Array(input, pageSize);
|
|
67
|
+
dataView = new DataView(bytes.buffer);
|
|
66
68
|
}
|
|
69
|
+
this.#bytes = bytes;
|
|
70
|
+
this.#length = length;
|
|
71
|
+
this.#dataView = dataView;
|
|
72
|
+
this.#pageSize = pageSize;
|
|
67
73
|
}
|
|
68
74
|
/**
|
|
69
75
|
* @private
|
|
70
|
-
* @method
|
|
71
|
-
* @description
|
|
72
|
-
* @param {number}
|
|
76
|
+
* @method seek
|
|
77
|
+
* @description 移动读写指针
|
|
78
|
+
* @param {number} offset 指针位置
|
|
73
79
|
*/
|
|
74
|
-
#
|
|
75
|
-
this.#length
|
|
80
|
+
#seek(offset) {
|
|
81
|
+
if (offset > this.#length) {
|
|
82
|
+
this.#length = offset;
|
|
83
|
+
}
|
|
84
|
+
this.#offset = offset;
|
|
76
85
|
}
|
|
77
86
|
/**
|
|
78
87
|
* @private
|
|
79
|
-
* @method
|
|
80
|
-
* @description
|
|
81
|
-
* @param
|
|
88
|
+
* @method getOffset
|
|
89
|
+
* @description 根据数据类型获取最新指针位置
|
|
90
|
+
* @param size 数据类型长度
|
|
82
91
|
*/
|
|
83
|
-
#
|
|
84
|
-
this.#offset
|
|
92
|
+
#getOffset(size) {
|
|
93
|
+
return this.#offset + size;
|
|
85
94
|
}
|
|
86
95
|
/**
|
|
87
96
|
* @private
|
|
88
97
|
* @method assertRead
|
|
89
98
|
* @description 读取断言,防止越界读取
|
|
90
|
-
* @param {number}
|
|
99
|
+
* @param {number} size 断言字节长度
|
|
91
100
|
*/
|
|
92
101
|
#assertRead(length) {
|
|
93
|
-
if (length
|
|
102
|
+
if (length > this.#length) {
|
|
94
103
|
throw new RangeError(readOverflow);
|
|
95
104
|
}
|
|
96
105
|
}
|
|
@@ -101,10 +110,9 @@ class Buffer {
|
|
|
101
110
|
* @param {number} length 分配字节长度
|
|
102
111
|
*/
|
|
103
112
|
#alloc(length) {
|
|
104
|
-
length += this.#offset;
|
|
105
113
|
const bytes = this.#bytes;
|
|
106
114
|
if (length > bytes.length) {
|
|
107
|
-
const newBytes =
|
|
115
|
+
const newBytes = makeUint8Array(length, this.#pageSize);
|
|
108
116
|
newBytes.set(bytes);
|
|
109
117
|
this.#bytes = newBytes;
|
|
110
118
|
this.#dataView = new DataView(newBytes.buffer);
|
|
@@ -117,12 +125,9 @@ class Buffer {
|
|
|
117
125
|
* @description 下一次调用读写方法时将在此位置开始读写
|
|
118
126
|
*/
|
|
119
127
|
set offset(offset) {
|
|
120
|
-
if (offset
|
|
128
|
+
if (!isNaturalNumber(offset)) {
|
|
121
129
|
throw new RangeError(offsetInvalid);
|
|
122
130
|
}
|
|
123
|
-
if (offset > this.#length) {
|
|
124
|
-
throw new RangeError(offsetOverflow);
|
|
125
|
-
}
|
|
126
131
|
this.#offset = offset;
|
|
127
132
|
}
|
|
128
133
|
/**
|
|
@@ -142,7 +147,7 @@ class Buffer {
|
|
|
142
147
|
* @description 如果将长度设置为大于当前长度的值,则用零填充字节数组的右侧
|
|
143
148
|
*/
|
|
144
149
|
set length(length) {
|
|
145
|
-
if (length
|
|
150
|
+
if (!isNaturalNumber(length)) {
|
|
146
151
|
throw new RangeError(lengthInvalid);
|
|
147
152
|
}
|
|
148
153
|
const currentLength = this.#length;
|
|
@@ -173,7 +178,7 @@ class Buffer {
|
|
|
173
178
|
* @returns {ArrayBuffer}
|
|
174
179
|
*/
|
|
175
180
|
get buffer() {
|
|
176
|
-
return this
|
|
181
|
+
return this.bytes.buffer;
|
|
177
182
|
}
|
|
178
183
|
/**
|
|
179
184
|
* @public
|
|
@@ -182,7 +187,7 @@ class Buffer {
|
|
|
182
187
|
* @returns {Uint8Array}
|
|
183
188
|
*/
|
|
184
189
|
get bytes() {
|
|
185
|
-
return this.#bytes.
|
|
190
|
+
return this.#bytes.subarray(0, this.#length);
|
|
186
191
|
}
|
|
187
192
|
/**
|
|
188
193
|
* @public
|
|
@@ -191,10 +196,10 @@ class Buffer {
|
|
|
191
196
|
* @param {number} value 介于 -128 和 127 之间的整数
|
|
192
197
|
*/
|
|
193
198
|
writeInt8(value) {
|
|
194
|
-
this.#
|
|
199
|
+
const offset = this.#getOffset(1 /* SizeOf.INT8 */);
|
|
200
|
+
this.#alloc(offset);
|
|
195
201
|
this.#dataView.setInt8(this.#offset, value);
|
|
196
|
-
this.#
|
|
197
|
-
this.#seek(1 /* SizeOf.INT8 */);
|
|
202
|
+
this.#seek(offset);
|
|
198
203
|
}
|
|
199
204
|
/**
|
|
200
205
|
* @public
|
|
@@ -203,10 +208,10 @@ class Buffer {
|
|
|
203
208
|
* @param {number} value 介于 0 和 255 之间的整数
|
|
204
209
|
*/
|
|
205
210
|
writeUint8(value) {
|
|
206
|
-
this.#
|
|
211
|
+
const offset = this.#getOffset(1 /* SizeOf.UINT8 */);
|
|
212
|
+
this.#alloc(offset);
|
|
207
213
|
this.#dataView.setUint8(this.#offset, value);
|
|
208
|
-
this.#
|
|
209
|
-
this.#seek(1 /* SizeOf.UINT8 */);
|
|
214
|
+
this.#seek(offset);
|
|
210
215
|
}
|
|
211
216
|
/**
|
|
212
217
|
* @method writeBoolean
|
|
@@ -223,10 +228,10 @@ class Buffer {
|
|
|
223
228
|
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
224
229
|
*/
|
|
225
230
|
writeInt16(value, littleEndian) {
|
|
226
|
-
this.#
|
|
231
|
+
const offset = this.#getOffset(2 /* SizeOf.INT16 */);
|
|
232
|
+
this.#alloc(offset);
|
|
227
233
|
this.#dataView.setInt16(this.#offset, value, littleEndian);
|
|
228
|
-
this.#
|
|
229
|
-
this.#seek(2 /* SizeOf.INT16 */);
|
|
234
|
+
this.#seek(offset);
|
|
230
235
|
}
|
|
231
236
|
/**
|
|
232
237
|
* @method writeUint16
|
|
@@ -235,10 +240,10 @@ class Buffer {
|
|
|
235
240
|
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
236
241
|
*/
|
|
237
242
|
writeUint16(value, littleEndian) {
|
|
238
|
-
this.#
|
|
243
|
+
const offset = this.#getOffset(2 /* SizeOf.UINT16 */);
|
|
244
|
+
this.#alloc(offset);
|
|
239
245
|
this.#dataView.setUint16(this.#offset, value, littleEndian);
|
|
240
|
-
this.#
|
|
241
|
-
this.#seek(2 /* SizeOf.UINT16 */);
|
|
246
|
+
this.#seek(offset);
|
|
242
247
|
}
|
|
243
248
|
/**
|
|
244
249
|
* @method writeInt32
|
|
@@ -247,10 +252,10 @@ class Buffer {
|
|
|
247
252
|
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
248
253
|
*/
|
|
249
254
|
writeInt32(value, littleEndian) {
|
|
250
|
-
this.#
|
|
255
|
+
const offset = this.#getOffset(4 /* SizeOf.INT32 */);
|
|
256
|
+
this.#alloc(offset);
|
|
251
257
|
this.#dataView.setInt32(this.#offset, value, littleEndian);
|
|
252
|
-
this.#
|
|
253
|
-
this.#seek(4 /* SizeOf.INT32 */);
|
|
258
|
+
this.#seek(offset);
|
|
254
259
|
}
|
|
255
260
|
/**
|
|
256
261
|
* @method writeUint32
|
|
@@ -259,10 +264,10 @@ class Buffer {
|
|
|
259
264
|
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
260
265
|
*/
|
|
261
266
|
writeUint32(value, littleEndian) {
|
|
262
|
-
this.#
|
|
267
|
+
const offset = this.#getOffset(4 /* SizeOf.UINT32 */);
|
|
268
|
+
this.#alloc(offset);
|
|
263
269
|
this.#dataView.setUint32(this.#offset, value, littleEndian);
|
|
264
|
-
this.#
|
|
265
|
-
this.#seek(4 /* SizeOf.UINT32 */);
|
|
270
|
+
this.#seek(offset);
|
|
266
271
|
}
|
|
267
272
|
/**
|
|
268
273
|
* @method writeInt64
|
|
@@ -271,10 +276,10 @@ class Buffer {
|
|
|
271
276
|
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
272
277
|
*/
|
|
273
278
|
writeInt64(value, littleEndian) {
|
|
274
|
-
this.#
|
|
279
|
+
const offset = this.#getOffset(8 /* SizeOf.INT64 */);
|
|
280
|
+
this.#alloc(offset);
|
|
275
281
|
this.#dataView.setBigInt64(this.#offset, value, littleEndian);
|
|
276
|
-
this.#
|
|
277
|
-
this.#seek(8 /* SizeOf.INT64 */);
|
|
282
|
+
this.#seek(offset);
|
|
278
283
|
}
|
|
279
284
|
/**
|
|
280
285
|
* @method writeUint64
|
|
@@ -283,10 +288,10 @@ class Buffer {
|
|
|
283
288
|
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
284
289
|
*/
|
|
285
290
|
writeUint64(value, littleEndian) {
|
|
286
|
-
this.#
|
|
291
|
+
const offset = this.#getOffset(8 /* SizeOf.UINT64 */);
|
|
292
|
+
this.#alloc(offset);
|
|
287
293
|
this.#dataView.setBigUint64(this.#offset, value, littleEndian);
|
|
288
|
-
this.#
|
|
289
|
-
this.#seek(8 /* SizeOf.UINT64 */);
|
|
294
|
+
this.#seek(offset);
|
|
290
295
|
}
|
|
291
296
|
/**
|
|
292
297
|
* @method writeFloat32
|
|
@@ -295,10 +300,10 @@ class Buffer {
|
|
|
295
300
|
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
296
301
|
*/
|
|
297
302
|
writeFloat32(value, littleEndian) {
|
|
298
|
-
this.#
|
|
303
|
+
const offset = this.#getOffset(4 /* SizeOf.FLOAT32 */);
|
|
304
|
+
this.#alloc(offset);
|
|
299
305
|
this.#dataView.setFloat32(this.#offset, value, littleEndian);
|
|
300
|
-
this.#
|
|
301
|
-
this.#seek(4 /* SizeOf.FLOAT32 */);
|
|
306
|
+
this.#seek(offset);
|
|
302
307
|
}
|
|
303
308
|
/**
|
|
304
309
|
* @method writeFloat64
|
|
@@ -307,10 +312,10 @@ class Buffer {
|
|
|
307
312
|
* @param {boolean} [littleEndian] 是否为小端字节序
|
|
308
313
|
*/
|
|
309
314
|
writeFloat64(value, littleEndian) {
|
|
310
|
-
this.#
|
|
315
|
+
const offset = this.#getOffset(8 /* SizeOf.FLOAT64 */);
|
|
316
|
+
this.#alloc(offset);
|
|
311
317
|
this.#dataView.setFloat64(this.#offset, value, littleEndian);
|
|
312
|
-
this.#
|
|
313
|
-
this.#seek(8 /* SizeOf.FLOAT64 */);
|
|
318
|
+
this.#seek(offset);
|
|
314
319
|
}
|
|
315
320
|
write(input, start, end) {
|
|
316
321
|
let bytes;
|
|
@@ -321,10 +326,10 @@ class Buffer {
|
|
|
321
326
|
}
|
|
322
327
|
const { length } = bytes;
|
|
323
328
|
if (length > 0) {
|
|
324
|
-
this.#
|
|
329
|
+
const offset = this.#getOffset(length);
|
|
330
|
+
this.#alloc(offset);
|
|
325
331
|
this.#bytes.set(bytes, this.#offset);
|
|
326
|
-
this.#
|
|
327
|
-
this.#seek(length);
|
|
332
|
+
this.#seek(offset);
|
|
328
333
|
}
|
|
329
334
|
}
|
|
330
335
|
/**
|
|
@@ -333,9 +338,10 @@ class Buffer {
|
|
|
333
338
|
* @returns {number} 介于 -128 和 127 之间的整数
|
|
334
339
|
*/
|
|
335
340
|
readInt8() {
|
|
336
|
-
this.#
|
|
341
|
+
const offset = this.#getOffset(1 /* SizeOf.INT8 */);
|
|
342
|
+
this.#assertRead(offset);
|
|
337
343
|
const value = this.#dataView.getInt8(this.#offset);
|
|
338
|
-
this.#seek(
|
|
344
|
+
this.#seek(offset);
|
|
339
345
|
return value;
|
|
340
346
|
}
|
|
341
347
|
/**
|
|
@@ -344,9 +350,10 @@ class Buffer {
|
|
|
344
350
|
* @returns {number} 介于 0 和 255 之间的无符号整数
|
|
345
351
|
*/
|
|
346
352
|
readUint8() {
|
|
347
|
-
this.#
|
|
353
|
+
const offset = this.#getOffset(1 /* SizeOf.UINT8 */);
|
|
354
|
+
this.#assertRead(offset);
|
|
348
355
|
const value = this.#dataView.getUint8(this.#offset);
|
|
349
|
-
this.#seek(
|
|
356
|
+
this.#seek(offset);
|
|
350
357
|
return value;
|
|
351
358
|
}
|
|
352
359
|
/**
|
|
@@ -364,9 +371,10 @@ class Buffer {
|
|
|
364
371
|
* @returns {number} 介于 -32768 和 32767 之间的 16 位有符号整数
|
|
365
372
|
*/
|
|
366
373
|
readInt16(littleEndian) {
|
|
367
|
-
this.#
|
|
374
|
+
const offset = this.#getOffset(2 /* SizeOf.INT16 */);
|
|
375
|
+
this.#assertRead(offset);
|
|
368
376
|
const value = this.#dataView.getInt16(this.#offset, littleEndian);
|
|
369
|
-
this.#seek(
|
|
377
|
+
this.#seek(offset);
|
|
370
378
|
return value;
|
|
371
379
|
}
|
|
372
380
|
/**
|
|
@@ -376,9 +384,10 @@ class Buffer {
|
|
|
376
384
|
* @returns {number} 介于 0 和 65535 之间的 16 位无符号整数
|
|
377
385
|
*/
|
|
378
386
|
readUint16(littleEndian) {
|
|
379
|
-
this.#
|
|
387
|
+
const offset = this.#getOffset(2 /* SizeOf.UINT16 */);
|
|
388
|
+
this.#assertRead(offset);
|
|
380
389
|
const value = this.#dataView.getUint16(this.#offset, littleEndian);
|
|
381
|
-
this.#seek(
|
|
390
|
+
this.#seek(offset);
|
|
382
391
|
return value;
|
|
383
392
|
}
|
|
384
393
|
/**
|
|
@@ -388,9 +397,10 @@ class Buffer {
|
|
|
388
397
|
* @returns {number} 介于 -2147483648 和 2147483647 之间的 32 位有符号整数
|
|
389
398
|
*/
|
|
390
399
|
readInt32(littleEndian) {
|
|
391
|
-
this.#
|
|
400
|
+
const offset = this.#getOffset(4 /* SizeOf.INT32 */);
|
|
401
|
+
this.#assertRead(offset);
|
|
392
402
|
const value = this.#dataView.getInt32(this.#offset, littleEndian);
|
|
393
|
-
this.#seek(
|
|
403
|
+
this.#seek(offset);
|
|
394
404
|
return value;
|
|
395
405
|
}
|
|
396
406
|
/**
|
|
@@ -400,9 +410,10 @@ class Buffer {
|
|
|
400
410
|
* @returns {number} 介于 0 和 4294967295 之间的 32 位无符号整数
|
|
401
411
|
*/
|
|
402
412
|
readUint32(littleEndian) {
|
|
403
|
-
this.#
|
|
413
|
+
const offset = this.#getOffset(4 /* SizeOf.UINT32 */);
|
|
414
|
+
this.#assertRead(offset);
|
|
404
415
|
const value = this.#dataView.getUint32(this.#offset, littleEndian);
|
|
405
|
-
this.#seek(
|
|
416
|
+
this.#seek(offset);
|
|
406
417
|
return value;
|
|
407
418
|
}
|
|
408
419
|
/**
|
|
@@ -412,9 +423,10 @@ class Buffer {
|
|
|
412
423
|
* @returns {bigint} 介于 -9223372036854775808 和 9223372036854775807 之间的 64 位有符号整数
|
|
413
424
|
*/
|
|
414
425
|
readInt64(littleEndian) {
|
|
415
|
-
this.#
|
|
426
|
+
const offset = this.#getOffset(8 /* SizeOf.INT64 */);
|
|
427
|
+
this.#assertRead(offset);
|
|
416
428
|
const value = this.#dataView.getBigInt64(this.#offset, littleEndian);
|
|
417
|
-
this.#seek(
|
|
429
|
+
this.#seek(offset);
|
|
418
430
|
return value;
|
|
419
431
|
}
|
|
420
432
|
/**
|
|
@@ -424,9 +436,10 @@ class Buffer {
|
|
|
424
436
|
* @returns {bigint} 介于 0 和 18446744073709551615 之间的 64 位无符号整数
|
|
425
437
|
*/
|
|
426
438
|
readUint64(littleEndian) {
|
|
427
|
-
this.#
|
|
439
|
+
const offset = this.#getOffset(8 /* SizeOf.UINT64 */);
|
|
440
|
+
this.#assertRead(offset);
|
|
428
441
|
const value = this.#dataView.getBigUint64(this.#offset, littleEndian);
|
|
429
|
-
this.#seek(
|
|
442
|
+
this.#seek(offset);
|
|
430
443
|
return value;
|
|
431
444
|
}
|
|
432
445
|
/**
|
|
@@ -436,9 +449,10 @@ class Buffer {
|
|
|
436
449
|
* @returns {number} 单精度 32 位浮点数
|
|
437
450
|
*/
|
|
438
451
|
readFloat32(littleEndian) {
|
|
439
|
-
this.#
|
|
452
|
+
const offset = this.#getOffset(4 /* SizeOf.FLOAT32 */);
|
|
453
|
+
this.#assertRead(offset);
|
|
440
454
|
const value = this.#dataView.getFloat32(this.#offset, littleEndian);
|
|
441
|
-
this.#seek(
|
|
455
|
+
this.#seek(offset);
|
|
442
456
|
return value;
|
|
443
457
|
}
|
|
444
458
|
/**
|
|
@@ -448,16 +462,20 @@ class Buffer {
|
|
|
448
462
|
* @returns {number} 双精度 64 位浮点数
|
|
449
463
|
*/
|
|
450
464
|
readFloat64(littleEndian) {
|
|
451
|
-
this.#
|
|
465
|
+
const offset = this.#getOffset(8 /* SizeOf.FLOAT64 */);
|
|
466
|
+
this.#assertRead(offset);
|
|
452
467
|
const value = this.#dataView.getFloat64(this.#offset, littleEndian);
|
|
453
|
-
this.#seek(
|
|
468
|
+
this.#seek(offset);
|
|
454
469
|
return value;
|
|
455
470
|
}
|
|
456
471
|
read(length, encoding) {
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
this.#
|
|
472
|
+
if (!isNaturalNumber(length)) {
|
|
473
|
+
throw new RangeError(readLengthInvalid);
|
|
474
|
+
}
|
|
475
|
+
const offset = this.#getOffset(length);
|
|
476
|
+
this.#assertRead(offset);
|
|
477
|
+
const bytes = this.#bytes.slice(this.#offset, offset);
|
|
478
|
+
this.#seek(offset);
|
|
461
479
|
if (arguments.length >= 2) {
|
|
462
480
|
return decode(bytes, encoding);
|
|
463
481
|
}
|
package/esm/utils.d.ts
CHANGED
|
@@ -2,10 +2,17 @@
|
|
|
2
2
|
* @module utils
|
|
3
3
|
*/
|
|
4
4
|
/**
|
|
5
|
-
* @function
|
|
6
|
-
* @description
|
|
7
|
-
* @param
|
|
5
|
+
* @function isNaturalNumber
|
|
6
|
+
* @description 判断是否为自然数
|
|
7
|
+
* @param value 待判断的值
|
|
8
|
+
* @returns {boolean}
|
|
9
|
+
*/
|
|
10
|
+
export declare function isNaturalNumber(value: number): boolean;
|
|
11
|
+
/**
|
|
12
|
+
* @function makeUint8Array
|
|
13
|
+
* @description 创建一个合适长度的 Uint8Array
|
|
14
|
+
* @param {number} byteLength 数据字节总大小
|
|
8
15
|
* @param {number} pageSize 缓冲区页大小
|
|
9
|
-
* @returns {
|
|
16
|
+
* @returns {Uint8Array}
|
|
10
17
|
*/
|
|
11
|
-
export declare function
|
|
18
|
+
export declare function makeUint8Array(byteLength: number, pageSize: number): Uint8Array;
|