@tgsnake/bytesio 2.0.1 → 2.1.0-browser.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/BytesIO.d.ts +228 -47
- package/dist/src/BytesIO.js +473 -101
- package/dist/src/deps.d.ts +4 -7
- package/dist/src/deps.js +4 -7
- package/dist/src/deps.node.d.ts +18 -0
- package/dist/src/deps.node.js +18 -0
- package/dist/src/index.d.ts +1 -1
- package/dist/src/index.js +1 -1
- package/package.json +46 -45
- package/dist/src/deps.browser.d.ts +0 -15
- package/dist/src/deps.browser.js +0 -15
package/dist/src/BytesIO.js
CHANGED
|
@@ -8,6 +8,15 @@
|
|
|
8
8
|
* it under the terms of the MIT License as published.
|
|
9
9
|
*/
|
|
10
10
|
import { Buffer } from './deps.js';
|
|
11
|
+
/**
|
|
12
|
+
* Standard seek origin flags matching POSIX and Python's `io` module.
|
|
13
|
+
*/
|
|
14
|
+
export var SeekOrigin;
|
|
15
|
+
(function (SeekOrigin) {
|
|
16
|
+
SeekOrigin[SeekOrigin["SEEK_SET"] = 0] = "SEEK_SET";
|
|
17
|
+
SeekOrigin[SeekOrigin["SEEK_CUR"] = 1] = "SEEK_CUR";
|
|
18
|
+
SeekOrigin[SeekOrigin["SEEK_END"] = 2] = "SEEK_END";
|
|
19
|
+
})(SeekOrigin || (SeekOrigin = {}));
|
|
11
20
|
/**
|
|
12
21
|
* A versatile in-memory binary stream class utilizing Node.js `Buffer`s.
|
|
13
22
|
* Mimics the behavior of Python's built-in `io.BytesIO` module, providing a stream-like
|
|
@@ -39,7 +48,7 @@ import { Buffer } from './deps.js';
|
|
|
39
48
|
* const stream = BytesIO.from('Hello, World!', 'utf-8');
|
|
40
49
|
*
|
|
41
50
|
* // Seek to position 7 (from the beginning of the buffer)
|
|
42
|
-
* stream.seek(7,
|
|
51
|
+
* stream.seek(7, BytesIO.SEEK_SET);
|
|
43
52
|
*
|
|
44
53
|
* // Read remaining bytes
|
|
45
54
|
* const msg = stream.read().toString('utf-8');
|
|
@@ -47,6 +56,18 @@ import { Buffer } from './deps.js';
|
|
|
47
56
|
* ```
|
|
48
57
|
*/
|
|
49
58
|
export class BytesIO {
|
|
59
|
+
/**
|
|
60
|
+
* Seek relative to the beginning of the stream. Offset must be >= 0.
|
|
61
|
+
*/
|
|
62
|
+
static SEEK_SET = 0;
|
|
63
|
+
/**
|
|
64
|
+
* Seek relative to the current stream position.
|
|
65
|
+
*/
|
|
66
|
+
static SEEK_CUR = 1;
|
|
67
|
+
/**
|
|
68
|
+
* Seek relative to the end of the stream.
|
|
69
|
+
*/
|
|
70
|
+
static SEEK_END = 2;
|
|
50
71
|
/**
|
|
51
72
|
* The raw internal Node.js Buffer instance where binary data is stored.
|
|
52
73
|
* @private
|
|
@@ -56,74 +77,186 @@ export class BytesIO {
|
|
|
56
77
|
* The current internal read/write pointer position (byte offset).
|
|
57
78
|
* @private
|
|
58
79
|
*/
|
|
59
|
-
|
|
80
|
+
_pos;
|
|
81
|
+
/**
|
|
82
|
+
* Indicates whether the stream has been closed.
|
|
83
|
+
* @private
|
|
84
|
+
*/
|
|
85
|
+
_closed = false;
|
|
60
86
|
/**
|
|
61
87
|
* Initializes a new instance of the `BytesIO` class.
|
|
62
88
|
*
|
|
63
89
|
* @param buffer - The initial binary buffer to populate the `BytesIO` instance with. Defaults to an empty buffer.
|
|
64
90
|
*/
|
|
65
91
|
constructor(buffer = Buffer.alloc(0)) {
|
|
66
|
-
this.
|
|
67
|
-
this._buffer = buffer
|
|
92
|
+
this._pos = 0;
|
|
93
|
+
this._buffer = Buffer.isBuffer(buffer)
|
|
94
|
+
? buffer
|
|
95
|
+
: Buffer.from(buffer.buffer, buffer.byteOffset, buffer.byteLength);
|
|
96
|
+
this._closed = false;
|
|
68
97
|
return this;
|
|
69
98
|
}
|
|
99
|
+
/**
|
|
100
|
+
* Internal helper to verify that the stream has not been closed.
|
|
101
|
+
* @private
|
|
102
|
+
*/
|
|
103
|
+
_checkClosed(msg) {
|
|
104
|
+
if (this._closed) {
|
|
105
|
+
throw new Error(msg ?? 'I/O operation on closed BytesIO instance.');
|
|
106
|
+
}
|
|
107
|
+
}
|
|
70
108
|
/**
|
|
71
109
|
* Adjusts the position of the internal read/write pointer (offset cursor).
|
|
72
110
|
*
|
|
73
111
|
* @param offset - The byte offset relative to the reference point defined by `whence`.
|
|
74
|
-
* - If `whence` is `0`, must be 0 or positive.
|
|
75
|
-
* - If `whence` is `
|
|
112
|
+
* - If `whence` is `0`, must be 0 or a positive number.
|
|
113
|
+
* - If `whence` is `1`, relative to the current position.
|
|
114
|
+
* - If `whence` is `2`, relative to the end of the buffer (supports 0 for EOF, negative offsets, and clamps underflow to 0).
|
|
76
115
|
* @param whence - The reference point for the seek operation:
|
|
77
|
-
* - `0` (default): Absolute positioning from the start of the buffer.
|
|
78
|
-
* - `1
|
|
79
|
-
* - `2
|
|
116
|
+
* - `0` (`SEEK_SET`, default): Absolute positioning from the start of the buffer.
|
|
117
|
+
* - `1` (`SEEK_CUR`): Relative positioning from the current pointer position.
|
|
118
|
+
* - `2` (`SEEK_END`): Relative positioning from the end of the buffer.
|
|
80
119
|
* @returns The new pointer position (relative to the start of the buffer).
|
|
81
120
|
* @throws {Error} If `whence` is `0` and `offset` is negative.
|
|
82
|
-
* @throws {Error} If `whence` is `2` and `offset` is non-negative.
|
|
83
|
-
* @throws {Error} If `whence` is `2` and the absolute seek location would be less than 0.
|
|
84
121
|
* @throws {Error} If `whence` is not `0`, `1`, or `2`.
|
|
85
122
|
*/
|
|
86
|
-
seek(offset, whence =
|
|
87
|
-
|
|
123
|
+
seek(offset, whence = BytesIO.SEEK_SET) {
|
|
124
|
+
this._checkClosed();
|
|
125
|
+
if (whence === BytesIO.SEEK_SET) {
|
|
88
126
|
if (offset < 0) {
|
|
89
127
|
throw new Error(`offset of BytesIO.seek must be zero or positive value when whence is 0`);
|
|
90
128
|
}
|
|
91
|
-
this.
|
|
129
|
+
this._pos = offset;
|
|
92
130
|
}
|
|
93
|
-
else if (whence ===
|
|
94
|
-
this.
|
|
131
|
+
else if (whence === BytesIO.SEEK_CUR) {
|
|
132
|
+
this._pos = Math.max(0, this._pos + offset);
|
|
95
133
|
}
|
|
96
|
-
else if (whence ===
|
|
97
|
-
|
|
98
|
-
throw new Error(`offset of BytesIO.seek must be less than zero or negative value when whence is 2`);
|
|
99
|
-
}
|
|
100
|
-
if (Buffer.byteLength(this._buffer) + offset < 0) {
|
|
101
|
-
throw new Error(`offset out of range, offset ${offset} is less than the available buffer length.`);
|
|
102
|
-
}
|
|
103
|
-
this._post = Buffer.byteLength(this._buffer) + offset;
|
|
134
|
+
else if (whence === BytesIO.SEEK_END) {
|
|
135
|
+
this._pos = Math.max(0, this._buffer.length + offset);
|
|
104
136
|
}
|
|
105
137
|
else {
|
|
106
|
-
throw new Error(`whence must be 0 or 1 or 2, but
|
|
138
|
+
throw new Error(`whence must be 0 or 1 or 2, but received ${whence}`);
|
|
139
|
+
}
|
|
140
|
+
return this._pos;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Returns the current stream position (cursor offset).
|
|
144
|
+
* Parity with Python's `BytesIO.tell()`.
|
|
145
|
+
*
|
|
146
|
+
* @returns Current byte offset.
|
|
147
|
+
*/
|
|
148
|
+
tell() {
|
|
149
|
+
this._checkClosed();
|
|
150
|
+
return this._pos;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Flushes the stream buffers (no-op for in-memory streams, provided for stream parity).
|
|
154
|
+
*/
|
|
155
|
+
flush() {
|
|
156
|
+
this._checkClosed();
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Returns whether this stream is readable.
|
|
160
|
+
*/
|
|
161
|
+
readable() {
|
|
162
|
+
return !this._closed;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Returns whether this stream is writable.
|
|
166
|
+
*/
|
|
167
|
+
writable() {
|
|
168
|
+
return !this._closed;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Returns whether this stream supports random access / seeking.
|
|
172
|
+
*/
|
|
173
|
+
seekable() {
|
|
174
|
+
return !this._closed;
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Flushes and closes the stream. Once closed, any further read, write, or seek operations will throw an error.
|
|
178
|
+
*/
|
|
179
|
+
close() {
|
|
180
|
+
if (!this._closed) {
|
|
181
|
+
this._closed = true;
|
|
182
|
+
this._buffer = Buffer.alloc(0);
|
|
183
|
+
this._pos = 0;
|
|
107
184
|
}
|
|
108
|
-
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Returns a copy of the entire buffer contents.
|
|
188
|
+
* Parity with Python's `BytesIO.getvalue()`.
|
|
189
|
+
*
|
|
190
|
+
* @returns An independent `Buffer` copy containing the full stream contents.
|
|
191
|
+
*/
|
|
192
|
+
getvalue() {
|
|
193
|
+
this._checkClosed();
|
|
194
|
+
return Buffer.from(this._buffer);
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Returns a direct view of the underlying buffer without copying memory.
|
|
198
|
+
* Parity with Python's `BytesIO.getbuffer()`.
|
|
199
|
+
*
|
|
200
|
+
* @returns The underlying `Buffer` instance.
|
|
201
|
+
*/
|
|
202
|
+
getbuffer() {
|
|
203
|
+
this._checkClosed();
|
|
204
|
+
return this._buffer;
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Truncates the stream to the specified size (in bytes).
|
|
208
|
+
* If `size` is omitted, it defaults to the current pointer position.
|
|
209
|
+
* If the new size is larger than the current buffer, it is padded with null bytes (`0x00`).
|
|
210
|
+
* The current stream position is not changed.
|
|
211
|
+
*
|
|
212
|
+
* @param size - The target size in bytes. Defaults to current cursor position.
|
|
213
|
+
* @returns The new size.
|
|
214
|
+
*/
|
|
215
|
+
truncate(size) {
|
|
216
|
+
this._checkClosed();
|
|
217
|
+
const targetSize = size ?? this._pos;
|
|
218
|
+
if (targetSize < 0) {
|
|
219
|
+
throw new Error(`negative truncate position: ${targetSize}`);
|
|
220
|
+
}
|
|
221
|
+
if (targetSize < this._buffer.length) {
|
|
222
|
+
this._buffer = Buffer.from(this._buffer.subarray(0, targetSize));
|
|
223
|
+
}
|
|
224
|
+
else if (targetSize > this._buffer.length) {
|
|
225
|
+
const newBuf = Buffer.alloc(targetSize);
|
|
226
|
+
this._buffer.copy(newBuf, 0, 0, this._buffer.length);
|
|
227
|
+
this._buffer = newBuf;
|
|
228
|
+
}
|
|
229
|
+
return targetSize;
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Returns bytes from the current stream position without advancing the pointer.
|
|
233
|
+
*
|
|
234
|
+
* @param size - Number of bytes to peek. If omitted or <= 0, peeks all remaining bytes.
|
|
235
|
+
* @returns A `Buffer` containing the peeked bytes.
|
|
236
|
+
*/
|
|
237
|
+
peek(size = 0) {
|
|
238
|
+
this._checkClosed();
|
|
239
|
+
if (this._pos >= this._buffer.length) {
|
|
240
|
+
return Buffer.alloc(0);
|
|
241
|
+
}
|
|
242
|
+
const readSize = size <= 0 ? this._buffer.length - this._pos : size;
|
|
243
|
+
const end = Math.min(this._buffer.length, this._pos + readSize);
|
|
244
|
+
return Buffer.from(this._buffer.subarray(this._pos, end));
|
|
109
245
|
}
|
|
110
246
|
/**
|
|
111
247
|
* Returns a new `BytesIO` instance populated with a subarray/slice of the current buffer.
|
|
112
|
-
* The slice is created using
|
|
248
|
+
* The slice is created using `Buffer.subarray`, meaning it shares the same
|
|
113
249
|
* allocated memory as the original buffer.
|
|
114
250
|
*
|
|
115
251
|
* @param args - Arguments passed directly to the underlying `Buffer.subarray` method.
|
|
116
|
-
* Typically `[start[, end]]`, where `start` is the starting index (default 0) and
|
|
117
|
-
* `end` is the ending index (exclusive, default buffer length).
|
|
118
252
|
* @returns A new `BytesIO` instance containing the sliced view of the buffer.
|
|
119
253
|
*/
|
|
120
254
|
slice(...args) {
|
|
255
|
+
this._checkClosed();
|
|
121
256
|
return new BytesIO(this._buffer.subarray(...args));
|
|
122
257
|
}
|
|
123
258
|
/**
|
|
124
259
|
* Returns a JSON representation of the underlying buffer.
|
|
125
|
-
* This is useful for serializing the binary data, representing it as an object
|
|
126
|
-
* containing a `type` property (typically `"Buffer"`) and a `data` array of byte values.
|
|
127
260
|
*
|
|
128
261
|
* @returns An object representing the buffer in JSON format.
|
|
129
262
|
*/
|
|
@@ -134,10 +267,6 @@ export class BytesIO {
|
|
|
134
267
|
* Decodes the underlying buffer to a string according to the specified encoding.
|
|
135
268
|
*
|
|
136
269
|
* @param args - Arguments passed directly to the underlying `Buffer.toString` method.
|
|
137
|
-
* Typically `[encoding[, start[, end]]]`, where:
|
|
138
|
-
* - `encoding` is the character encoding to use (e.g., `'utf8'`, `'hex'`, `'base64'`).
|
|
139
|
-
* - `start` is the byte offset to start decoding (default 0).
|
|
140
|
-
* - `end` is the byte offset to stop decoding (exclusive, default buffer length).
|
|
141
270
|
* @returns The decoded string representation of the buffer.
|
|
142
271
|
*/
|
|
143
272
|
toString(...args) {
|
|
@@ -148,33 +277,131 @@ export class BytesIO {
|
|
|
148
277
|
* Advances the internal pointer by the number of bytes successfully read.
|
|
149
278
|
*
|
|
150
279
|
* @param length - The number of bytes to read.
|
|
151
|
-
* - If `undefined` or
|
|
280
|
+
* - If `undefined` or negative (e.g. `-1`), reads all remaining bytes to the end of the buffer.
|
|
152
281
|
* - If provided, reads at most `length` bytes from the current pointer position.
|
|
153
|
-
* @returns A new `Buffer` containing the read bytes. If the pointer is
|
|
282
|
+
* @returns A new `Buffer` containing the read bytes. If the pointer is at or past EOF, returns an empty buffer.
|
|
154
283
|
*/
|
|
155
284
|
read(length) {
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
this.
|
|
159
|
-
|
|
285
|
+
this._checkClosed();
|
|
286
|
+
if (length === undefined || length < 0) {
|
|
287
|
+
if (this._pos >= this._buffer.length) {
|
|
288
|
+
return Buffer.alloc(0);
|
|
289
|
+
}
|
|
290
|
+
const results = this._buffer.subarray(this._pos);
|
|
291
|
+
this._pos = this._buffer.length;
|
|
292
|
+
return Buffer.from(results);
|
|
160
293
|
}
|
|
161
|
-
if (length
|
|
162
|
-
|
|
163
|
-
this.seek(length, 1);
|
|
164
|
-
return results;
|
|
294
|
+
if (length === 0 || this._pos >= this._buffer.length) {
|
|
295
|
+
return Buffer.alloc(0);
|
|
165
296
|
}
|
|
166
|
-
|
|
297
|
+
const newPos = Math.min(this._buffer.length, this._pos + length);
|
|
298
|
+
const results = this._buffer.subarray(this._pos, newPos);
|
|
299
|
+
this._pos = newPos;
|
|
300
|
+
return Buffer.from(results);
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* Writes binary data at the current stream pointer position.
|
|
304
|
+
*
|
|
305
|
+
* @remarks
|
|
306
|
+
* If the current pointer is before the end of the buffer, existing bytes are overwritten in-place.
|
|
307
|
+
* If the current pointer is beyond the end of the buffer, the buffer is padded with null bytes (`0x00`) up to the pointer.
|
|
308
|
+
* Advances the pointer by the number of bytes written.
|
|
309
|
+
*
|
|
310
|
+
* @param data - The `Buffer`, `Uint8Array`, or `string` containing the data to write.
|
|
311
|
+
* @param encoding - The character encoding if `data` is a string. Defaults to `'utf-8'`.
|
|
312
|
+
* @returns The current `BytesIO` instance, enabling method chaining.
|
|
313
|
+
*/
|
|
314
|
+
write(data, encoding = 'utf-8') {
|
|
315
|
+
this._checkClosed();
|
|
316
|
+
const toWrite = typeof data === 'string'
|
|
317
|
+
? Buffer.from(data, encoding)
|
|
318
|
+
: Buffer.isBuffer(data)
|
|
319
|
+
? data
|
|
320
|
+
: Buffer.from(data.buffer, data.byteOffset, data.byteLength);
|
|
321
|
+
if (toWrite.length === 0) {
|
|
322
|
+
return this;
|
|
323
|
+
}
|
|
324
|
+
const pos = this._pos;
|
|
325
|
+
const endPos = pos + toWrite.length;
|
|
326
|
+
if (endPos <= this._buffer.length) {
|
|
327
|
+
toWrite.copy(this._buffer, pos);
|
|
328
|
+
}
|
|
329
|
+
else {
|
|
330
|
+
const newLength = Math.max(endPos, this._buffer.length);
|
|
331
|
+
const newBuf = Buffer.alloc(newLength);
|
|
332
|
+
this._buffer.copy(newBuf, 0, 0, Math.min(this._buffer.length, pos));
|
|
333
|
+
toWrite.copy(newBuf, pos);
|
|
334
|
+
this._buffer = newBuf;
|
|
335
|
+
}
|
|
336
|
+
this._pos = endPos;
|
|
337
|
+
return this;
|
|
338
|
+
}
|
|
339
|
+
// --- Typed Integer, Float, and Double Readers ---
|
|
340
|
+
/**
|
|
341
|
+
* Reads a signed 8-bit integer from the current pointer position.
|
|
342
|
+
*/
|
|
343
|
+
readInt8(size = 1) {
|
|
344
|
+
this._checkClosed();
|
|
345
|
+
const results = this._buffer.readInt8(this._pos);
|
|
346
|
+
this.seek(size, BytesIO.SEEK_CUR);
|
|
347
|
+
return results;
|
|
348
|
+
}
|
|
349
|
+
/**
|
|
350
|
+
* Reads an unsigned 8-bit integer from the current pointer position.
|
|
351
|
+
*/
|
|
352
|
+
readUInt8(size = 1) {
|
|
353
|
+
this._checkClosed();
|
|
354
|
+
const results = this._buffer.readUInt8(this._pos);
|
|
355
|
+
this.seek(size, BytesIO.SEEK_CUR);
|
|
356
|
+
return results;
|
|
357
|
+
}
|
|
358
|
+
/**
|
|
359
|
+
* Reads a signed 16-bit integer in little-endian format (`Int16LE`).
|
|
360
|
+
*/
|
|
361
|
+
readInt16LE(size = 2) {
|
|
362
|
+
this._checkClosed();
|
|
363
|
+
const results = this._buffer.readInt16LE(this._pos);
|
|
364
|
+
this.seek(size, BytesIO.SEEK_CUR);
|
|
365
|
+
return results;
|
|
366
|
+
}
|
|
367
|
+
/**
|
|
368
|
+
* Reads a signed 16-bit integer in big-endian format (`Int16BE`).
|
|
369
|
+
*/
|
|
370
|
+
readInt16BE(size = 2) {
|
|
371
|
+
this._checkClosed();
|
|
372
|
+
const results = this._buffer.readInt16BE(this._pos);
|
|
373
|
+
this.seek(size, BytesIO.SEEK_CUR);
|
|
374
|
+
return results;
|
|
375
|
+
}
|
|
376
|
+
/**
|
|
377
|
+
* Reads an unsigned 16-bit integer in little-endian format (`UInt16LE`).
|
|
378
|
+
*/
|
|
379
|
+
readUInt16LE(size = 2) {
|
|
380
|
+
this._checkClosed();
|
|
381
|
+
const results = this._buffer.readUInt16LE(this._pos);
|
|
382
|
+
this.seek(size, BytesIO.SEEK_CUR);
|
|
383
|
+
return results;
|
|
384
|
+
}
|
|
385
|
+
/**
|
|
386
|
+
* Reads an unsigned 16-bit integer in big-endian format (`UInt16BE`).
|
|
387
|
+
*/
|
|
388
|
+
readUInt16BE(size = 2) {
|
|
389
|
+
this._checkClosed();
|
|
390
|
+
const results = this._buffer.readUInt16BE(this._pos);
|
|
391
|
+
this.seek(size, BytesIO.SEEK_CUR);
|
|
392
|
+
return results;
|
|
167
393
|
}
|
|
168
394
|
/**
|
|
169
395
|
* Reads a signed 32-bit integer in little-endian format (`Int32LE`) from the current pointer position.
|
|
170
396
|
* Automatically advances the pointer by the specified `size` of bytes.
|
|
171
397
|
*
|
|
172
398
|
* @param size - The number of bytes to advance the pointer after reading. Defaults to `4`.
|
|
173
|
-
* @returns The read signed 32-bit integer
|
|
399
|
+
* @returns The read signed 32-bit integer.
|
|
174
400
|
*/
|
|
175
401
|
readInt32LE(size = 4) {
|
|
176
|
-
|
|
177
|
-
this.
|
|
402
|
+
this._checkClosed();
|
|
403
|
+
const results = this._buffer.readInt32LE(this._pos);
|
|
404
|
+
this.seek(size, BytesIO.SEEK_CUR);
|
|
178
405
|
return results;
|
|
179
406
|
}
|
|
180
407
|
/**
|
|
@@ -182,11 +409,12 @@ export class BytesIO {
|
|
|
182
409
|
* Automatically advances the pointer by the specified `size` of bytes.
|
|
183
410
|
*
|
|
184
411
|
* @param size - The number of bytes to advance the pointer after reading. Defaults to `4`.
|
|
185
|
-
* @returns The read signed 32-bit integer
|
|
412
|
+
* @returns The read signed 32-bit integer.
|
|
186
413
|
*/
|
|
187
414
|
readInt32BE(size = 4) {
|
|
188
|
-
|
|
189
|
-
this.
|
|
415
|
+
this._checkClosed();
|
|
416
|
+
const results = this._buffer.readInt32BE(this._pos);
|
|
417
|
+
this.seek(size, BytesIO.SEEK_CUR);
|
|
190
418
|
return results;
|
|
191
419
|
}
|
|
192
420
|
/**
|
|
@@ -194,11 +422,12 @@ export class BytesIO {
|
|
|
194
422
|
* Automatically advances the pointer by the specified `size` of bytes.
|
|
195
423
|
*
|
|
196
424
|
* @param size - The number of bytes to advance the pointer after reading. Defaults to `4`.
|
|
197
|
-
* @returns The read unsigned 32-bit integer
|
|
425
|
+
* @returns The read unsigned 32-bit integer.
|
|
198
426
|
*/
|
|
199
427
|
readUInt32LE(size = 4) {
|
|
200
|
-
|
|
201
|
-
this.
|
|
428
|
+
this._checkClosed();
|
|
429
|
+
const results = this._buffer.readUInt32LE(this._pos);
|
|
430
|
+
this.seek(size, BytesIO.SEEK_CUR);
|
|
202
431
|
return results;
|
|
203
432
|
}
|
|
204
433
|
/**
|
|
@@ -209,8 +438,9 @@ export class BytesIO {
|
|
|
209
438
|
* @returns The read unsigned 32-bit integer.
|
|
210
439
|
*/
|
|
211
440
|
readUInt32BE(size = 4) {
|
|
212
|
-
|
|
213
|
-
this.
|
|
441
|
+
this._checkClosed();
|
|
442
|
+
const results = this._buffer.readUInt32BE(this._pos);
|
|
443
|
+
this.seek(size, BytesIO.SEEK_CUR);
|
|
214
444
|
return results;
|
|
215
445
|
}
|
|
216
446
|
/**
|
|
@@ -218,11 +448,12 @@ export class BytesIO {
|
|
|
218
448
|
* Automatically advances the pointer by the specified `size` of bytes.
|
|
219
449
|
*
|
|
220
450
|
* @param size - The number of bytes to advance the pointer after reading. Defaults to `8`.
|
|
221
|
-
* @returns The read signed 64-bit BigInt
|
|
451
|
+
* @returns The read signed 64-bit BigInt.
|
|
222
452
|
*/
|
|
223
453
|
readBigInt64LE(size = 8) {
|
|
224
|
-
|
|
225
|
-
this.
|
|
454
|
+
this._checkClosed();
|
|
455
|
+
const results = this._buffer.readBigInt64LE(this._pos);
|
|
456
|
+
this.seek(size, BytesIO.SEEK_CUR);
|
|
226
457
|
return results;
|
|
227
458
|
}
|
|
228
459
|
/**
|
|
@@ -230,11 +461,12 @@ export class BytesIO {
|
|
|
230
461
|
* Automatically advances the pointer by the specified `size` of bytes.
|
|
231
462
|
*
|
|
232
463
|
* @param size - The number of bytes to advance the pointer after reading. Defaults to `8`.
|
|
233
|
-
* @returns The read signed 64-bit BigInt
|
|
464
|
+
* @returns The read signed 64-bit BigInt.
|
|
234
465
|
*/
|
|
235
466
|
readBigInt64BE(size = 8) {
|
|
236
|
-
|
|
237
|
-
this.
|
|
467
|
+
this._checkClosed();
|
|
468
|
+
const results = this._buffer.readBigInt64BE(this._pos);
|
|
469
|
+
this.seek(size, BytesIO.SEEK_CUR);
|
|
238
470
|
return results;
|
|
239
471
|
}
|
|
240
472
|
/**
|
|
@@ -242,11 +474,12 @@ export class BytesIO {
|
|
|
242
474
|
* Automatically advances the pointer by the specified `size` of bytes.
|
|
243
475
|
*
|
|
244
476
|
* @param size - The number of bytes to advance the pointer after reading. Defaults to `8`.
|
|
245
|
-
* @returns The read unsigned 64-bit BigInt
|
|
477
|
+
* @returns The read unsigned 64-bit BigInt.
|
|
246
478
|
*/
|
|
247
479
|
readBigUInt64LE(size = 8) {
|
|
248
|
-
|
|
249
|
-
this.
|
|
480
|
+
this._checkClosed();
|
|
481
|
+
const results = this._buffer.readBigUInt64LE(this._pos);
|
|
482
|
+
this.seek(size, BytesIO.SEEK_CUR);
|
|
250
483
|
return results;
|
|
251
484
|
}
|
|
252
485
|
/**
|
|
@@ -254,77 +487,210 @@ export class BytesIO {
|
|
|
254
487
|
* Automatically advances the pointer by the specified `size` of bytes.
|
|
255
488
|
*
|
|
256
489
|
* @param size - The number of bytes to advance the pointer after reading. Defaults to `8`.
|
|
257
|
-
* @returns The read unsigned 64-bit BigInt
|
|
490
|
+
* @returns The read unsigned 64-bit BigInt.
|
|
258
491
|
*/
|
|
259
492
|
readBigUInt64BE(size = 8) {
|
|
260
|
-
|
|
261
|
-
this.
|
|
493
|
+
this._checkClosed();
|
|
494
|
+
const results = this._buffer.readBigUInt64BE(this._pos);
|
|
495
|
+
this.seek(size, BytesIO.SEEK_CUR);
|
|
262
496
|
return results;
|
|
263
497
|
}
|
|
264
498
|
/**
|
|
265
|
-
* Reads a 64-bit double-precision floating-point number in little-endian format (`DoubleLE`)
|
|
499
|
+
* Reads a 64-bit double-precision floating-point number in little-endian format (`DoubleLE`).
|
|
266
500
|
* Automatically advances the pointer by the specified `size` of bytes.
|
|
267
501
|
*
|
|
268
502
|
* @param size - The number of bytes to advance the pointer after reading. Defaults to `8`.
|
|
269
503
|
* @returns The read double-precision floating-point number.
|
|
270
504
|
*/
|
|
271
505
|
readDoubleLE(size = 8) {
|
|
272
|
-
|
|
273
|
-
this.
|
|
506
|
+
this._checkClosed();
|
|
507
|
+
const results = this._buffer.readDoubleLE(this._pos);
|
|
508
|
+
this.seek(size, BytesIO.SEEK_CUR);
|
|
274
509
|
return results;
|
|
275
510
|
}
|
|
276
511
|
/**
|
|
277
|
-
* Reads a 64-bit double-precision floating-point number in big-endian format (`DoubleBE`)
|
|
512
|
+
* Reads a 64-bit double-precision floating-point number in big-endian format (`DoubleBE`).
|
|
278
513
|
* Automatically advances the pointer by the specified `size` of bytes.
|
|
279
514
|
*
|
|
280
515
|
* @param size - The number of bytes to advance the pointer after reading. Defaults to `8`.
|
|
281
516
|
* @returns The read double-precision floating-point number.
|
|
282
517
|
*/
|
|
283
518
|
readDoubleBE(size = 8) {
|
|
284
|
-
|
|
285
|
-
this.
|
|
519
|
+
this._checkClosed();
|
|
520
|
+
const results = this._buffer.readDoubleBE(this._pos);
|
|
521
|
+
this.seek(size, BytesIO.SEEK_CUR);
|
|
286
522
|
return results;
|
|
287
523
|
}
|
|
288
524
|
/**
|
|
289
|
-
* Reads a 32-bit single-precision floating-point number in little-endian format (`FloatLE`)
|
|
525
|
+
* Reads a 32-bit single-precision floating-point number in little-endian format (`FloatLE`).
|
|
290
526
|
* Automatically advances the pointer by the specified `size` of bytes.
|
|
291
527
|
*
|
|
292
528
|
* @param size - The number of bytes to advance the pointer after reading. Defaults to `4`.
|
|
293
529
|
* @returns The read single-precision floating-point number.
|
|
294
530
|
*/
|
|
295
531
|
readFloatLE(size = 4) {
|
|
296
|
-
|
|
297
|
-
this.
|
|
532
|
+
this._checkClosed();
|
|
533
|
+
const results = this._buffer.readFloatLE(this._pos);
|
|
534
|
+
this.seek(size, BytesIO.SEEK_CUR);
|
|
298
535
|
return results;
|
|
299
536
|
}
|
|
300
537
|
/**
|
|
301
|
-
* Reads a 32-bit single-precision floating-point number in big-endian format (`FloatBE`)
|
|
538
|
+
* Reads a 32-bit single-precision floating-point number in big-endian format (`FloatBE`).
|
|
302
539
|
* Automatically advances the pointer by the specified `size` of bytes.
|
|
303
540
|
*
|
|
304
541
|
* @param size - The number of bytes to advance the pointer after reading. Defaults to `4`.
|
|
305
542
|
* @returns The read single-precision floating-point number.
|
|
306
543
|
*/
|
|
307
544
|
readFloatBE(size = 4) {
|
|
308
|
-
|
|
309
|
-
this.
|
|
545
|
+
this._checkClosed();
|
|
546
|
+
const results = this._buffer.readFloatBE(this._pos);
|
|
547
|
+
this.seek(size, BytesIO.SEEK_CUR);
|
|
310
548
|
return results;
|
|
311
549
|
}
|
|
550
|
+
// --- Typed Integer, Float, and Double Writers ---
|
|
312
551
|
/**
|
|
313
|
-
*
|
|
314
|
-
*
|
|
315
|
-
* @remarks
|
|
316
|
-
* This operation concatenates the new data to the existing buffer, increasing its total length.
|
|
317
|
-
* Note that this method does not modify or advance the current pointer position (`post`).
|
|
318
|
-
*
|
|
319
|
-
* @param data - The `Buffer` containing the binary data to append.
|
|
320
|
-
* @returns The current `BytesIO` instance, enabling method chaining.
|
|
552
|
+
* Writes a signed 8-bit integer at the current stream pointer position.
|
|
321
553
|
*/
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
554
|
+
writeInt8(value) {
|
|
555
|
+
const buf = Buffer.allocUnsafe(1);
|
|
556
|
+
buf.writeInt8(value, 0);
|
|
557
|
+
return this.write(buf);
|
|
558
|
+
}
|
|
559
|
+
/**
|
|
560
|
+
* Writes an unsigned 8-bit integer at the current stream pointer position.
|
|
561
|
+
*/
|
|
562
|
+
writeUInt8(value) {
|
|
563
|
+
const buf = Buffer.allocUnsafe(1);
|
|
564
|
+
buf.writeUInt8(value, 0);
|
|
565
|
+
return this.write(buf);
|
|
566
|
+
}
|
|
567
|
+
/**
|
|
568
|
+
* Writes a signed 16-bit integer in little-endian format at the current stream pointer position.
|
|
569
|
+
*/
|
|
570
|
+
writeInt16LE(value) {
|
|
571
|
+
const buf = Buffer.allocUnsafe(2);
|
|
572
|
+
buf.writeInt16LE(value, 0);
|
|
573
|
+
return this.write(buf);
|
|
574
|
+
}
|
|
575
|
+
/**
|
|
576
|
+
* Writes a signed 16-bit integer in big-endian format at the current stream pointer position.
|
|
577
|
+
*/
|
|
578
|
+
writeInt16BE(value) {
|
|
579
|
+
const buf = Buffer.allocUnsafe(2);
|
|
580
|
+
buf.writeInt16BE(value, 0);
|
|
581
|
+
return this.write(buf);
|
|
582
|
+
}
|
|
583
|
+
/**
|
|
584
|
+
* Writes an unsigned 16-bit integer in little-endian format at the current stream pointer position.
|
|
585
|
+
*/
|
|
586
|
+
writeUInt16LE(value) {
|
|
587
|
+
const buf = Buffer.allocUnsafe(2);
|
|
588
|
+
buf.writeUInt16LE(value, 0);
|
|
589
|
+
return this.write(buf);
|
|
590
|
+
}
|
|
591
|
+
/**
|
|
592
|
+
* Writes an unsigned 16-bit integer in big-endian format at the current stream pointer position.
|
|
593
|
+
*/
|
|
594
|
+
writeUInt16BE(value) {
|
|
595
|
+
const buf = Buffer.allocUnsafe(2);
|
|
596
|
+
buf.writeUInt16BE(value, 0);
|
|
597
|
+
return this.write(buf);
|
|
598
|
+
}
|
|
599
|
+
/**
|
|
600
|
+
* Writes a signed 32-bit integer in little-endian format at the current stream pointer position.
|
|
601
|
+
*/
|
|
602
|
+
writeInt32LE(value) {
|
|
603
|
+
const buf = Buffer.allocUnsafe(4);
|
|
604
|
+
buf.writeInt32LE(value, 0);
|
|
605
|
+
return this.write(buf);
|
|
606
|
+
}
|
|
607
|
+
/**
|
|
608
|
+
* Writes a signed 32-bit integer in big-endian format at the current stream pointer position.
|
|
609
|
+
*/
|
|
610
|
+
writeInt32BE(value) {
|
|
611
|
+
const buf = Buffer.allocUnsafe(4);
|
|
612
|
+
buf.writeInt32BE(value, 0);
|
|
613
|
+
return this.write(buf);
|
|
614
|
+
}
|
|
615
|
+
/**
|
|
616
|
+
* Writes an unsigned 32-bit integer in little-endian format at the current stream pointer position.
|
|
617
|
+
*/
|
|
618
|
+
writeUInt32LE(value) {
|
|
619
|
+
const buf = Buffer.allocUnsafe(4);
|
|
620
|
+
buf.writeUInt32LE(value, 0);
|
|
621
|
+
return this.write(buf);
|
|
622
|
+
}
|
|
623
|
+
/**
|
|
624
|
+
* Writes an unsigned 32-bit integer in big-endian format at the current stream pointer position.
|
|
625
|
+
*/
|
|
626
|
+
writeUInt32BE(value) {
|
|
627
|
+
const buf = Buffer.allocUnsafe(4);
|
|
628
|
+
buf.writeUInt32BE(value, 0);
|
|
629
|
+
return this.write(buf);
|
|
630
|
+
}
|
|
631
|
+
/**
|
|
632
|
+
* Writes a signed 64-bit BigInt in little-endian format at the current stream pointer position.
|
|
633
|
+
*/
|
|
634
|
+
writeBigInt64LE(value) {
|
|
635
|
+
const buf = Buffer.allocUnsafe(8);
|
|
636
|
+
buf.writeBigInt64LE(value, 0);
|
|
637
|
+
return this.write(buf);
|
|
638
|
+
}
|
|
639
|
+
/**
|
|
640
|
+
* Writes a signed 64-bit BigInt in big-endian format at the current stream pointer position.
|
|
641
|
+
*/
|
|
642
|
+
writeBigInt64BE(value) {
|
|
643
|
+
const buf = Buffer.allocUnsafe(8);
|
|
644
|
+
buf.writeBigInt64BE(value, 0);
|
|
645
|
+
return this.write(buf);
|
|
646
|
+
}
|
|
647
|
+
/**
|
|
648
|
+
* Writes an unsigned 64-bit BigInt in little-endian format at the current stream pointer position.
|
|
649
|
+
*/
|
|
650
|
+
writeBigUInt64LE(value) {
|
|
651
|
+
const buf = Buffer.allocUnsafe(8);
|
|
652
|
+
buf.writeBigUInt64LE(value, 0);
|
|
653
|
+
return this.write(buf);
|
|
654
|
+
}
|
|
655
|
+
/**
|
|
656
|
+
* Writes an unsigned 64-bit BigInt in big-endian format at the current stream pointer position.
|
|
657
|
+
*/
|
|
658
|
+
writeBigUInt64BE(value) {
|
|
659
|
+
const buf = Buffer.allocUnsafe(8);
|
|
660
|
+
buf.writeBigUInt64BE(value, 0);
|
|
661
|
+
return this.write(buf);
|
|
662
|
+
}
|
|
663
|
+
/**
|
|
664
|
+
* Writes a 32-bit single-precision floating-point number in little-endian format at the current stream pointer position.
|
|
665
|
+
*/
|
|
666
|
+
writeFloatLE(value) {
|
|
667
|
+
const buf = Buffer.allocUnsafe(4);
|
|
668
|
+
buf.writeFloatLE(value, 0);
|
|
669
|
+
return this.write(buf);
|
|
670
|
+
}
|
|
671
|
+
/**
|
|
672
|
+
* Writes a 32-bit single-precision floating-point number in big-endian format at the current stream pointer position.
|
|
673
|
+
*/
|
|
674
|
+
writeFloatBE(value) {
|
|
675
|
+
const buf = Buffer.allocUnsafe(4);
|
|
676
|
+
buf.writeFloatBE(value, 0);
|
|
677
|
+
return this.write(buf);
|
|
678
|
+
}
|
|
679
|
+
/**
|
|
680
|
+
* Writes a 64-bit double-precision floating-point number in little-endian format at the current stream pointer position.
|
|
681
|
+
*/
|
|
682
|
+
writeDoubleLE(value) {
|
|
683
|
+
const buf = Buffer.allocUnsafe(8);
|
|
684
|
+
buf.writeDoubleLE(value, 0);
|
|
685
|
+
return this.write(buf);
|
|
686
|
+
}
|
|
687
|
+
/**
|
|
688
|
+
* Writes a 64-bit double-precision floating-point number in big-endian format at the current stream pointer position.
|
|
689
|
+
*/
|
|
690
|
+
writeDoubleBE(value) {
|
|
691
|
+
const buf = Buffer.allocUnsafe(8);
|
|
692
|
+
buf.writeDoubleBE(value, 0);
|
|
693
|
+
return this.write(buf);
|
|
328
694
|
}
|
|
329
695
|
/**
|
|
330
696
|
* Allocates a new `BytesIO` instance populated with a zero-filled buffer of the specified size.
|
|
@@ -340,16 +706,16 @@ export class BytesIO {
|
|
|
340
706
|
*
|
|
341
707
|
* @param input - The input data. Can be a `Buffer`, `Uint8Array`, `ArrayBuffer`, `SharedArrayBuffer`,
|
|
342
708
|
* an `Array` of numbers, a `string`, or an object with a `valueOf` or `Symbol.toPrimitive` method.
|
|
343
|
-
* @param encode - The character encoding to use if the `input` is a string
|
|
709
|
+
* @param encode - The character encoding to use if the `input` is a string.
|
|
344
710
|
* @returns A new `BytesIO` instance initialized with the parsed input data.
|
|
345
711
|
*/
|
|
346
712
|
static from(input, encode) {
|
|
347
713
|
return new BytesIO(Buffer.from(input, encode));
|
|
348
714
|
}
|
|
349
715
|
/**
|
|
350
|
-
* Concatenates an array of `Buffer` instances into a single `BytesIO` instance.
|
|
716
|
+
* Concatenates an array of `Buffer` or `Uint8Array` instances into a single `BytesIO` instance.
|
|
351
717
|
*
|
|
352
|
-
* @param data - An array of `Buffer` objects to be joined together in order.
|
|
718
|
+
* @param data - An array of `Buffer` or `Uint8Array` objects to be joined together in order.
|
|
353
719
|
* @returns A new `BytesIO` instance containing the concatenated binary data.
|
|
354
720
|
*/
|
|
355
721
|
static concat(data) {
|
|
@@ -361,7 +727,7 @@ export class BytesIO {
|
|
|
361
727
|
* @returns The byte length of the buffer.
|
|
362
728
|
*/
|
|
363
729
|
get length() {
|
|
364
|
-
return
|
|
730
|
+
return this._buffer.length;
|
|
365
731
|
}
|
|
366
732
|
/**
|
|
367
733
|
* Gets the raw, underlying `Buffer` instance managed by this `BytesIO` object.
|
|
@@ -376,7 +742,13 @@ export class BytesIO {
|
|
|
376
742
|
*
|
|
377
743
|
* @returns The current pointer position.
|
|
378
744
|
*/
|
|
379
|
-
get
|
|
380
|
-
return this.
|
|
745
|
+
get pos() {
|
|
746
|
+
return this._pos;
|
|
747
|
+
}
|
|
748
|
+
/**
|
|
749
|
+
* Gets whether the stream has been closed.
|
|
750
|
+
*/
|
|
751
|
+
get closed() {
|
|
752
|
+
return this._closed;
|
|
381
753
|
}
|
|
382
754
|
}
|