@jsprismarine/jsbinaryutils
Advanced tools
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
@@ -1,7 +0,7 @@ | ||
| /// <reference types="node" /> | ||
| export default class BinaryStream { | ||
| private binary; | ||
| private buffer; | ||
| private writeBuffer; | ||
| private readBuffer; | ||
| private readIndex; | ||
| private writeIndex; | ||
| private writeCapacity; | ||
| /** | ||
@@ -69,3 +69,3 @@ * Creates a new BinaryStream instance. | ||
| /** | ||
| * Writes a 16 bit (2 bytes) signed big-endian number. | ||
| * Writes a 16 bit (2 bytes) signed little-endian number. | ||
| * @param {number} v | ||
@@ -230,3 +230,3 @@ */ | ||
| /** | ||
| * Writes a 64 bit (8 bytes) signed big-endian number. | ||
| * Writes a 64 bit (8 bytes) signed little-endian number. | ||
| * @param {bigint} v | ||
@@ -251,3 +251,3 @@ */ | ||
| /** | ||
| * Writes a 64 bit (8 bytes) unsigned big-endian number. | ||
| * Writes a 64 bit (8 bytes) unsigned little-endian number. | ||
| * @param {bigint} v | ||
@@ -297,2 +297,7 @@ */ | ||
| /** | ||
| * Ensures write buffer has enough capacity, grows if needed. | ||
| * @param {number} needed | ||
| */ | ||
| private ensureCapacity; | ||
| /** | ||
| * Increases the write offset by the given length. | ||
@@ -320,11 +325,37 @@ * @param {number} length | ||
| * @returns {Buffer} | ||
| * @deprecated | ||
| * @see getReadBuffer() | ||
| * @see getWriteBuffer() | ||
| */ | ||
| getBuffer(): Buffer; | ||
| /** | ||
| * Returns the read buffer if available. | ||
| * @returns {Buffer} | ||
| */ | ||
| getReadBuffer(): Buffer | null; | ||
| /** | ||
| * Returns the write buffer. | ||
| * @returns {Buffer} | ||
| */ | ||
| getWriteBuffer(): Buffer; | ||
| /** | ||
| * Sets the buffer for reading. | ||
| * make sure to reset the reading index! | ||
| * @param buf - The new Buffer. | ||
| * @deprecated | ||
| */ | ||
| setBuffer(buf: Buffer): void; | ||
| /** | ||
| * Sets the buffer for reading. | ||
| * @param buf - The new Buffer. | ||
| * @param rIndex - The new read index (default: 0, pass -1 to keep current). | ||
| */ | ||
| setReadBuffer(buf: Buffer, rIndex?: number): void; | ||
| /** | ||
| * Sets the buffer for writing. | ||
| * @param buf - The new Buffer. | ||
| * @param wIndex - The new write index (default: 0, pass -1 to keep current). | ||
| */ | ||
| setWriteBuffer(buf: Buffer, wIndex?: number): void; | ||
| /** | ||
| * Clears the whole BinaryStream instance. | ||
@@ -331,0 +362,0 @@ */ |
+180
-140
@@ -14,6 +14,7 @@ "use strict"; | ||
| constructor(buffer, offset = 0) { | ||
| this.binary = []; | ||
| this.buffer = null; | ||
| this.writeBuffer = null; | ||
| this.readBuffer = null; | ||
| this.writeIndex = 0; | ||
| this.buffer = buffer ?? null; // Keep this instance for reading | ||
| this.writeCapacity = 0; | ||
| this.readBuffer = buffer ?? null; // Keep this instance for reading | ||
| this.readIndex = offset; | ||
@@ -27,6 +28,7 @@ } | ||
| this.doReadAssertions(len); | ||
| return this.buffer.slice(this.readIndex, (this.readIndex += len)); | ||
| return this.readBuffer.subarray(this.readIndex, (this.readIndex += len)); | ||
| } | ||
| write(buf) { | ||
| this.binary = [...this.binary, ...buf]; | ||
| this.ensureCapacity(buf.byteLength); | ||
| buf.copy(this.writeBuffer, this.writeIndex); | ||
| this.writeIndex += buf.byteLength; | ||
@@ -40,3 +42,3 @@ } | ||
| this.doReadAssertions(1); | ||
| return this.buffer.readUInt8(this.readIndex++); | ||
| return this.readBuffer.readUInt8(this.readIndex++); | ||
| } | ||
@@ -48,4 +50,4 @@ /** | ||
| writeByte(v) { | ||
| v &= 0xff; | ||
| this.binary[this.writeIndex++] = v; | ||
| this.ensureCapacity(1); | ||
| this.writeBuffer[this.writeIndex++] = v & 0xff; | ||
| } | ||
@@ -58,3 +60,3 @@ /** | ||
| this.doReadAssertions(1); | ||
| return this.buffer.readInt8(this.readIndex++); | ||
| return this.readBuffer.readInt8(this.readIndex++); | ||
| } | ||
@@ -66,5 +68,5 @@ /** | ||
| writeSignedByte(v) { | ||
| if (v < 0) | ||
| v = 0xff + v + 1; | ||
| this.binary[this.writeIndex++] = v & 0xff; | ||
| this.ensureCapacity(1); | ||
| this.writeBuffer[this.writeIndex++] = | ||
| (v < 0 ? 0xff + v + 1 : v) & 0xff; | ||
| } | ||
@@ -77,3 +79,3 @@ /** | ||
| this.doReadAssertions(1); | ||
| return !!this.readByte(); | ||
| return this.readBuffer[this.readIndex++] !== 0; | ||
| } | ||
@@ -85,3 +87,4 @@ /** | ||
| writeBoolean(v) { | ||
| this.writeByte(+v); | ||
| this.ensureCapacity(1); | ||
| this.writeBuffer[this.writeIndex++] = v ? 1 : 0; | ||
| } | ||
@@ -94,3 +97,3 @@ /** | ||
| this.doReadAssertions(2); | ||
| return this.buffer.readInt16BE(this.addOffset(2)); | ||
| return this.readBuffer.readInt16BE(this.addOffset(2)); | ||
| } | ||
@@ -103,4 +106,5 @@ /** | ||
| this.doWriteAssertions(v, -32768, 32767); | ||
| this.writeByte(v >> 8); | ||
| this.writeByte(v); | ||
| this.ensureCapacity(2); | ||
| this.writeBuffer[this.writeIndex++] = (v >> 8) & 0xff; | ||
| this.writeBuffer[this.writeIndex++] = v & 0xff; | ||
| } | ||
@@ -113,6 +117,6 @@ /** | ||
| this.doReadAssertions(2); | ||
| return this.buffer.readInt16LE(this.addOffset(2)); | ||
| return this.readBuffer.readInt16LE(this.addOffset(2)); | ||
| } | ||
| /** | ||
| * Writes a 16 bit (2 bytes) signed big-endian number. | ||
| * Writes a 16 bit (2 bytes) signed little-endian number. | ||
| * @param {number} v | ||
@@ -122,4 +126,5 @@ */ | ||
| this.doWriteAssertions(v, -32768, 32767); | ||
| this.writeByte(v); | ||
| this.writeByte(v >> 8); | ||
| this.ensureCapacity(2); | ||
| this.writeBuffer[this.writeIndex++] = v & 0xff; | ||
| this.writeBuffer[this.writeIndex++] = (v >> 8) & 0xff; | ||
| } | ||
@@ -132,3 +137,3 @@ /** | ||
| this.doReadAssertions(2); | ||
| return this.buffer.readUInt16BE(this.addOffset(2)); | ||
| return this.readBuffer.readUInt16BE(this.addOffset(2)); | ||
| } | ||
@@ -141,4 +146,5 @@ /** | ||
| this.doWriteAssertions(v, 0, 65535); | ||
| this.writeByte(v >>> 8); | ||
| this.writeByte(v); | ||
| this.ensureCapacity(2); | ||
| this.writeBuffer[this.writeIndex++] = (v >>> 8) & 0xff; | ||
| this.writeBuffer[this.writeIndex++] = v & 0xff; | ||
| } | ||
@@ -151,3 +157,3 @@ /** | ||
| this.doReadAssertions(2); | ||
| return this.buffer.readUInt16LE(this.addOffset(2)); | ||
| return this.readBuffer.readUInt16LE(this.addOffset(2)); | ||
| } | ||
@@ -160,4 +166,5 @@ /** | ||
| this.doWriteAssertions(v, 0, 65535); | ||
| this.writeByte(v); | ||
| this.writeByte(v >>> 8); | ||
| this.ensureCapacity(2); | ||
| this.writeBuffer[this.writeIndex++] = v & 0xff; | ||
| this.writeBuffer[this.writeIndex++] = (v >>> 8) & 0xff; | ||
| } | ||
@@ -170,3 +177,3 @@ /** | ||
| this.doReadAssertions(3); | ||
| return this.buffer.readIntBE(this.addOffset(3), 3); | ||
| return this.readBuffer.readIntBE(this.addOffset(3), 3); | ||
| } | ||
@@ -179,5 +186,6 @@ /** | ||
| this.doWriteAssertions(v, -8388608, 8388607); | ||
| this.writeByte((v & 0xff0000) >> 16); // msb | ||
| this.writeByte((v & 0x00ff00) >> 8); // mib | ||
| this.writeByte(v & 0x0000ff); // lsb | ||
| this.ensureCapacity(3); | ||
| this.writeBuffer[this.writeIndex++] = (v >> 16) & 0xff; | ||
| this.writeBuffer[this.writeIndex++] = (v >> 8) & 0xff; | ||
| this.writeBuffer[this.writeIndex++] = v & 0xff; | ||
| } | ||
@@ -190,3 +198,3 @@ /** | ||
| this.doReadAssertions(3); | ||
| return this.buffer.readIntLE(this.addOffset(3), 3); | ||
| return this.readBuffer.readIntLE(this.addOffset(3), 3); | ||
| } | ||
@@ -199,5 +207,6 @@ /** | ||
| this.doWriteAssertions(v, -8388608, 8388607); | ||
| this.writeByte(v & 0x0000ff); | ||
| this.writeByte((v & 0x00ff00) >> 8); | ||
| this.writeByte((v & 0xff0000) >> 16); | ||
| this.ensureCapacity(3); | ||
| this.writeBuffer[this.writeIndex++] = v & 0xff; | ||
| this.writeBuffer[this.writeIndex++] = (v >> 8) & 0xff; | ||
| this.writeBuffer[this.writeIndex++] = (v >> 16) & 0xff; | ||
| } | ||
@@ -210,3 +219,3 @@ /** | ||
| this.doReadAssertions(3); | ||
| return this.buffer.readUIntBE(this.addOffset(3), 3); | ||
| return this.readBuffer.readUIntBE(this.addOffset(3), 3); | ||
| } | ||
@@ -219,5 +228,6 @@ /** | ||
| this.doWriteAssertions(v, 0, 16777215); | ||
| this.writeByte((v & 0xff0000) >>> 16); // msb | ||
| this.writeByte((v & 0x00ff00) >>> 8); // mib | ||
| this.writeByte(v & 0x0000ff); // lsb | ||
| this.ensureCapacity(3); | ||
| this.writeBuffer[this.writeIndex++] = (v >>> 16) & 0xff; | ||
| this.writeBuffer[this.writeIndex++] = (v >>> 8) & 0xff; | ||
| this.writeBuffer[this.writeIndex++] = v & 0xff; | ||
| } | ||
@@ -230,3 +240,3 @@ /** | ||
| this.doReadAssertions(3); | ||
| return this.buffer.readUIntLE(this.addOffset(3), 3); | ||
| return this.readBuffer.readUIntLE(this.addOffset(3), 3); | ||
| } | ||
@@ -239,5 +249,6 @@ /** | ||
| this.doWriteAssertions(v, 0, 16777215); | ||
| this.writeByte(v & 0x0000ff); | ||
| this.writeByte((v & 0x00ff00) >>> 8); | ||
| this.writeByte((v & 0xff0000) >>> 16); | ||
| this.ensureCapacity(3); | ||
| this.writeBuffer[this.writeIndex++] = v & 0xff; | ||
| this.writeBuffer[this.writeIndex++] = (v >>> 8) & 0xff; | ||
| this.writeBuffer[this.writeIndex++] = (v >>> 16) & 0xff; | ||
| } | ||
@@ -250,3 +261,3 @@ /** | ||
| this.doReadAssertions(4); | ||
| return this.buffer.readInt32BE(this.addOffset(4)); | ||
| return this.readBuffer.readInt32BE(this.addOffset(4)); | ||
| } | ||
@@ -258,9 +269,6 @@ /** | ||
| writeInt(v) { | ||
| if (v < 0) | ||
| v = v & (0xffffffff + v + 1); | ||
| this.doWriteAssertions(v, -2147483648, 2147483647); | ||
| this.writeByte(v >> 24); | ||
| this.writeByte(v >> 16); | ||
| this.writeByte(v >> 8); | ||
| this.writeByte(v); | ||
| this.ensureCapacity(4); | ||
| this.writeBuffer.writeInt32BE(v, this.writeIndex); | ||
| this.writeIndex += 4; | ||
| } | ||
@@ -273,3 +281,3 @@ /** | ||
| this.doReadAssertions(4); | ||
| return this.buffer.readIntLE(this.addOffset(4), 4); | ||
| return this.readBuffer.readIntLE(this.addOffset(4), 4); | ||
| } | ||
@@ -281,9 +289,6 @@ /** | ||
| writeIntLE(v) { | ||
| if (v < 0) | ||
| v = v & (0xffffffff + v + 1); | ||
| this.doWriteAssertions(v, -2147483648, 2147483647); | ||
| this.writeByte(v); | ||
| this.writeByte(v >> 8); | ||
| this.writeByte(v >> 16); | ||
| this.writeByte(v >> 24); | ||
| this.ensureCapacity(4); | ||
| this.writeBuffer.writeInt32LE(v, this.writeIndex); | ||
| this.writeIndex += 4; | ||
| } | ||
@@ -296,3 +301,3 @@ /** | ||
| this.doReadAssertions(4); | ||
| return this.buffer.readUInt32BE(this.addOffset(4)); | ||
| return this.readBuffer.readUInt32BE(this.addOffset(4)); | ||
| } | ||
@@ -305,6 +310,5 @@ /** | ||
| this.doWriteAssertions(v, 0, 4294967295); | ||
| this.writeByte(v >>> 24); | ||
| this.writeByte(v >>> 16); | ||
| this.writeByte(v >>> 8); | ||
| this.writeByte(v); | ||
| this.ensureCapacity(4); | ||
| this.writeBuffer.writeUInt32BE(v, this.writeIndex); | ||
| this.writeIndex += 4; | ||
| } | ||
@@ -317,3 +321,3 @@ /** | ||
| this.doReadAssertions(4); | ||
| return this.buffer.readUInt32LE(this.addOffset(4)); | ||
| return this.readBuffer.readUInt32LE(this.addOffset(4)); | ||
| } | ||
@@ -326,6 +330,5 @@ /** | ||
| this.doWriteAssertions(v, 0, 4294967295); | ||
| this.writeByte(v); | ||
| this.writeByte(v >>> 8); | ||
| this.writeByte(v >>> 16); | ||
| this.writeByte(v >>> 24); | ||
| this.ensureCapacity(4); | ||
| this.writeBuffer.writeUInt32LE(v, this.writeIndex); | ||
| this.writeIndex += 4; | ||
| } | ||
@@ -338,3 +341,3 @@ /** | ||
| this.doReadAssertions(4); | ||
| return this.buffer.readFloatBE(this.addOffset(4)); | ||
| return this.readBuffer.readFloatBE(this.addOffset(4)); | ||
| } | ||
@@ -347,3 +350,5 @@ /** | ||
| this.doWriteAssertions(v, -3.4028234663852886e38, +3.4028234663852886e38); | ||
| this.write(new Uint8Array(new Float32Array([v]).buffer).reverse()); | ||
| this.ensureCapacity(4); | ||
| this.writeBuffer.writeFloatBE(v, this.writeIndex); | ||
| this.writeIndex += 4; | ||
| } | ||
@@ -356,3 +361,3 @@ /** | ||
| this.doReadAssertions(4); | ||
| return this.buffer.readFloatLE(this.addOffset(4)); | ||
| return this.readBuffer.readFloatLE(this.addOffset(4)); | ||
| } | ||
@@ -365,3 +370,5 @@ /** | ||
| this.doWriteAssertions(v, -3.4028234663852886e38, +3.4028234663852886e38); | ||
| this.write(new Uint8Array(new Float32Array([v]).buffer)); | ||
| this.ensureCapacity(4); | ||
| this.writeBuffer.writeFloatLE(v, this.writeIndex); | ||
| this.writeIndex += 4; | ||
| } | ||
@@ -374,3 +381,3 @@ /** | ||
| this.doReadAssertions(8); | ||
| return this.buffer.readDoubleBE(this.addOffset(8)); | ||
| return this.readBuffer.readDoubleBE(this.addOffset(8)); | ||
| } | ||
@@ -383,3 +390,5 @@ /** | ||
| this.doWriteAssertions(v, -1.7976931348623157e308, +1.7976931348623157e308); | ||
| this.write(new Uint8Array(new Float64Array([v]).buffer).reverse()); | ||
| this.ensureCapacity(8); | ||
| this.writeBuffer.writeDoubleBE(v, this.writeIndex); | ||
| this.writeIndex += 8; | ||
| } | ||
@@ -392,3 +401,3 @@ /** | ||
| this.doReadAssertions(8); | ||
| return this.buffer.readDoubleLE(this.addOffset(8)); | ||
| return this.readBuffer.readDoubleLE(this.addOffset(8)); | ||
| } | ||
@@ -401,3 +410,5 @@ /** | ||
| this.doWriteAssertions(v, -1.7976931348623157e308, +1.7976931348623157e308); | ||
| this.write(new Uint8Array(new Float64Array([v]).buffer)); | ||
| this.ensureCapacity(8); | ||
| this.writeBuffer.writeDoubleLE(v, this.writeIndex); | ||
| this.writeIndex += 8; | ||
| } | ||
@@ -410,3 +421,3 @@ /** | ||
| this.doReadAssertions(8); | ||
| return this.buffer.readBigInt64BE(this.addOffset(8)); | ||
| return this.readBuffer.readBigInt64BE(this.addOffset(8)); | ||
| } | ||
@@ -418,12 +429,4 @@ /** | ||
| writeLong(v) { | ||
| const lo = Number(v & BigInt(0xffffffff)); | ||
| this.binary[this.writeIndex + 7] = lo; | ||
| this.binary[this.writeIndex + 6] = lo >> 8; | ||
| this.binary[this.writeIndex + 5] = lo >> 16; | ||
| this.binary[this.writeIndex + 4] = lo >> 24; | ||
| const hi = Number((v >> BigInt(32)) & BigInt(0xffffffff)); | ||
| this.binary[this.writeIndex + 3] = hi; | ||
| this.binary[this.writeIndex + 2] = hi >> 8; | ||
| this.binary[this.writeIndex + 1] = hi >> 16; | ||
| this.binary[this.writeIndex] = hi >> 24; | ||
| this.ensureCapacity(8); | ||
| this.writeBuffer.writeBigInt64BE(v, this.writeIndex); | ||
| this.writeIndex += 8; | ||
@@ -437,19 +440,12 @@ } | ||
| this.doReadAssertions(8); | ||
| return this.buffer.readBigInt64LE(this.addOffset(8)); | ||
| return this.readBuffer.readBigInt64LE(this.addOffset(8)); | ||
| } | ||
| /** | ||
| * Writes a 64 bit (8 bytes) signed big-endian number. | ||
| * Writes a 64 bit (8 bytes) signed little-endian number. | ||
| * @param {bigint} v | ||
| */ | ||
| writeLongLE(v) { | ||
| const lo = Number(v & BigInt(0xffffffff)); | ||
| this.binary[this.writeIndex++] = lo; | ||
| this.binary[this.writeIndex++] = lo >> 8; | ||
| this.binary[this.writeIndex++] = lo >> 16; | ||
| this.binary[this.writeIndex++] = lo >> 24; | ||
| const hi = Number((v >> BigInt(32)) & BigInt(0xffffffff)); | ||
| this.binary[this.writeIndex++] = hi; | ||
| this.binary[this.writeIndex++] = hi >> 8; | ||
| this.binary[this.writeIndex++] = hi >> 16; | ||
| this.binary[this.writeIndex++] = hi >> 24; | ||
| this.ensureCapacity(8); | ||
| this.writeBuffer.writeBigInt64LE(v, this.writeIndex); | ||
| this.writeIndex += 8; | ||
| } | ||
@@ -462,3 +458,3 @@ /** | ||
| this.doReadAssertions(8); | ||
| return this.buffer.readBigUInt64BE(this.addOffset(8)); | ||
| return this.readBuffer.readBigUInt64BE(this.addOffset(8)); | ||
| } | ||
@@ -470,12 +466,4 @@ /** | ||
| writeUnsignedLong(v) { | ||
| const lo = Number(v & BigInt(0xffffffff)); | ||
| this.binary[this.writeIndex + 7] = lo; | ||
| this.binary[this.writeIndex + 6] = lo >> 8; | ||
| this.binary[this.writeIndex + 5] = lo >> 16; | ||
| this.binary[this.writeIndex + 4] = lo >> 24; | ||
| const hi = Number((v >> BigInt(32)) & BigInt(0xffffffff)); | ||
| this.binary[this.writeIndex + 3] = hi; | ||
| this.binary[this.writeIndex + 2] = hi >> 8; | ||
| this.binary[this.writeIndex + 1] = hi >> 16; | ||
| this.binary[this.writeIndex] = hi >> 24; | ||
| this.ensureCapacity(8); | ||
| this.writeBuffer.writeBigUInt64BE(v, this.writeIndex); | ||
| this.writeIndex += 8; | ||
@@ -489,19 +477,12 @@ } | ||
| this.doReadAssertions(8); | ||
| return this.buffer.readBigUInt64LE(this.addOffset(8)); | ||
| return this.readBuffer.readBigUInt64LE(this.addOffset(8)); | ||
| } | ||
| /** | ||
| * Writes a 64 bit (8 bytes) unsigned big-endian number. | ||
| * Writes a 64 bit (8 bytes) unsigned little-endian number. | ||
| * @param {bigint} v | ||
| */ | ||
| writeUnsignedLongLE(v) { | ||
| const lo = Number(v & BigInt(0xffffffff)); | ||
| this.binary[this.writeIndex++] = lo; | ||
| this.binary[this.writeIndex++] = lo >> 8; | ||
| this.binary[this.writeIndex++] = lo >> 16; | ||
| this.binary[this.writeIndex++] = lo >> 24; | ||
| const hi = Number((v >> BigInt(32)) & BigInt(0xffffffff)); | ||
| this.binary[this.writeIndex++] = hi; | ||
| this.binary[this.writeIndex++] = hi >> 8; | ||
| this.binary[this.writeIndex++] = hi >> 16; | ||
| this.binary[this.writeIndex++] = hi >> 24; | ||
| this.ensureCapacity(8); | ||
| this.writeBuffer.writeBigUInt64LE(v, this.writeIndex); | ||
| this.writeIndex += 8; | ||
| } | ||
@@ -530,9 +511,9 @@ /** | ||
| readUnsignedVarInt() { | ||
| (0, assert_1.default)(this.buffer != null, 'Reading on empty buffer!'); | ||
| (0, assert_1.default)(this.readBuffer != null, 'Reading on empty buffer!'); | ||
| let value = 0; | ||
| for (let i = 0; i <= 28; i += 7) { | ||
| if (typeof this.buffer[this.readIndex] === 'undefined') { | ||
| if (typeof this.readBuffer[this.readIndex] === 'undefined') { | ||
| throw new Error('No bytes left in buffer'); | ||
| } | ||
| let b = this.readByte(); | ||
| const b = this.readBuffer[this.readIndex++]; | ||
| value |= (b & 0x7f) << i; | ||
@@ -581,3 +562,3 @@ if ((b & 0x80) === 0) { | ||
| } | ||
| const b = this.readByte(); | ||
| const b = this.readBuffer[this.readIndex++]; | ||
| value |= (BigInt(b) & 0x7fn) << BigInt(i); | ||
@@ -607,2 +588,22 @@ if ((b & 0x80) === 0) { | ||
| /** | ||
| * Ensures write buffer has enough capacity, grows if needed. | ||
| * @param {number} needed | ||
| */ | ||
| ensureCapacity(needed) { | ||
| const required = this.writeIndex + needed; | ||
| if (required > this.writeCapacity) { | ||
| // Initial allocation or growth | ||
| const newCapacity = this.writeCapacity === 0 | ||
| ? Math.max(256, required) // Initial: 256 or required size | ||
| : Math.max(required, this.writeCapacity << 1); // Growth: 2x | ||
| const newBuffer = Buffer.allocUnsafe(newCapacity); | ||
| // Copy existing data if any | ||
| if (this.writeBuffer !== null && this.writeIndex > 0) { | ||
| this.writeBuffer.copy(newBuffer, 0, 0, this.writeIndex); | ||
| } | ||
| this.writeBuffer = newBuffer; | ||
| this.writeCapacity = newCapacity; | ||
| } | ||
| } | ||
| /** | ||
| * Increases the write offset by the given length. | ||
@@ -619,5 +620,5 @@ * @param {number} length | ||
| feof() { | ||
| if (!this.buffer) | ||
| if (!this.readBuffer) | ||
| throw new Error('Buffer is write only!'); | ||
| return typeof this.buffer[this.readIndex] === 'undefined'; | ||
| return typeof this.readBuffer[this.readIndex] === 'undefined'; | ||
| } | ||
@@ -629,6 +630,6 @@ /** | ||
| readRemaining() { | ||
| if (!this.buffer) | ||
| if (!this.readBuffer) | ||
| throw new Error('Buffer is write only!'); | ||
| const buf = this.buffer.slice(this.readIndex); | ||
| this.readIndex = this.buffer.byteLength; | ||
| const buf = this.readBuffer.subarray(this.readIndex); | ||
| this.readIndex = this.readBuffer.byteLength; | ||
| return buf; | ||
@@ -647,20 +648,60 @@ } | ||
| * @returns {Buffer} | ||
| * @deprecated | ||
| * @see getReadBuffer() | ||
| * @see getWriteBuffer() | ||
| */ | ||
| getBuffer() { | ||
| return this.buffer !== null ? this.buffer : Buffer.from(this.binary); | ||
| return this.readBuffer !== null | ||
| ? this.readBuffer | ||
| : this.writeBuffer.subarray(0, this.writeIndex); | ||
| } | ||
| /** | ||
| * Returns the read buffer if available. | ||
| * @returns {Buffer} | ||
| */ | ||
| getReadBuffer() { | ||
| return this.readBuffer; | ||
| } | ||
| /** | ||
| * Returns the write buffer. | ||
| * @returns {Buffer} | ||
| */ | ||
| getWriteBuffer() { | ||
| return this.writeBuffer.subarray(0, this.writeIndex); | ||
| } | ||
| /** | ||
| * Sets the buffer for reading. | ||
| * make sure to reset the reading index! | ||
| * @param buf - The new Buffer. | ||
| * @deprecated | ||
| */ | ||
| setBuffer(buf) { | ||
| this.buffer = buf; | ||
| this.readBuffer = buf; | ||
| } | ||
| /** | ||
| * Sets the buffer for reading. | ||
| * @param buf - The new Buffer. | ||
| * @param rIndex - The new read index (default: 0, pass -1 to keep current). | ||
| */ | ||
| setReadBuffer(buf, rIndex = 0) { | ||
| this.readBuffer = buf; | ||
| if (rIndex >= 0) | ||
| this.readIndex = rIndex; | ||
| } | ||
| /** | ||
| * Sets the buffer for writing. | ||
| * @param buf - The new Buffer. | ||
| * @param wIndex - The new write index (default: 0, pass -1 to keep current). | ||
| */ | ||
| setWriteBuffer(buf, wIndex = 0) { | ||
| this.writeBuffer = buf; | ||
| if (wIndex >= 0) | ||
| this.writeIndex = wIndex; | ||
| this.writeCapacity = buf.byteLength; | ||
| } | ||
| /** | ||
| * Clears the whole BinaryStream instance. | ||
| */ | ||
| clear() { | ||
| this.buffer = null; | ||
| this.binary = []; | ||
| this.readBuffer = null; | ||
| this.readIndex = 0; | ||
@@ -675,4 +716,3 @@ this.writeIndex = 0; | ||
| reuse(buf) { | ||
| this.buffer = buf; | ||
| this.binary = []; | ||
| this.readBuffer = buf; | ||
| this.readIndex = 0; | ||
@@ -686,3 +726,3 @@ this.writeIndex = 0; | ||
| setReadIndex(index) { | ||
| (0, assert_1.default)(index > 0, 'Index must be a positive integer'); | ||
| (0, assert_1.default)(index >= 0, 'Index must be non-negative'); | ||
| this.readIndex = index; | ||
@@ -695,3 +735,3 @@ } | ||
| setWriteIndex(index) { | ||
| (0, assert_1.default)(index > 0, 'Index must be a positive integer'); | ||
| (0, assert_1.default)(index >= 0, 'Index must be non-negative'); | ||
| this.writeIndex = index; | ||
@@ -718,4 +758,4 @@ } | ||
| doReadAssertions(byteLength) { | ||
| (0, assert_1.default)(this.buffer !== null, 'Cannot read without buffer data!'); | ||
| (0, assert_1.default)(this.buffer.byteLength >= byteLength, 'Cannot read without buffer data!'); | ||
| (0, assert_1.default)(this.readBuffer !== null, 'Cannot read without buffer data!'); | ||
| (0, assert_1.default)(this.readBuffer.byteLength >= byteLength, 'Cannot read without buffer data!'); | ||
| } | ||
@@ -722,0 +762,0 @@ /** |
@@ -1,7 +0,7 @@ | ||
| /// <reference types="node" /> | ||
| export default class BinaryStream { | ||
| private binary; | ||
| private buffer; | ||
| private writeBuffer; | ||
| private readBuffer; | ||
| private readIndex; | ||
| private writeIndex; | ||
| private writeCapacity; | ||
| /** | ||
@@ -69,3 +69,3 @@ * Creates a new BinaryStream instance. | ||
| /** | ||
| * Writes a 16 bit (2 bytes) signed big-endian number. | ||
| * Writes a 16 bit (2 bytes) signed little-endian number. | ||
| * @param {number} v | ||
@@ -230,3 +230,3 @@ */ | ||
| /** | ||
| * Writes a 64 bit (8 bytes) signed big-endian number. | ||
| * Writes a 64 bit (8 bytes) signed little-endian number. | ||
| * @param {bigint} v | ||
@@ -251,3 +251,3 @@ */ | ||
| /** | ||
| * Writes a 64 bit (8 bytes) unsigned big-endian number. | ||
| * Writes a 64 bit (8 bytes) unsigned little-endian number. | ||
| * @param {bigint} v | ||
@@ -297,2 +297,7 @@ */ | ||
| /** | ||
| * Ensures write buffer has enough capacity, grows if needed. | ||
| * @param {number} needed | ||
| */ | ||
| private ensureCapacity; | ||
| /** | ||
| * Increases the write offset by the given length. | ||
@@ -320,11 +325,37 @@ * @param {number} length | ||
| * @returns {Buffer} | ||
| * @deprecated | ||
| * @see getReadBuffer() | ||
| * @see getWriteBuffer() | ||
| */ | ||
| getBuffer(): Buffer; | ||
| /** | ||
| * Returns the read buffer if available. | ||
| * @returns {Buffer} | ||
| */ | ||
| getReadBuffer(): Buffer | null; | ||
| /** | ||
| * Returns the write buffer. | ||
| * @returns {Buffer} | ||
| */ | ||
| getWriteBuffer(): Buffer; | ||
| /** | ||
| * Sets the buffer for reading. | ||
| * make sure to reset the reading index! | ||
| * @param buf - The new Buffer. | ||
| * @deprecated | ||
| */ | ||
| setBuffer(buf: Buffer): void; | ||
| /** | ||
| * Sets the buffer for reading. | ||
| * @param buf - The new Buffer. | ||
| * @param rIndex - The new read index (default: 0, pass -1 to keep current). | ||
| */ | ||
| setReadBuffer(buf: Buffer, rIndex?: number): void; | ||
| /** | ||
| * Sets the buffer for writing. | ||
| * @param buf - The new Buffer. | ||
| * @param wIndex - The new write index (default: 0, pass -1 to keep current). | ||
| */ | ||
| setWriteBuffer(buf: Buffer, wIndex?: number): void; | ||
| /** | ||
| * Clears the whole BinaryStream instance. | ||
@@ -331,0 +362,0 @@ */ |
+180
-140
@@ -9,6 +9,7 @@ import assert from 'assert'; | ||
| constructor(buffer, offset = 0) { | ||
| this.binary = []; | ||
| this.buffer = null; | ||
| this.writeBuffer = null; | ||
| this.readBuffer = null; | ||
| this.writeIndex = 0; | ||
| this.buffer = buffer ?? null; // Keep this instance for reading | ||
| this.writeCapacity = 0; | ||
| this.readBuffer = buffer ?? null; // Keep this instance for reading | ||
| this.readIndex = offset; | ||
@@ -22,6 +23,7 @@ } | ||
| this.doReadAssertions(len); | ||
| return this.buffer.slice(this.readIndex, (this.readIndex += len)); | ||
| return this.readBuffer.subarray(this.readIndex, (this.readIndex += len)); | ||
| } | ||
| write(buf) { | ||
| this.binary = [...this.binary, ...buf]; | ||
| this.ensureCapacity(buf.byteLength); | ||
| buf.copy(this.writeBuffer, this.writeIndex); | ||
| this.writeIndex += buf.byteLength; | ||
@@ -35,3 +37,3 @@ } | ||
| this.doReadAssertions(1); | ||
| return this.buffer.readUInt8(this.readIndex++); | ||
| return this.readBuffer.readUInt8(this.readIndex++); | ||
| } | ||
@@ -43,4 +45,4 @@ /** | ||
| writeByte(v) { | ||
| v &= 0xff; | ||
| this.binary[this.writeIndex++] = v; | ||
| this.ensureCapacity(1); | ||
| this.writeBuffer[this.writeIndex++] = v & 0xff; | ||
| } | ||
@@ -53,3 +55,3 @@ /** | ||
| this.doReadAssertions(1); | ||
| return this.buffer.readInt8(this.readIndex++); | ||
| return this.readBuffer.readInt8(this.readIndex++); | ||
| } | ||
@@ -61,5 +63,5 @@ /** | ||
| writeSignedByte(v) { | ||
| if (v < 0) | ||
| v = 0xff + v + 1; | ||
| this.binary[this.writeIndex++] = v & 0xff; | ||
| this.ensureCapacity(1); | ||
| this.writeBuffer[this.writeIndex++] = | ||
| (v < 0 ? 0xff + v + 1 : v) & 0xff; | ||
| } | ||
@@ -72,3 +74,3 @@ /** | ||
| this.doReadAssertions(1); | ||
| return !!this.readByte(); | ||
| return this.readBuffer[this.readIndex++] !== 0; | ||
| } | ||
@@ -80,3 +82,4 @@ /** | ||
| writeBoolean(v) { | ||
| this.writeByte(+v); | ||
| this.ensureCapacity(1); | ||
| this.writeBuffer[this.writeIndex++] = v ? 1 : 0; | ||
| } | ||
@@ -89,3 +92,3 @@ /** | ||
| this.doReadAssertions(2); | ||
| return this.buffer.readInt16BE(this.addOffset(2)); | ||
| return this.readBuffer.readInt16BE(this.addOffset(2)); | ||
| } | ||
@@ -98,4 +101,5 @@ /** | ||
| this.doWriteAssertions(v, -32768, 32767); | ||
| this.writeByte(v >> 8); | ||
| this.writeByte(v); | ||
| this.ensureCapacity(2); | ||
| this.writeBuffer[this.writeIndex++] = (v >> 8) & 0xff; | ||
| this.writeBuffer[this.writeIndex++] = v & 0xff; | ||
| } | ||
@@ -108,6 +112,6 @@ /** | ||
| this.doReadAssertions(2); | ||
| return this.buffer.readInt16LE(this.addOffset(2)); | ||
| return this.readBuffer.readInt16LE(this.addOffset(2)); | ||
| } | ||
| /** | ||
| * Writes a 16 bit (2 bytes) signed big-endian number. | ||
| * Writes a 16 bit (2 bytes) signed little-endian number. | ||
| * @param {number} v | ||
@@ -117,4 +121,5 @@ */ | ||
| this.doWriteAssertions(v, -32768, 32767); | ||
| this.writeByte(v); | ||
| this.writeByte(v >> 8); | ||
| this.ensureCapacity(2); | ||
| this.writeBuffer[this.writeIndex++] = v & 0xff; | ||
| this.writeBuffer[this.writeIndex++] = (v >> 8) & 0xff; | ||
| } | ||
@@ -127,3 +132,3 @@ /** | ||
| this.doReadAssertions(2); | ||
| return this.buffer.readUInt16BE(this.addOffset(2)); | ||
| return this.readBuffer.readUInt16BE(this.addOffset(2)); | ||
| } | ||
@@ -136,4 +141,5 @@ /** | ||
| this.doWriteAssertions(v, 0, 65535); | ||
| this.writeByte(v >>> 8); | ||
| this.writeByte(v); | ||
| this.ensureCapacity(2); | ||
| this.writeBuffer[this.writeIndex++] = (v >>> 8) & 0xff; | ||
| this.writeBuffer[this.writeIndex++] = v & 0xff; | ||
| } | ||
@@ -146,3 +152,3 @@ /** | ||
| this.doReadAssertions(2); | ||
| return this.buffer.readUInt16LE(this.addOffset(2)); | ||
| return this.readBuffer.readUInt16LE(this.addOffset(2)); | ||
| } | ||
@@ -155,4 +161,5 @@ /** | ||
| this.doWriteAssertions(v, 0, 65535); | ||
| this.writeByte(v); | ||
| this.writeByte(v >>> 8); | ||
| this.ensureCapacity(2); | ||
| this.writeBuffer[this.writeIndex++] = v & 0xff; | ||
| this.writeBuffer[this.writeIndex++] = (v >>> 8) & 0xff; | ||
| } | ||
@@ -165,3 +172,3 @@ /** | ||
| this.doReadAssertions(3); | ||
| return this.buffer.readIntBE(this.addOffset(3), 3); | ||
| return this.readBuffer.readIntBE(this.addOffset(3), 3); | ||
| } | ||
@@ -174,5 +181,6 @@ /** | ||
| this.doWriteAssertions(v, -8388608, 8388607); | ||
| this.writeByte((v & 0xff0000) >> 16); // msb | ||
| this.writeByte((v & 0x00ff00) >> 8); // mib | ||
| this.writeByte(v & 0x0000ff); // lsb | ||
| this.ensureCapacity(3); | ||
| this.writeBuffer[this.writeIndex++] = (v >> 16) & 0xff; | ||
| this.writeBuffer[this.writeIndex++] = (v >> 8) & 0xff; | ||
| this.writeBuffer[this.writeIndex++] = v & 0xff; | ||
| } | ||
@@ -185,3 +193,3 @@ /** | ||
| this.doReadAssertions(3); | ||
| return this.buffer.readIntLE(this.addOffset(3), 3); | ||
| return this.readBuffer.readIntLE(this.addOffset(3), 3); | ||
| } | ||
@@ -194,5 +202,6 @@ /** | ||
| this.doWriteAssertions(v, -8388608, 8388607); | ||
| this.writeByte(v & 0x0000ff); | ||
| this.writeByte((v & 0x00ff00) >> 8); | ||
| this.writeByte((v & 0xff0000) >> 16); | ||
| this.ensureCapacity(3); | ||
| this.writeBuffer[this.writeIndex++] = v & 0xff; | ||
| this.writeBuffer[this.writeIndex++] = (v >> 8) & 0xff; | ||
| this.writeBuffer[this.writeIndex++] = (v >> 16) & 0xff; | ||
| } | ||
@@ -205,3 +214,3 @@ /** | ||
| this.doReadAssertions(3); | ||
| return this.buffer.readUIntBE(this.addOffset(3), 3); | ||
| return this.readBuffer.readUIntBE(this.addOffset(3), 3); | ||
| } | ||
@@ -214,5 +223,6 @@ /** | ||
| this.doWriteAssertions(v, 0, 16777215); | ||
| this.writeByte((v & 0xff0000) >>> 16); // msb | ||
| this.writeByte((v & 0x00ff00) >>> 8); // mib | ||
| this.writeByte(v & 0x0000ff); // lsb | ||
| this.ensureCapacity(3); | ||
| this.writeBuffer[this.writeIndex++] = (v >>> 16) & 0xff; | ||
| this.writeBuffer[this.writeIndex++] = (v >>> 8) & 0xff; | ||
| this.writeBuffer[this.writeIndex++] = v & 0xff; | ||
| } | ||
@@ -225,3 +235,3 @@ /** | ||
| this.doReadAssertions(3); | ||
| return this.buffer.readUIntLE(this.addOffset(3), 3); | ||
| return this.readBuffer.readUIntLE(this.addOffset(3), 3); | ||
| } | ||
@@ -234,5 +244,6 @@ /** | ||
| this.doWriteAssertions(v, 0, 16777215); | ||
| this.writeByte(v & 0x0000ff); | ||
| this.writeByte((v & 0x00ff00) >>> 8); | ||
| this.writeByte((v & 0xff0000) >>> 16); | ||
| this.ensureCapacity(3); | ||
| this.writeBuffer[this.writeIndex++] = v & 0xff; | ||
| this.writeBuffer[this.writeIndex++] = (v >>> 8) & 0xff; | ||
| this.writeBuffer[this.writeIndex++] = (v >>> 16) & 0xff; | ||
| } | ||
@@ -245,3 +256,3 @@ /** | ||
| this.doReadAssertions(4); | ||
| return this.buffer.readInt32BE(this.addOffset(4)); | ||
| return this.readBuffer.readInt32BE(this.addOffset(4)); | ||
| } | ||
@@ -253,9 +264,6 @@ /** | ||
| writeInt(v) { | ||
| if (v < 0) | ||
| v = v & (0xffffffff + v + 1); | ||
| this.doWriteAssertions(v, -2147483648, 2147483647); | ||
| this.writeByte(v >> 24); | ||
| this.writeByte(v >> 16); | ||
| this.writeByte(v >> 8); | ||
| this.writeByte(v); | ||
| this.ensureCapacity(4); | ||
| this.writeBuffer.writeInt32BE(v, this.writeIndex); | ||
| this.writeIndex += 4; | ||
| } | ||
@@ -268,3 +276,3 @@ /** | ||
| this.doReadAssertions(4); | ||
| return this.buffer.readIntLE(this.addOffset(4), 4); | ||
| return this.readBuffer.readIntLE(this.addOffset(4), 4); | ||
| } | ||
@@ -276,9 +284,6 @@ /** | ||
| writeIntLE(v) { | ||
| if (v < 0) | ||
| v = v & (0xffffffff + v + 1); | ||
| this.doWriteAssertions(v, -2147483648, 2147483647); | ||
| this.writeByte(v); | ||
| this.writeByte(v >> 8); | ||
| this.writeByte(v >> 16); | ||
| this.writeByte(v >> 24); | ||
| this.ensureCapacity(4); | ||
| this.writeBuffer.writeInt32LE(v, this.writeIndex); | ||
| this.writeIndex += 4; | ||
| } | ||
@@ -291,3 +296,3 @@ /** | ||
| this.doReadAssertions(4); | ||
| return this.buffer.readUInt32BE(this.addOffset(4)); | ||
| return this.readBuffer.readUInt32BE(this.addOffset(4)); | ||
| } | ||
@@ -300,6 +305,5 @@ /** | ||
| this.doWriteAssertions(v, 0, 4294967295); | ||
| this.writeByte(v >>> 24); | ||
| this.writeByte(v >>> 16); | ||
| this.writeByte(v >>> 8); | ||
| this.writeByte(v); | ||
| this.ensureCapacity(4); | ||
| this.writeBuffer.writeUInt32BE(v, this.writeIndex); | ||
| this.writeIndex += 4; | ||
| } | ||
@@ -312,3 +316,3 @@ /** | ||
| this.doReadAssertions(4); | ||
| return this.buffer.readUInt32LE(this.addOffset(4)); | ||
| return this.readBuffer.readUInt32LE(this.addOffset(4)); | ||
| } | ||
@@ -321,6 +325,5 @@ /** | ||
| this.doWriteAssertions(v, 0, 4294967295); | ||
| this.writeByte(v); | ||
| this.writeByte(v >>> 8); | ||
| this.writeByte(v >>> 16); | ||
| this.writeByte(v >>> 24); | ||
| this.ensureCapacity(4); | ||
| this.writeBuffer.writeUInt32LE(v, this.writeIndex); | ||
| this.writeIndex += 4; | ||
| } | ||
@@ -333,3 +336,3 @@ /** | ||
| this.doReadAssertions(4); | ||
| return this.buffer.readFloatBE(this.addOffset(4)); | ||
| return this.readBuffer.readFloatBE(this.addOffset(4)); | ||
| } | ||
@@ -342,3 +345,5 @@ /** | ||
| this.doWriteAssertions(v, -3.4028234663852886e38, +3.4028234663852886e38); | ||
| this.write(new Uint8Array(new Float32Array([v]).buffer).reverse()); | ||
| this.ensureCapacity(4); | ||
| this.writeBuffer.writeFloatBE(v, this.writeIndex); | ||
| this.writeIndex += 4; | ||
| } | ||
@@ -351,3 +356,3 @@ /** | ||
| this.doReadAssertions(4); | ||
| return this.buffer.readFloatLE(this.addOffset(4)); | ||
| return this.readBuffer.readFloatLE(this.addOffset(4)); | ||
| } | ||
@@ -360,3 +365,5 @@ /** | ||
| this.doWriteAssertions(v, -3.4028234663852886e38, +3.4028234663852886e38); | ||
| this.write(new Uint8Array(new Float32Array([v]).buffer)); | ||
| this.ensureCapacity(4); | ||
| this.writeBuffer.writeFloatLE(v, this.writeIndex); | ||
| this.writeIndex += 4; | ||
| } | ||
@@ -369,3 +376,3 @@ /** | ||
| this.doReadAssertions(8); | ||
| return this.buffer.readDoubleBE(this.addOffset(8)); | ||
| return this.readBuffer.readDoubleBE(this.addOffset(8)); | ||
| } | ||
@@ -378,3 +385,5 @@ /** | ||
| this.doWriteAssertions(v, -1.7976931348623157e308, +1.7976931348623157e308); | ||
| this.write(new Uint8Array(new Float64Array([v]).buffer).reverse()); | ||
| this.ensureCapacity(8); | ||
| this.writeBuffer.writeDoubleBE(v, this.writeIndex); | ||
| this.writeIndex += 8; | ||
| } | ||
@@ -387,3 +396,3 @@ /** | ||
| this.doReadAssertions(8); | ||
| return this.buffer.readDoubleLE(this.addOffset(8)); | ||
| return this.readBuffer.readDoubleLE(this.addOffset(8)); | ||
| } | ||
@@ -396,3 +405,5 @@ /** | ||
| this.doWriteAssertions(v, -1.7976931348623157e308, +1.7976931348623157e308); | ||
| this.write(new Uint8Array(new Float64Array([v]).buffer)); | ||
| this.ensureCapacity(8); | ||
| this.writeBuffer.writeDoubleLE(v, this.writeIndex); | ||
| this.writeIndex += 8; | ||
| } | ||
@@ -405,3 +416,3 @@ /** | ||
| this.doReadAssertions(8); | ||
| return this.buffer.readBigInt64BE(this.addOffset(8)); | ||
| return this.readBuffer.readBigInt64BE(this.addOffset(8)); | ||
| } | ||
@@ -413,12 +424,4 @@ /** | ||
| writeLong(v) { | ||
| const lo = Number(v & BigInt(0xffffffff)); | ||
| this.binary[this.writeIndex + 7] = lo; | ||
| this.binary[this.writeIndex + 6] = lo >> 8; | ||
| this.binary[this.writeIndex + 5] = lo >> 16; | ||
| this.binary[this.writeIndex + 4] = lo >> 24; | ||
| const hi = Number((v >> BigInt(32)) & BigInt(0xffffffff)); | ||
| this.binary[this.writeIndex + 3] = hi; | ||
| this.binary[this.writeIndex + 2] = hi >> 8; | ||
| this.binary[this.writeIndex + 1] = hi >> 16; | ||
| this.binary[this.writeIndex] = hi >> 24; | ||
| this.ensureCapacity(8); | ||
| this.writeBuffer.writeBigInt64BE(v, this.writeIndex); | ||
| this.writeIndex += 8; | ||
@@ -432,19 +435,12 @@ } | ||
| this.doReadAssertions(8); | ||
| return this.buffer.readBigInt64LE(this.addOffset(8)); | ||
| return this.readBuffer.readBigInt64LE(this.addOffset(8)); | ||
| } | ||
| /** | ||
| * Writes a 64 bit (8 bytes) signed big-endian number. | ||
| * Writes a 64 bit (8 bytes) signed little-endian number. | ||
| * @param {bigint} v | ||
| */ | ||
| writeLongLE(v) { | ||
| const lo = Number(v & BigInt(0xffffffff)); | ||
| this.binary[this.writeIndex++] = lo; | ||
| this.binary[this.writeIndex++] = lo >> 8; | ||
| this.binary[this.writeIndex++] = lo >> 16; | ||
| this.binary[this.writeIndex++] = lo >> 24; | ||
| const hi = Number((v >> BigInt(32)) & BigInt(0xffffffff)); | ||
| this.binary[this.writeIndex++] = hi; | ||
| this.binary[this.writeIndex++] = hi >> 8; | ||
| this.binary[this.writeIndex++] = hi >> 16; | ||
| this.binary[this.writeIndex++] = hi >> 24; | ||
| this.ensureCapacity(8); | ||
| this.writeBuffer.writeBigInt64LE(v, this.writeIndex); | ||
| this.writeIndex += 8; | ||
| } | ||
@@ -457,3 +453,3 @@ /** | ||
| this.doReadAssertions(8); | ||
| return this.buffer.readBigUInt64BE(this.addOffset(8)); | ||
| return this.readBuffer.readBigUInt64BE(this.addOffset(8)); | ||
| } | ||
@@ -465,12 +461,4 @@ /** | ||
| writeUnsignedLong(v) { | ||
| const lo = Number(v & BigInt(0xffffffff)); | ||
| this.binary[this.writeIndex + 7] = lo; | ||
| this.binary[this.writeIndex + 6] = lo >> 8; | ||
| this.binary[this.writeIndex + 5] = lo >> 16; | ||
| this.binary[this.writeIndex + 4] = lo >> 24; | ||
| const hi = Number((v >> BigInt(32)) & BigInt(0xffffffff)); | ||
| this.binary[this.writeIndex + 3] = hi; | ||
| this.binary[this.writeIndex + 2] = hi >> 8; | ||
| this.binary[this.writeIndex + 1] = hi >> 16; | ||
| this.binary[this.writeIndex] = hi >> 24; | ||
| this.ensureCapacity(8); | ||
| this.writeBuffer.writeBigUInt64BE(v, this.writeIndex); | ||
| this.writeIndex += 8; | ||
@@ -484,19 +472,12 @@ } | ||
| this.doReadAssertions(8); | ||
| return this.buffer.readBigUInt64LE(this.addOffset(8)); | ||
| return this.readBuffer.readBigUInt64LE(this.addOffset(8)); | ||
| } | ||
| /** | ||
| * Writes a 64 bit (8 bytes) unsigned big-endian number. | ||
| * Writes a 64 bit (8 bytes) unsigned little-endian number. | ||
| * @param {bigint} v | ||
| */ | ||
| writeUnsignedLongLE(v) { | ||
| const lo = Number(v & BigInt(0xffffffff)); | ||
| this.binary[this.writeIndex++] = lo; | ||
| this.binary[this.writeIndex++] = lo >> 8; | ||
| this.binary[this.writeIndex++] = lo >> 16; | ||
| this.binary[this.writeIndex++] = lo >> 24; | ||
| const hi = Number((v >> BigInt(32)) & BigInt(0xffffffff)); | ||
| this.binary[this.writeIndex++] = hi; | ||
| this.binary[this.writeIndex++] = hi >> 8; | ||
| this.binary[this.writeIndex++] = hi >> 16; | ||
| this.binary[this.writeIndex++] = hi >> 24; | ||
| this.ensureCapacity(8); | ||
| this.writeBuffer.writeBigUInt64LE(v, this.writeIndex); | ||
| this.writeIndex += 8; | ||
| } | ||
@@ -525,9 +506,9 @@ /** | ||
| readUnsignedVarInt() { | ||
| assert(this.buffer != null, 'Reading on empty buffer!'); | ||
| assert(this.readBuffer != null, 'Reading on empty buffer!'); | ||
| let value = 0; | ||
| for (let i = 0; i <= 28; i += 7) { | ||
| if (typeof this.buffer[this.readIndex] === 'undefined') { | ||
| if (typeof this.readBuffer[this.readIndex] === 'undefined') { | ||
| throw new Error('No bytes left in buffer'); | ||
| } | ||
| let b = this.readByte(); | ||
| const b = this.readBuffer[this.readIndex++]; | ||
| value |= (b & 0x7f) << i; | ||
@@ -576,3 +557,3 @@ if ((b & 0x80) === 0) { | ||
| } | ||
| const b = this.readByte(); | ||
| const b = this.readBuffer[this.readIndex++]; | ||
| value |= (BigInt(b) & 0x7fn) << BigInt(i); | ||
@@ -602,2 +583,22 @@ if ((b & 0x80) === 0) { | ||
| /** | ||
| * Ensures write buffer has enough capacity, grows if needed. | ||
| * @param {number} needed | ||
| */ | ||
| ensureCapacity(needed) { | ||
| const required = this.writeIndex + needed; | ||
| if (required > this.writeCapacity) { | ||
| // Initial allocation or growth | ||
| const newCapacity = this.writeCapacity === 0 | ||
| ? Math.max(256, required) // Initial: 256 or required size | ||
| : Math.max(required, this.writeCapacity << 1); // Growth: 2x | ||
| const newBuffer = Buffer.allocUnsafe(newCapacity); | ||
| // Copy existing data if any | ||
| if (this.writeBuffer !== null && this.writeIndex > 0) { | ||
| this.writeBuffer.copy(newBuffer, 0, 0, this.writeIndex); | ||
| } | ||
| this.writeBuffer = newBuffer; | ||
| this.writeCapacity = newCapacity; | ||
| } | ||
| } | ||
| /** | ||
| * Increases the write offset by the given length. | ||
@@ -614,5 +615,5 @@ * @param {number} length | ||
| feof() { | ||
| if (!this.buffer) | ||
| if (!this.readBuffer) | ||
| throw new Error('Buffer is write only!'); | ||
| return typeof this.buffer[this.readIndex] === 'undefined'; | ||
| return typeof this.readBuffer[this.readIndex] === 'undefined'; | ||
| } | ||
@@ -624,6 +625,6 @@ /** | ||
| readRemaining() { | ||
| if (!this.buffer) | ||
| if (!this.readBuffer) | ||
| throw new Error('Buffer is write only!'); | ||
| const buf = this.buffer.slice(this.readIndex); | ||
| this.readIndex = this.buffer.byteLength; | ||
| const buf = this.readBuffer.subarray(this.readIndex); | ||
| this.readIndex = this.readBuffer.byteLength; | ||
| return buf; | ||
@@ -642,20 +643,60 @@ } | ||
| * @returns {Buffer} | ||
| * @deprecated | ||
| * @see getReadBuffer() | ||
| * @see getWriteBuffer() | ||
| */ | ||
| getBuffer() { | ||
| return this.buffer !== null ? this.buffer : Buffer.from(this.binary); | ||
| return this.readBuffer !== null | ||
| ? this.readBuffer | ||
| : this.writeBuffer.subarray(0, this.writeIndex); | ||
| } | ||
| /** | ||
| * Returns the read buffer if available. | ||
| * @returns {Buffer} | ||
| */ | ||
| getReadBuffer() { | ||
| return this.readBuffer; | ||
| } | ||
| /** | ||
| * Returns the write buffer. | ||
| * @returns {Buffer} | ||
| */ | ||
| getWriteBuffer() { | ||
| return this.writeBuffer.subarray(0, this.writeIndex); | ||
| } | ||
| /** | ||
| * Sets the buffer for reading. | ||
| * make sure to reset the reading index! | ||
| * @param buf - The new Buffer. | ||
| * @deprecated | ||
| */ | ||
| setBuffer(buf) { | ||
| this.buffer = buf; | ||
| this.readBuffer = buf; | ||
| } | ||
| /** | ||
| * Sets the buffer for reading. | ||
| * @param buf - The new Buffer. | ||
| * @param rIndex - The new read index (default: 0, pass -1 to keep current). | ||
| */ | ||
| setReadBuffer(buf, rIndex = 0) { | ||
| this.readBuffer = buf; | ||
| if (rIndex >= 0) | ||
| this.readIndex = rIndex; | ||
| } | ||
| /** | ||
| * Sets the buffer for writing. | ||
| * @param buf - The new Buffer. | ||
| * @param wIndex - The new write index (default: 0, pass -1 to keep current). | ||
| */ | ||
| setWriteBuffer(buf, wIndex = 0) { | ||
| this.writeBuffer = buf; | ||
| if (wIndex >= 0) | ||
| this.writeIndex = wIndex; | ||
| this.writeCapacity = buf.byteLength; | ||
| } | ||
| /** | ||
| * Clears the whole BinaryStream instance. | ||
| */ | ||
| clear() { | ||
| this.buffer = null; | ||
| this.binary = []; | ||
| this.readBuffer = null; | ||
| this.readIndex = 0; | ||
@@ -670,4 +711,3 @@ this.writeIndex = 0; | ||
| reuse(buf) { | ||
| this.buffer = buf; | ||
| this.binary = []; | ||
| this.readBuffer = buf; | ||
| this.readIndex = 0; | ||
@@ -681,3 +721,3 @@ this.writeIndex = 0; | ||
| setReadIndex(index) { | ||
| assert(index > 0, 'Index must be a positive integer'); | ||
| assert(index >= 0, 'Index must be non-negative'); | ||
| this.readIndex = index; | ||
@@ -690,3 +730,3 @@ } | ||
| setWriteIndex(index) { | ||
| assert(index > 0, 'Index must be a positive integer'); | ||
| assert(index >= 0, 'Index must be non-negative'); | ||
| this.writeIndex = index; | ||
@@ -713,4 +753,4 @@ } | ||
| doReadAssertions(byteLength) { | ||
| assert(this.buffer !== null, 'Cannot read without buffer data!'); | ||
| assert(this.buffer.byteLength >= byteLength, 'Cannot read without buffer data!'); | ||
| assert(this.readBuffer !== null, 'Cannot read without buffer data!'); | ||
| assert(this.readBuffer.byteLength >= byteLength, 'Cannot read without buffer data!'); | ||
| } | ||
@@ -717,0 +757,0 @@ /** |
@@ -1,1 +0,1 @@ | ||
| {"type": "module"} | ||
| {"type":"module"} |
+22
-15
| { | ||
| "name": "@jsprismarine/jsbinaryutils", | ||
| "version": "5.5.3", | ||
| "version": "6.0.0", | ||
| "description": "Basic binary data managing tool written in TypeScript.", | ||
@@ -15,4 +15,12 @@ "main": "./dist/cjs/BinaryStream.js", | ||
| "test:watch": "jest --watchAll", | ||
| "prepublishOnly": "npm run test", | ||
| "build": "build.sh" | ||
| "lint": "eslint src/**/*.ts", | ||
| "lint:fix": "eslint src/**/*.ts --fix", | ||
| "format": "prettier --write src/**/*.ts", | ||
| "format:check": "prettier --check src/**/*.ts", | ||
| "prepare": "husky || true", | ||
| "prepublishOnly": "npm run test && npm run build", | ||
| "clean": "rimraf dist", | ||
| "build": "npm run clean && npm run build:cjs && npm run build:esm", | ||
| "build:cjs": "tsc --build tsconfig.cjs.json", | ||
| "build:esm": "tsc --build tsconfig.esm.json && node -e \"require('fs').writeFileSync('dist/esm/package.json', '{\\\"type\\\":\\\"module\\\"}')\"" | ||
| }, | ||
@@ -37,15 +45,14 @@ "files": [ | ||
| "devDependencies": { | ||
| "@types/jest": "^29.2.5", | ||
| "@types/node": "^18.11.3", | ||
| "husky": "^8.0.3", | ||
| "jest": "^29.3.1", | ||
| "prettier": "2.8.7", | ||
| "ts-jest": "^29.0.3", | ||
| "typescript": "4.9.5" | ||
| }, | ||
| "husky": { | ||
| "hooks": { | ||
| "pre-commit": "npm run test" | ||
| } | ||
| "@types/jest": "^30.0.0", | ||
| "@types/node": "^24.7.2", | ||
| "@typescript-eslint/eslint-plugin": "^8.46.1", | ||
| "@typescript-eslint/parser": "^8.46.1", | ||
| "eslint": "^9.37.0", | ||
| "husky": "^9.1.7", | ||
| "jest": "^30.2.0", | ||
| "prettier": "^3.6.2", | ||
| "rimraf": "^6.0.1", | ||
| "ts-jest": "^29.4.5", | ||
| "typescript": "^5.9.3" | ||
| } | ||
| } |
70315
4.67%9
28.57%2220
6.83%11
57.14%