extended-buffer 6.1.0 → 7.0.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.
@@ -2,98 +2,116 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ExtendedBuffer = void 0;
4
4
  const buffer_1 = require("buffer");
5
+ const utils = require("./utils");
6
+ const errors_1 = require("./errors");
7
+ const defaultCapacity = 512 * 1024;
8
+ const defaultCapacityStep = defaultCapacity;
5
9
  class ExtendedBuffer {
6
10
  constructor(options) {
7
- var _a;
8
- this._maxBufferLength = (_a = options === null || options === void 0 ? void 0 : options.maxBufferLength) !== null && _a !== void 0 ? _a : (1024 * 1024);
9
- this._initEmptyBuffer();
10
- }
11
- static get maxSize() {
12
- return buffer_1.kMaxLength;
13
- }
14
- static concat(list, totalLength) {
15
- let buffer = new this;
16
- let listLength = list.length;
17
- for (let i = 0; i < listLength; ++i) {
18
- buffer.writeBuffer(list[i], false);
19
- if (undefined !== totalLength && buffer.length >= totalLength) {
20
- buffer._pointerEnd = buffer._pointerStart + totalLength;
21
- break;
22
- }
23
- }
24
- return buffer;
25
- }
26
- static zigZagEncode32(value) {
27
- return (((value |= 0) << 1) ^ (value >> 31)) >>> 0;
28
- }
29
- static zigZagDecode32(value) {
30
- return ((value >>> 1) ^ -(value & 1)) | 0;
11
+ var _a, _b;
12
+ this._capacity = (_a = options === null || options === void 0 ? void 0 : options.capacity) !== null && _a !== void 0 ? _a : defaultCapacity;
13
+ this._capacityStep = (_b = options === null || options === void 0 ? void 0 : options.capacityStep) !== null && _b !== void 0 ? _b : defaultCapacityStep;
14
+ utils.assertUnsignedInteger(this._capacity);
15
+ utils.assertUnsignedInteger(this._capacityStep);
16
+ this.initExtendedBuffer();
17
+ }
18
+ createInstance(options) {
19
+ const ThisClass = this.constructor;
20
+ return new ThisClass(options);
31
21
  }
32
22
  get length() {
33
23
  return this._pointerEnd - this._pointerStart;
34
24
  }
35
- get nativeLength() {
25
+ get capacity() {
36
26
  return this._nativeBuffer.length;
37
27
  }
38
- get buffer() {
39
- return this._nativeBuffer.slice(this._pointerStart, this._pointerEnd);
28
+ get pointer() {
29
+ return this.getPointer();
40
30
  }
41
- _initEmptyBuffer() {
42
- if (this._maxBufferLength > buffer_1.kMaxLength) {
43
- throw new RangeError(`"_maxBufferLength" cannot be more than ${buffer_1.kMaxLength} bytes`);
44
- }
45
- this._pointerStart = this._pointerEnd = Math.floor(this._maxBufferLength / 2);
46
- this._nativeBuffer = buffer_1.Buffer.allocUnsafe ? buffer_1.Buffer.allocUnsafe(this._maxBufferLength) : new buffer_1.Buffer(this._maxBufferLength);
31
+ get nativeBufferView() {
32
+ return utils.nativeBufferSubarray(this._nativeBuffer, this._pointerStart, this._pointerEnd);
33
+ }
34
+ initExtendedBuffer() {
47
35
  this._pointer = 0;
36
+ this._nativeBuffer = utils.allocNativeBuffer(this._capacity);
37
+ this._pointerStart = this._pointerEnd = Math.floor(this._capacity / 2);
38
+ this.assertInstanceState();
39
+ return this;
40
+ }
41
+ assertInstanceState() {
42
+ if (!this._nativeBuffer ||
43
+ !Number.isSafeInteger(this._pointer) ||
44
+ !Number.isSafeInteger(this._pointerStart) ||
45
+ !Number.isSafeInteger(this._pointerEnd) ||
46
+ !Number.isSafeInteger(this._capacity) ||
47
+ !Number.isSafeInteger(this._capacityStep) ||
48
+ this._pointer < 0 ||
49
+ this._pointerStart < 0 ||
50
+ this._pointerEnd < 0 ||
51
+ this._capacity < 0 ||
52
+ this._capacityStep < 0 ||
53
+ this._pointerStart > this._pointerEnd ||
54
+ this._pointerEnd > this._nativeBuffer.length ||
55
+ this._pointer > (this._pointerEnd - this._pointerStart)) {
56
+ throw new errors_1.ExtendedBufferError('INVALID_INSTANCE_STATE');
57
+ }
48
58
  return this;
49
59
  }
50
60
  clean() {
51
- return this._initEmptyBuffer();
61
+ return this.initExtendedBuffer();
52
62
  }
53
- getFreeSpaceStart() {
63
+ nativePointer() {
64
+ return this._pointerStart + this._pointer;
65
+ }
66
+ getWritableSizeStart() {
54
67
  return this._pointerStart;
55
68
  }
56
- getFreeSpaceEnd() {
69
+ getWritableSizeEnd() {
57
70
  return this._nativeBuffer.length - this._pointerEnd;
58
71
  }
59
- getFreeSpace() {
60
- return this.getFreeSpaceStart() + this.getFreeSpaceEnd();
72
+ getWritableSize() {
73
+ return this.getWritableSizeStart() + this.getWritableSizeEnd();
61
74
  }
62
- allocStart(byteLength) {
63
- if (byteLength > this.getFreeSpaceStart()) {
64
- if (byteLength > this.getFreeSpace()) {
65
- throw new RangeError('Not enough free space');
66
- }
67
- const offset = Math.floor((this.getFreeSpace() - byteLength) / 2) + byteLength - this._pointerStart;
68
- this._nativeBuffer.copy(this._nativeBuffer, this._pointerStart + offset, this._pointerStart, this._pointerEnd);
69
- this._pointerStart += offset;
70
- this._pointerEnd += offset;
75
+ allocStart(size) {
76
+ utils.assertUnsignedInteger(size);
77
+ if (this.getWritableSizeStart() >= size) {
78
+ return this;
79
+ }
80
+ if (this.getWritableSize() < size) {
81
+ const newSize = this._nativeBuffer.length + (size - this.getWritableSize()) + this._capacityStep;
82
+ this._nativeBuffer = utils.reallocNativeBuffer(this._nativeBuffer, newSize);
71
83
  }
84
+ const offset = Math.floor((this.getWritableSize() - size) / 2) + size - this._pointerStart;
85
+ this._nativeBuffer.copy(this._nativeBuffer, this._pointerStart + offset, this._pointerStart, this._pointerEnd);
86
+ this._pointerStart += offset;
87
+ this._pointerEnd += offset;
88
+ this.assertInstanceState();
72
89
  return this;
73
90
  }
74
- allocEnd(byteLength) {
75
- if (byteLength > this.getFreeSpaceEnd()) {
76
- if (byteLength > this.getFreeSpace()) {
77
- throw new RangeError('Not enough free space');
78
- }
79
- let offset = this._nativeBuffer.length - Math.floor((this.getFreeSpace() - byteLength) / 2) - byteLength - this._pointerEnd;
80
- this._nativeBuffer.copy(this._nativeBuffer, this._pointerStart + offset, this._pointerStart, this._pointerEnd);
81
- this._pointerStart += offset;
82
- this._pointerEnd += offset;
91
+ allocEnd(size) {
92
+ utils.assertUnsignedInteger(size);
93
+ if (this.getWritableSizeEnd() >= size) {
94
+ return this;
83
95
  }
96
+ if (this.getWritableSize() < size) {
97
+ const newSize = this._nativeBuffer.length + (size - this.getWritableSize()) + this._capacityStep;
98
+ this._nativeBuffer = utils.reallocNativeBuffer(this._nativeBuffer, newSize);
99
+ }
100
+ const offset = this._nativeBuffer.length - Math.floor((this.getWritableSize() - size) / 2) - size - this._pointerEnd;
101
+ this._nativeBuffer.copy(this._nativeBuffer, this._pointerStart + offset, this._pointerStart, this._pointerEnd);
102
+ this._pointerStart += offset;
103
+ this._pointerEnd += offset;
104
+ this.assertInstanceState();
84
105
  return this;
85
106
  }
86
107
  getReadableSize() {
87
108
  return this._pointerEnd - this._pointerStart - this._pointer;
88
109
  }
89
- getWritableSize() {
90
- return this.getFreeSpace();
91
- }
92
- _writeNativeBuffer(buffer, unshift) {
110
+ writeNativeBuffer(buffer, unshift) {
93
111
  if (unshift) {
94
112
  this.allocStart(buffer.length);
113
+ buffer.copy(this._nativeBuffer, this._pointerStart - buffer.length);
95
114
  this._pointerStart -= buffer.length;
96
- buffer.copy(this._nativeBuffer, this._pointerStart);
97
115
  }
98
116
  else {
99
117
  this.allocEnd(buffer.length);
@@ -103,19 +121,33 @@ class ExtendedBuffer {
103
121
  return this;
104
122
  }
105
123
  gc() {
124
+ this.discardReadData();
125
+ const freeSize = this.getWritableSize();
126
+ if (freeSize > this._capacityStep) {
127
+ const reduceSize = freeSize - this._capacityStep;
128
+ const newNativeSize = this._nativeBuffer.length - reduceSize;
129
+ this.allocEnd(reduceSize);
130
+ this._nativeBuffer = utils.reallocNativeBuffer(this._nativeBuffer, newNativeSize);
131
+ }
132
+ return this;
133
+ }
134
+ discardReadData() {
106
135
  if (this._pointer > 0) {
107
- let payload = this._nativeBuffer.slice(this._pointerStart + this._pointer, this._pointerEnd);
108
- return this._initEmptyBuffer()._writeNativeBuffer(payload, false);
136
+ this._pointerStart += this._pointer;
137
+ this._pointer = 0;
109
138
  }
110
139
  return this;
111
140
  }
112
141
  nodeGc() {
113
- global.gc && global.gc();
142
+ if (typeof global.gc === 'function') {
143
+ global.gc();
144
+ }
114
145
  return this;
115
146
  }
116
147
  setPointer(pointer) {
148
+ utils.assertInteger(pointer);
117
149
  if (pointer < 0 || pointer > this.length) {
118
- throw new RangeError('Pointer out of range');
150
+ throw new errors_1.ExtendedBufferRangeError('POINTER_OUT_OF_RANGE');
119
151
  }
120
152
  this._pointer = pointer;
121
153
  return this;
@@ -124,304 +156,303 @@ class ExtendedBuffer {
124
156
  return this._pointer;
125
157
  }
126
158
  offset(offset) {
159
+ utils.assertInteger(offset);
127
160
  return this.setPointer(this._pointer + offset);
128
161
  }
129
- isReadable(byteLength) {
130
- byteLength = byteLength < 0 ? 0 : byteLength;
131
- return this.getReadableSize() >= byteLength;
132
- }
133
- isWritable(byteLength = 1) {
134
- byteLength = byteLength < 1 ? 1 : byteLength;
135
- return this.getFreeSpace() >= byteLength;
136
- }
137
- toString(encoding, start, end) {
138
- return this.buffer.toString(encoding, start, end);
162
+ isReadable(size) {
163
+ utils.assertUnsignedInteger(size);
164
+ return this.getReadableSize() >= size;
139
165
  }
140
166
  writeBuffer(value, unshift) {
141
167
  if (value instanceof buffer_1.Buffer) {
142
- return this._writeNativeBuffer(value, unshift);
168
+ return this.writeNativeBuffer(value, unshift);
143
169
  }
144
- if (value instanceof ExtendedBuffer) {
145
- return this._writeNativeBuffer(value.buffer, unshift);
146
- }
147
- throw new TypeError('"value" is incorrect buffer');
170
+ return this.writeNativeBuffer(value.nativeBufferView, unshift);
148
171
  }
149
172
  writeString(string, encoding, unshift) {
150
- let byteLength = buffer_1.Buffer.byteLength(string, encoding);
151
- if (unshift) {
152
- this.allocStart(byteLength);
153
- this._pointerStart -= byteLength;
154
- this._nativeBuffer.write(string, this._pointerStart, byteLength, encoding);
155
- }
156
- else {
157
- this.allocEnd(byteLength);
158
- this._nativeBuffer.write(string, this._pointerEnd, byteLength, encoding);
159
- this._pointerEnd += byteLength;
160
- }
161
- return this;
173
+ const bytes = buffer_1.Buffer.from(string, encoding);
174
+ return this.writeNativeBuffer(bytes, unshift);
162
175
  }
163
- writeIntBE(value, byteLength, unshift, noAssert) {
176
+ writeIntBE(value, size, unshift) {
177
+ utils.assertInteger(value);
178
+ utils.assertUnsignedInteger(size);
179
+ utils.assertIntegerSize(size);
164
180
  if (unshift) {
165
- this.allocStart(byteLength);
166
- this._pointerStart -= byteLength;
167
- this._nativeBuffer.writeIntBE(value, this._pointerStart, byteLength, noAssert);
181
+ this.allocStart(size);
182
+ this._nativeBuffer.writeIntBE(value, this._pointerStart - size, size);
183
+ this._pointerStart -= size;
168
184
  }
169
185
  else {
170
- this.allocEnd(byteLength);
171
- this._nativeBuffer.writeIntBE(value, this._pointerEnd, byteLength, noAssert);
172
- this._pointerEnd += byteLength;
186
+ this.allocEnd(size);
187
+ this._nativeBuffer.writeIntBE(value, this._pointerEnd, size);
188
+ this._pointerEnd += size;
173
189
  }
174
190
  return this;
175
191
  }
176
- writeIntLE(value, byteLength, unshift, noAssert) {
192
+ writeIntLE(value, size, unshift) {
193
+ utils.assertInteger(value);
194
+ utils.assertUnsignedInteger(size);
195
+ utils.assertIntegerSize(size);
177
196
  if (unshift) {
178
- this.allocStart(byteLength);
179
- this._pointerStart -= byteLength;
180
- this._nativeBuffer.writeIntLE(value, this._pointerStart, byteLength, noAssert);
197
+ this.allocStart(size);
198
+ this._nativeBuffer.writeIntLE(value, this._pointerStart - size, size);
199
+ this._pointerStart -= size;
181
200
  }
182
201
  else {
183
- this.allocEnd(byteLength);
184
- this._nativeBuffer.writeIntLE(value, this._pointerEnd, byteLength, noAssert);
185
- this._pointerEnd += byteLength;
202
+ this.allocEnd(size);
203
+ this._nativeBuffer.writeIntLE(value, this._pointerEnd, size);
204
+ this._pointerEnd += size;
186
205
  }
187
206
  return this;
188
207
  }
189
- writeUIntBE(value, byteLength, unshift, noAssert) {
208
+ writeUIntBE(value, size, unshift) {
209
+ utils.assertUnsignedInteger(value);
210
+ utils.assertUnsignedInteger(size);
211
+ utils.assertIntegerSize(size);
190
212
  if (unshift) {
191
- this.allocStart(byteLength);
192
- this._pointerStart -= byteLength;
193
- this._nativeBuffer.writeUIntBE(value, this._pointerStart, byteLength, noAssert);
213
+ this.allocStart(size);
214
+ this._nativeBuffer.writeUIntBE(value, this._pointerStart - size, size);
215
+ this._pointerStart -= size;
194
216
  }
195
217
  else {
196
- this.allocEnd(byteLength);
197
- this._nativeBuffer.writeUIntBE(value, this._pointerEnd, byteLength, noAssert);
198
- this._pointerEnd += byteLength;
218
+ this.allocEnd(size);
219
+ this._nativeBuffer.writeUIntBE(value, this._pointerEnd, size);
220
+ this._pointerEnd += size;
199
221
  }
200
222
  return this;
201
223
  }
202
- writeUIntLE(value, byteLength, unshift, noAssert) {
224
+ writeUIntLE(value, size, unshift) {
225
+ utils.assertUnsignedInteger(value);
226
+ utils.assertUnsignedInteger(size);
227
+ utils.assertIntegerSize(size);
203
228
  if (unshift) {
204
- this.allocStart(byteLength);
205
- this._pointerStart -= byteLength;
206
- this._nativeBuffer.writeUIntLE(value, this._pointerStart, byteLength, noAssert);
229
+ this.allocStart(size);
230
+ this._nativeBuffer.writeUIntLE(value, this._pointerStart - size, size);
231
+ this._pointerStart -= size;
207
232
  }
208
233
  else {
209
- this.allocEnd(byteLength);
210
- this._nativeBuffer.writeUIntLE(value, this._pointerEnd, byteLength, noAssert);
211
- this._pointerEnd += byteLength;
234
+ this.allocEnd(size);
235
+ this._nativeBuffer.writeUIntLE(value, this._pointerEnd, size);
236
+ this._pointerEnd += size;
212
237
  }
213
238
  return this;
214
239
  }
215
- writeInt8(value, unshift, noAssert) {
216
- return this.writeIntBE(value, 1, unshift, noAssert);
240
+ writeInt8(value, unshift) {
241
+ return this.writeIntBE(value, 1, unshift);
217
242
  }
218
- writeUInt8(value, unshift, noAssert) {
219
- return this.writeUIntBE(value, 1, unshift, noAssert);
243
+ writeUInt8(value, unshift) {
244
+ return this.writeUIntLE(value, 1, unshift);
220
245
  }
221
- writeInt16BE(value, unshift, noAssert) {
222
- return this.writeIntBE(value, 2, unshift, noAssert);
246
+ writeInt16BE(value, unshift) {
247
+ return this.writeIntBE(value, 2, unshift);
223
248
  }
224
- writeInt16LE(value, unshift, noAssert) {
225
- return this.writeIntLE(value, 2, unshift, noAssert);
249
+ writeInt16LE(value, unshift) {
250
+ return this.writeIntLE(value, 2, unshift);
226
251
  }
227
- writeUInt16BE(value, unshift, noAssert) {
228
- return this.writeUIntBE(value, 2, unshift, noAssert);
252
+ writeUInt16BE(value, unshift) {
253
+ return this.writeUIntBE(value, 2, unshift);
229
254
  }
230
- writeUInt16LE(value, unshift, noAssert) {
231
- return this.writeUIntLE(value, 2, unshift, noAssert);
255
+ writeUInt16LE(value, unshift) {
256
+ return this.writeUIntLE(value, 2, unshift);
232
257
  }
233
- writeInt32BE(value, unshift, noAssert) {
234
- return this.writeIntBE(value, 4, unshift, noAssert);
258
+ writeInt32BE(value, unshift) {
259
+ return this.writeIntBE(value, 4, unshift);
235
260
  }
236
- writeInt32LE(value, unshift, noAssert) {
237
- return this.writeIntLE(value, 4, unshift, noAssert);
261
+ writeInt32LE(value, unshift) {
262
+ return this.writeIntLE(value, 4, unshift);
238
263
  }
239
- writeUInt32BE(value, unshift, noAssert) {
240
- return this.writeUIntBE(value, 4, unshift, noAssert);
264
+ writeUInt32BE(value, unshift) {
265
+ return this.writeUIntBE(value, 4, unshift);
241
266
  }
242
- writeUInt32LE(value, unshift, noAssert) {
243
- return this.writeUIntLE(value, 4, unshift, noAssert);
267
+ writeUInt32LE(value, unshift) {
268
+ return this.writeUIntLE(value, 4, unshift);
244
269
  }
245
- writeFloatBE(value, unshift, noAssert) {
270
+ writeFloatBE(value, unshift) {
246
271
  if (unshift) {
247
272
  this.allocStart(4);
273
+ this._nativeBuffer.writeFloatBE(value, this._pointerStart - 4);
248
274
  this._pointerStart -= 4;
249
- this._nativeBuffer.writeFloatBE(value, this._pointerStart, noAssert);
250
275
  }
251
276
  else {
252
277
  this.allocEnd(4);
253
- this._nativeBuffer.writeFloatBE(value, this._pointerEnd, noAssert);
278
+ this._nativeBuffer.writeFloatBE(value, this._pointerEnd);
254
279
  this._pointerEnd += 4;
255
280
  }
256
281
  return this;
257
282
  }
258
- writeFloatLE(value, unshift, noAssert) {
283
+ writeFloatLE(value, unshift) {
259
284
  if (unshift) {
260
285
  this.allocStart(4);
286
+ this._nativeBuffer.writeFloatLE(value, this._pointerStart - 4);
261
287
  this._pointerStart -= 4;
262
- this._nativeBuffer.writeFloatLE(value, this._pointerStart, noAssert);
263
288
  }
264
289
  else {
265
290
  this.allocEnd(4);
266
- this._nativeBuffer.writeFloatLE(value, this._pointerEnd, noAssert);
291
+ this._nativeBuffer.writeFloatLE(value, this._pointerEnd);
267
292
  this._pointerEnd += 4;
268
293
  }
269
294
  return this;
270
295
  }
271
- writeDoubleBE(value, unshift, noAssert) {
296
+ writeDoubleBE(value, unshift) {
272
297
  if (unshift) {
273
298
  this.allocStart(8);
299
+ this._nativeBuffer.writeDoubleBE(value, this._pointerStart - 8);
274
300
  this._pointerStart -= 8;
275
- this._nativeBuffer.writeDoubleBE(value, this._pointerStart, noAssert);
276
301
  }
277
302
  else {
278
303
  this.allocEnd(8);
279
- this._nativeBuffer.writeDoubleBE(value, this._pointerEnd, noAssert);
304
+ this._nativeBuffer.writeDoubleBE(value, this._pointerEnd);
280
305
  this._pointerEnd += 8;
281
306
  }
282
307
  return this;
283
308
  }
284
- writeDoubleLE(value, unshift, noAssert) {
309
+ writeDoubleLE(value, unshift) {
285
310
  if (unshift) {
286
311
  this.allocStart(8);
312
+ this._nativeBuffer.writeDoubleLE(value, this._pointerStart - 8);
287
313
  this._pointerStart -= 8;
288
- this._nativeBuffer.writeDoubleLE(value, this._pointerStart, noAssert);
289
314
  }
290
315
  else {
291
316
  this.allocEnd(8);
292
- this._nativeBuffer.writeDoubleLE(value, this._pointerEnd, noAssert);
317
+ this._nativeBuffer.writeDoubleLE(value, this._pointerEnd);
293
318
  this._pointerEnd += 8;
294
319
  }
295
320
  return this;
296
321
  }
297
- writeVarInt32(value, unshift) {
298
- value >>>= 0;
299
- let b;
300
- if (unshift) {
301
- let buffer = new ExtendedBuffer({
302
- maxBufferLength: 10
303
- });
304
- while (value >= 0x80) {
305
- b = (value & 0x7f) | 0x80;
306
- buffer.writeUIntBE(b, 1);
307
- value >>>= 7;
308
- }
309
- buffer.writeUIntBE(value, 1);
310
- return this._writeNativeBuffer(buffer.buffer, true);
311
- }
312
- while (value >= 0x80) {
313
- b = (value & 0x7f) | 0x80;
314
- this.writeUIntBE(b, 1);
315
- value >>>= 7;
316
- }
317
- return this.writeUIntBE(value, 1);
318
- }
319
322
  readBuffer(size, asNative, bufferOptions) {
320
- let buffer = this._nativeBuffer.slice(this._pointerStart + this._pointer, this._pointerStart + this._pointer + size);
321
- this._pointer += size;
322
- const ThisClass = this.constructor;
323
+ utils.assertUnsignedInteger(size);
324
+ if (!this.isReadable(size)) {
325
+ throw new errors_1.ExtendedBufferRangeError('SIZE_OUT_OF_RANGE');
326
+ }
327
+ let result;
328
+ const buffer = utils.nativeBufferSubarray(this._nativeBuffer, this.nativePointer(), this.nativePointer() + size);
323
329
  if (asNative) {
324
- return buffer_1.Buffer.from ? buffer_1.Buffer.from(buffer) : new buffer_1.Buffer(buffer);
330
+ result = buffer_1.Buffer.from(buffer);
331
+ }
332
+ else {
333
+ bufferOptions = Object.assign({
334
+ capacity: this._capacity,
335
+ capacityStep: this._capacityStep
336
+ }, bufferOptions !== null && bufferOptions !== void 0 ? bufferOptions : {});
337
+ result = this.createInstance(bufferOptions).writeNativeBuffer(buffer);
325
338
  }
326
- return (new ThisClass(bufferOptions))._writeNativeBuffer(buffer, false);
339
+ this.offset(size);
340
+ return result;
327
341
  }
328
342
  readString(size, encoding) {
329
- this._pointer += size;
330
- return this._nativeBuffer.toString(encoding, this._pointerStart + this._pointer - size, this._pointerStart + this._pointer);
331
- }
332
- readIntBE(byteLength, noAssert) {
333
- this._pointer += byteLength;
334
- return this._nativeBuffer.readIntBE(this._pointerStart + this._pointer - byteLength, byteLength, noAssert);
335
- }
336
- readIntLE(byteLength, noAssert) {
337
- this._pointer += byteLength;
338
- return this._nativeBuffer.readIntLE(this._pointerStart + this._pointer - byteLength, byteLength, noAssert);
339
- }
340
- readUIntBE(byteLength, noAssert) {
341
- this._pointer += byteLength;
342
- return this._nativeBuffer.readUIntBE(this._pointerStart + this._pointer - byteLength, byteLength, noAssert);
343
- }
344
- readUIntLE(byteLength, noAssert) {
345
- this._pointer += byteLength;
346
- return this._nativeBuffer.readUIntLE(this._pointerStart + this._pointer - byteLength, byteLength, noAssert);
347
- }
348
- readInt8(noAssert) {
349
- return this.readIntBE(1, noAssert);
350
- }
351
- readUInt8(noAssert) {
352
- return this.readUIntBE(1, noAssert);
353
- }
354
- readInt16BE(noAssert) {
355
- return this.readIntBE(2, noAssert);
356
- }
357
- readInt16LE(noAssert) {
358
- return this.readIntLE(2, noAssert);
359
- }
360
- readUInt16BE(noAssert) {
361
- return this.readUIntBE(2, noAssert);
362
- }
363
- readUInt16LE(noAssert) {
364
- return this.readUIntLE(2, noAssert);
365
- }
366
- readInt32BE(noAssert) {
367
- return this.readIntBE(4, noAssert);
368
- }
369
- readInt32LE(noAssert) {
370
- return this.readIntLE(4, noAssert);
371
- }
372
- readUInt32BE(noAssert) {
373
- return this.readUIntBE(4, noAssert);
374
- }
375
- readUInt32LE(noAssert) {
376
- return this.readUIntLE(4, noAssert);
377
- }
378
- readFloatBE(noAssert) {
379
- this._pointer += 4;
380
- return this._nativeBuffer.readFloatBE(this._pointerStart + this._pointer - 4, noAssert);
381
- }
382
- readFloatLE(noAssert) {
383
- this._pointer += 4;
384
- return this._nativeBuffer.readFloatLE(this._pointerStart + this._pointer - 4, noAssert);
385
- }
386
- readDoubleBE(noAssert) {
387
- this._pointer += 8;
388
- return this._nativeBuffer.readDoubleBE(this._pointerStart + this._pointer - 8, noAssert);
389
- }
390
- readDoubleLE(noAssert) {
391
- this._pointer += 8;
392
- return this._nativeBuffer.readDoubleLE(this._pointerStart + this._pointer - 8, noAssert);
393
- }
394
- readVarInt32() {
395
- let c = 0;
396
- let value = 0 >>> 0;
397
- let b;
398
- do {
399
- b = this.readUIntBE(1);
400
- if (c < 5) {
401
- value |= (b & 0x7f) << (7 * c);
402
- }
403
- ++c;
404
- } while ((b & 0x80) !== 0);
405
- return value | 0;
406
- }
407
- isReadableVarInt32() {
408
- let c = 0;
409
- let value = 0 >>> 0;
410
- let b;
411
- let oldPointer = this._pointer;
412
- do {
413
- if (!this.isReadable(1)) {
414
- this._pointer = oldPointer;
415
- return false;
416
- }
417
- b = this.readUIntBE(1);
418
- if (c < 5) {
419
- value |= (b & 0x7f) << (7 * c);
420
- }
421
- ++c;
422
- } while ((b & 0x80) !== 0);
423
- this._pointer = oldPointer;
424
- return true;
343
+ utils.assertUnsignedInteger(size);
344
+ if (!this.isReadable(size)) {
345
+ throw new errors_1.ExtendedBufferRangeError('SIZE_OUT_OF_RANGE');
346
+ }
347
+ const result = this._nativeBuffer.toString(encoding, this.nativePointer(), this.nativePointer() + size);
348
+ this.offset(size);
349
+ return result;
350
+ }
351
+ readIntBE(size) {
352
+ utils.assertUnsignedInteger(size);
353
+ utils.assertIntegerSize(size);
354
+ if (!this.isReadable(size)) {
355
+ throw new errors_1.ExtendedBufferRangeError('SIZE_OUT_OF_RANGE');
356
+ }
357
+ const result = this._nativeBuffer.readIntBE(this.nativePointer(), size);
358
+ this.offset(size);
359
+ return result;
360
+ }
361
+ readIntLE(size) {
362
+ utils.assertUnsignedInteger(size);
363
+ utils.assertIntegerSize(size);
364
+ if (!this.isReadable(size)) {
365
+ throw new errors_1.ExtendedBufferRangeError('SIZE_OUT_OF_RANGE');
366
+ }
367
+ const result = this._nativeBuffer.readIntLE(this.nativePointer(), size);
368
+ this.offset(size);
369
+ return result;
370
+ }
371
+ readUIntBE(size) {
372
+ utils.assertUnsignedInteger(size);
373
+ utils.assertIntegerSize(size);
374
+ if (!this.isReadable(size)) {
375
+ throw new errors_1.ExtendedBufferRangeError('SIZE_OUT_OF_RANGE');
376
+ }
377
+ const result = this._nativeBuffer.readUIntBE(this.nativePointer(), size);
378
+ this.offset(size);
379
+ return result;
380
+ }
381
+ readUIntLE(size) {
382
+ utils.assertUnsignedInteger(size);
383
+ utils.assertIntegerSize(size);
384
+ if (!this.isReadable(size)) {
385
+ throw new errors_1.ExtendedBufferRangeError('SIZE_OUT_OF_RANGE');
386
+ }
387
+ const result = this._nativeBuffer.readUIntLE(this.nativePointer(), size);
388
+ this.offset(size);
389
+ return result;
390
+ }
391
+ readInt8() {
392
+ return this.readIntBE(1);
393
+ }
394
+ readUInt8() {
395
+ return this.readUIntLE(1);
396
+ }
397
+ readInt16BE() {
398
+ return this.readIntBE(2);
399
+ }
400
+ readInt16LE() {
401
+ return this.readIntLE(2);
402
+ }
403
+ readUInt16BE() {
404
+ return this.readUIntBE(2);
405
+ }
406
+ readUInt16LE() {
407
+ return this.readUIntLE(2);
408
+ }
409
+ readInt32BE() {
410
+ return this.readIntBE(4);
411
+ }
412
+ readInt32LE() {
413
+ return this.readIntLE(4);
414
+ }
415
+ readUInt32BE() {
416
+ return this.readUIntBE(4);
417
+ }
418
+ readUInt32LE() {
419
+ return this.readUIntLE(4);
420
+ }
421
+ readFloatBE() {
422
+ const size = 4;
423
+ if (!this.isReadable(size)) {
424
+ throw new errors_1.ExtendedBufferRangeError('SIZE_OUT_OF_RANGE');
425
+ }
426
+ const result = this._nativeBuffer.readFloatBE(this.nativePointer());
427
+ this.offset(size);
428
+ return result;
429
+ }
430
+ readFloatLE() {
431
+ const size = 4;
432
+ if (!this.isReadable(size)) {
433
+ throw new errors_1.ExtendedBufferRangeError('SIZE_OUT_OF_RANGE');
434
+ }
435
+ const result = this._nativeBuffer.readFloatLE(this.nativePointer());
436
+ this.offset(size);
437
+ return result;
438
+ }
439
+ readDoubleBE() {
440
+ const size = 8;
441
+ if (!this.isReadable(size)) {
442
+ throw new errors_1.ExtendedBufferRangeError('SIZE_OUT_OF_RANGE');
443
+ }
444
+ const result = this._nativeBuffer.readDoubleBE(this.nativePointer());
445
+ this.offset(size);
446
+ return result;
447
+ }
448
+ readDoubleLE() {
449
+ const size = 8;
450
+ if (!this.isReadable(size)) {
451
+ throw new errors_1.ExtendedBufferRangeError('SIZE_OUT_OF_RANGE');
452
+ }
453
+ const result = this._nativeBuffer.readDoubleLE(this.nativePointer());
454
+ this.offset(size);
455
+ return result;
425
456
  }
426
457
  }
427
458
  exports.ExtendedBuffer = ExtendedBuffer;