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.
- package/README.md +273 -94
- package/dist/ExtendedBuffer.d.ts +61 -66
- package/dist/ExtendedBuffer.js +306 -275
- package/dist/ExtendedBufferOptions.d.ts +4 -3
- package/dist/errors/ExtendedBufferError.d.ts +2 -0
- package/dist/errors/ExtendedBufferError.js +6 -0
- package/dist/errors/ExtendedBufferRangeError.d.ts +3 -0
- package/dist/errors/ExtendedBufferRangeError.js +7 -0
- package/dist/errors/ExtendedBufferTypeError.d.ts +3 -0
- package/dist/errors/ExtendedBufferTypeError.js +7 -0
- package/dist/errors/index.d.ts +3 -0
- package/dist/errors/index.js +9 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.js +16 -0
- package/dist/utils/alloc-native-buffer.d.ts +2 -0
- package/dist/utils/alloc-native-buffer.js +17 -0
- package/dist/utils/assert-integer-size.d.ts +1 -0
- package/dist/utils/assert-integer-size.js +13 -0
- package/dist/utils/assert-integer.d.ts +1 -0
- package/dist/utils/assert-integer.js +10 -0
- package/dist/utils/assert-unsigned-integer.d.ts +1 -0
- package/dist/utils/assert-unsigned-integer.js +10 -0
- package/dist/utils/index.d.ts +6 -0
- package/dist/utils/index.js +15 -0
- package/dist/utils/native-buffer-subarray.d.ts +2 -0
- package/dist/utils/native-buffer-subarray.js +9 -0
- package/dist/utils/realloc-native-buffer.d.ts +2 -0
- package/dist/utils/realloc-native-buffer.js +15 -0
- package/package.json +34 -5
package/dist/ExtendedBuffer.js
CHANGED
|
@@ -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.
|
|
9
|
-
this.
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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
|
|
25
|
+
get capacity() {
|
|
36
26
|
return this._nativeBuffer.length;
|
|
37
27
|
}
|
|
38
|
-
get
|
|
39
|
-
return this.
|
|
28
|
+
get pointer() {
|
|
29
|
+
return this.getPointer();
|
|
40
30
|
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
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.
|
|
61
|
+
return this.initExtendedBuffer();
|
|
52
62
|
}
|
|
53
|
-
|
|
63
|
+
nativePointer() {
|
|
64
|
+
return this._pointerStart + this._pointer;
|
|
65
|
+
}
|
|
66
|
+
getWritableSizeStart() {
|
|
54
67
|
return this._pointerStart;
|
|
55
68
|
}
|
|
56
|
-
|
|
69
|
+
getWritableSizeEnd() {
|
|
57
70
|
return this._nativeBuffer.length - this._pointerEnd;
|
|
58
71
|
}
|
|
59
|
-
|
|
60
|
-
return this.
|
|
72
|
+
getWritableSize() {
|
|
73
|
+
return this.getWritableSizeStart() + this.getWritableSizeEnd();
|
|
61
74
|
}
|
|
62
|
-
allocStart(
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
this._nativeBuffer.
|
|
69
|
-
this.
|
|
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(
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
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
|
-
|
|
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
|
-
|
|
108
|
-
|
|
136
|
+
this._pointerStart += this._pointer;
|
|
137
|
+
this._pointer = 0;
|
|
109
138
|
}
|
|
110
139
|
return this;
|
|
111
140
|
}
|
|
112
141
|
nodeGc() {
|
|
113
|
-
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
|
|
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(
|
|
130
|
-
|
|
131
|
-
return this.getReadableSize() >=
|
|
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.
|
|
168
|
+
return this.writeNativeBuffer(value, unshift);
|
|
143
169
|
}
|
|
144
|
-
|
|
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
|
-
|
|
151
|
-
|
|
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,
|
|
176
|
+
writeIntBE(value, size, unshift) {
|
|
177
|
+
utils.assertInteger(value);
|
|
178
|
+
utils.assertUnsignedInteger(size);
|
|
179
|
+
utils.assertIntegerSize(size);
|
|
164
180
|
if (unshift) {
|
|
165
|
-
this.allocStart(
|
|
166
|
-
this._pointerStart
|
|
167
|
-
this.
|
|
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(
|
|
171
|
-
this._nativeBuffer.writeIntBE(value, this._pointerEnd,
|
|
172
|
-
this._pointerEnd +=
|
|
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,
|
|
192
|
+
writeIntLE(value, size, unshift) {
|
|
193
|
+
utils.assertInteger(value);
|
|
194
|
+
utils.assertUnsignedInteger(size);
|
|
195
|
+
utils.assertIntegerSize(size);
|
|
177
196
|
if (unshift) {
|
|
178
|
-
this.allocStart(
|
|
179
|
-
this._pointerStart
|
|
180
|
-
this.
|
|
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(
|
|
184
|
-
this._nativeBuffer.writeIntLE(value, this._pointerEnd,
|
|
185
|
-
this._pointerEnd +=
|
|
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,
|
|
208
|
+
writeUIntBE(value, size, unshift) {
|
|
209
|
+
utils.assertUnsignedInteger(value);
|
|
210
|
+
utils.assertUnsignedInteger(size);
|
|
211
|
+
utils.assertIntegerSize(size);
|
|
190
212
|
if (unshift) {
|
|
191
|
-
this.allocStart(
|
|
192
|
-
this._pointerStart
|
|
193
|
-
this.
|
|
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(
|
|
197
|
-
this._nativeBuffer.writeUIntBE(value, this._pointerEnd,
|
|
198
|
-
this._pointerEnd +=
|
|
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,
|
|
224
|
+
writeUIntLE(value, size, unshift) {
|
|
225
|
+
utils.assertUnsignedInteger(value);
|
|
226
|
+
utils.assertUnsignedInteger(size);
|
|
227
|
+
utils.assertIntegerSize(size);
|
|
203
228
|
if (unshift) {
|
|
204
|
-
this.allocStart(
|
|
205
|
-
this._pointerStart
|
|
206
|
-
this.
|
|
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(
|
|
210
|
-
this._nativeBuffer.writeUIntLE(value, this._pointerEnd,
|
|
211
|
-
this._pointerEnd +=
|
|
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
|
|
216
|
-
return this.writeIntBE(value, 1, unshift
|
|
240
|
+
writeInt8(value, unshift) {
|
|
241
|
+
return this.writeIntBE(value, 1, unshift);
|
|
217
242
|
}
|
|
218
|
-
writeUInt8(value, unshift
|
|
219
|
-
return this.
|
|
243
|
+
writeUInt8(value, unshift) {
|
|
244
|
+
return this.writeUIntLE(value, 1, unshift);
|
|
220
245
|
}
|
|
221
|
-
writeInt16BE(value, unshift
|
|
222
|
-
return this.writeIntBE(value, 2, unshift
|
|
246
|
+
writeInt16BE(value, unshift) {
|
|
247
|
+
return this.writeIntBE(value, 2, unshift);
|
|
223
248
|
}
|
|
224
|
-
writeInt16LE(value, unshift
|
|
225
|
-
return this.writeIntLE(value, 2, unshift
|
|
249
|
+
writeInt16LE(value, unshift) {
|
|
250
|
+
return this.writeIntLE(value, 2, unshift);
|
|
226
251
|
}
|
|
227
|
-
writeUInt16BE(value, unshift
|
|
228
|
-
return this.writeUIntBE(value, 2, unshift
|
|
252
|
+
writeUInt16BE(value, unshift) {
|
|
253
|
+
return this.writeUIntBE(value, 2, unshift);
|
|
229
254
|
}
|
|
230
|
-
writeUInt16LE(value, unshift
|
|
231
|
-
return this.writeUIntLE(value, 2, unshift
|
|
255
|
+
writeUInt16LE(value, unshift) {
|
|
256
|
+
return this.writeUIntLE(value, 2, unshift);
|
|
232
257
|
}
|
|
233
|
-
writeInt32BE(value, unshift
|
|
234
|
-
return this.writeIntBE(value, 4, unshift
|
|
258
|
+
writeInt32BE(value, unshift) {
|
|
259
|
+
return this.writeIntBE(value, 4, unshift);
|
|
235
260
|
}
|
|
236
|
-
writeInt32LE(value, unshift
|
|
237
|
-
return this.writeIntLE(value, 4, unshift
|
|
261
|
+
writeInt32LE(value, unshift) {
|
|
262
|
+
return this.writeIntLE(value, 4, unshift);
|
|
238
263
|
}
|
|
239
|
-
writeUInt32BE(value, unshift
|
|
240
|
-
return this.writeUIntBE(value, 4, unshift
|
|
264
|
+
writeUInt32BE(value, unshift) {
|
|
265
|
+
return this.writeUIntBE(value, 4, unshift);
|
|
241
266
|
}
|
|
242
|
-
writeUInt32LE(value, unshift
|
|
243
|
-
return this.writeUIntLE(value, 4, unshift
|
|
267
|
+
writeUInt32LE(value, unshift) {
|
|
268
|
+
return this.writeUIntLE(value, 4, unshift);
|
|
244
269
|
}
|
|
245
|
-
writeFloatBE(value, unshift
|
|
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
|
|
278
|
+
this._nativeBuffer.writeFloatBE(value, this._pointerEnd);
|
|
254
279
|
this._pointerEnd += 4;
|
|
255
280
|
}
|
|
256
281
|
return this;
|
|
257
282
|
}
|
|
258
|
-
writeFloatLE(value, unshift
|
|
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
|
|
291
|
+
this._nativeBuffer.writeFloatLE(value, this._pointerEnd);
|
|
267
292
|
this._pointerEnd += 4;
|
|
268
293
|
}
|
|
269
294
|
return this;
|
|
270
295
|
}
|
|
271
|
-
writeDoubleBE(value, unshift
|
|
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
|
|
304
|
+
this._nativeBuffer.writeDoubleBE(value, this._pointerEnd);
|
|
280
305
|
this._pointerEnd += 8;
|
|
281
306
|
}
|
|
282
307
|
return this;
|
|
283
308
|
}
|
|
284
|
-
writeDoubleLE(value, unshift
|
|
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
|
|
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
|
-
|
|
321
|
-
this.
|
|
322
|
-
|
|
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
|
-
|
|
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
|
-
|
|
339
|
+
this.offset(size);
|
|
340
|
+
return result;
|
|
327
341
|
}
|
|
328
342
|
readString(size, encoding) {
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
this.
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
return
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
return this.
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
this.
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
return
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
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;
|