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