@chickenjdk/byteutils
Version:
Some utilitys for working with binary data
526 lines (525 loc) • 28.2 kB
JavaScript
"use strict";
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var _writableBufferBase_isLe, _writableBuffer_chunkSize, _writableBuffer_buffers, _writableBuffer_used, _writableBufferFixedSize_buffer, _writableBufferFixedSize_used;
Object.defineProperty(exports, "__esModule", { value: true });
exports.writableBufferFixedSizeLE = exports.writableBufferFixedSize = exports.writableBufferLE = exports.writableBuffer = exports.writableBufferBase = void 0;
const utf8tools_1 = require("./utf8tools");
const common_1 = require("./common");
const constants = {
// 11111111111111111111111111111111
allOnes: 0xffffffff,
// 10000000000000000000000000000000
oneThen31Zeros: 0x80000000,
// 11111111111111111111111100000000
allOnesButLastByte: 0xffffff00,
};
// Returns not typed for non-abstract/abstracet alias methods
class writableBufferBase {
constructor() {
// Little-endian support: <-
/**
* Write data to the buffer (writes data that was origionaly in BE format to the endianness of the buffer)
* @param value The data to write
*/
this.writeArrayEndian = this.writeArray;
/**
* Write data to the buffer backwards (writes data that was origionaly in LE format to the endianness of the buffer, I know that "backwards" is a little opinionated but the class was origionaly BE-only and I did not want to change too mutch)
* @param value The data to write
*/
this.writeArrayBackwardsEndian = this.writeArrayBackwards;
/**
* Write a Uint8Array to the buffer (for the endian)
* Alias for .write because .write can handle Uint8Arrays. This exsists to have the similar naming of methods as readableBuffer's methods
*/
this.writeUint8ArrayEndian = this.writeUint8Array;
/**
* Write a Uint8Array to the buffer backwars (for the endian)
* Alias for .write because .write can handle Uint8Arrays. This exsists to have the similar naming of methods as readableBuffer's methods
*/
this.writeUint8ArrayBackwardsEndian = this.writeUint8ArrayBackwards;
_writableBufferBase_isLe.set(this, false);
}
/**
* Write a writeable buffer storing data to the buffer
*/
writeWriteableBuffer(value) {
return this.writeUint8Array(value.buffer);
}
/**
* If the buffer is little endian
*/
get isLe() {
return __classPrivateFieldGet(this, _writableBufferBase_isLe, "f");
}
/**
* If the buffer is little endian
*/
set isLe(isLe) {
if (isLe) {
this.writeArrayEndian = this.writeArrayBackwards;
this.writeArrayBackwardsEndian = this.writeArray;
this.writeUint8ArrayEndian = this.writeUint8ArrayBackwards;
this.writeUint8ArrayBackwardsEndian = this.writeUint8Array;
}
else {
this.writeArrayEndian = this.writeArray;
this.writeArrayBackwardsEndian = this.writeArrayBackwards;
this.writeUint8ArrayEndian = this.writeUint8Array;
this.writeUint8ArrayBackwardsEndian = this.writeUint8ArrayBackwards;
}
__classPrivateFieldSet(this, _writableBufferBase_isLe, isLe, "f");
}
// ->
// REMEMBER: return type is not specifyed so the isasync param properly propigates
/**
* Calculate the minimum length of an unsigned integer in bytes.
* WARNING: No unssigned ints above 4294967295 (2^32 - 1) are supported, so this will not work for those.
* This is due to the limitations of bitwise operators. You can write numbers higher than that via writeUnsignedIntBigint, but this function will not work for them.
* @remarks
* This function calculates the minimum number of bytes needed to represent an unsigned integer in binary format.
* It uses the `Math.clz32` function to count the number of leading zeros in the binary representation of the value.
* The result is rounded up to the nearest byte.
* @param value The value to check
* @returns The calculated minimum length in bytes
*/
minLengthOfUnsignedInt(value) {
return Math.ceil((32 - Math.clz32(value)) / 8);
}
/**
* Write an unsigned integer to the buffer
* @param value The unsigned int to write
* @param bytes How many bytes the unsined int is (If not provided, it will write the minimum length)
* @returns How many bytes were written (Same as bytes parameter if provided)
*/
writeUnsignedInt(value, bytes) {
let mask = 0b11111111;
let out = [];
let i = -8;
const bits = bytes * 8;
while ((i += 8) < bits) {
out.unshift((mask & value) >>> i);
mask <<= 8;
}
return (0, common_1.wrapForPromise)(this.writeArrayEndian(out), bytes);
}
/**
* Write an unsigned integer to the buffer
* @param value The unsigned int to write (a bigint)
* @param bytes How many bytes the unsined int is (If not provided, it will write the minimum length)
* @returns How many bytes were written (Same as bytes parameter)
*/
writeUnsignedIntBigint(value, bytes) {
let mask = 255n;
let out = [];
let i = -8n;
const bits = bytes * 8;
while ((i += 8n) < bits) {
out.unshift(Number((mask & value) >> i));
mask <<= 8n;
}
return (0, common_1.wrapForPromise)(this.writeArrayEndian(out), bytes);
}
/**
* Calculate the minimum length of a two's complement in bytes.
* WARNING: No two's complements above 4278190079 or below -4278190079 (2^31 - 1) are supported, so this will not work for those.
* This is due to the limitations of bitwise operators. You can write numbers higher than that via writeTwosComplementBigint, but this function will not work for them.
* @remarks
* This function calculates the minimum number of bytes needed to represent an two's complement in binary format.
* It uses the `Math.clz32` function to count the number of leading zeros in the binary representation of the value.
* It subtracts this from 33 (equivilent to the number of bits in the two's complement +1 to account for the sign) to get the number of bits needed.
* The result is rounded up to the nearest byte.
* @param value The value to check
* @returns The calculated minimum length in bytes
*/
minLengthOfTwosComplement(value) {
// Length of the number as a unsigned int+1 is the bytes (to take into account the sign bit)
// This is then converted to bytes
return Math.ceil((33 - Math.clz32(Math.abs(value))) / 8);
}
/**
* Write a twos complement to the buffer
* @param value The number to encode
* @param bytes How long the twos complement to be written is in bytes
* @returns How many bytes were written (Same as bytes parameter if provided)
*/
writeTwosComplement(value, bytes) {
return (0, common_1.wrapForPromise)(this.writeUnsignedInt(value < 0
? ((constants.allOnes >>> ((4 - bytes) * 8)) & value) >>> 0
: value, bytes), bytes);
}
/**
* Write a twos complement to the buffer (From a bigint)
* @param value The number to encode
* @param bytes How long the twos complement to be written is in bytes
* @returns How many bytes were written (Same as bytes parameter)
*/
writeTwosComplementBigint(value, bytes) {
return (0, common_1.wrapForPromise)(this.writeUnsignedIntBigint(value < 0n ? ~(-1n << BigInt(bytes * 8)) & value : value, bytes), bytes);
}
/**
* Write a twos complement to the buffer (one byte)
* @param value The number to encode
* @returns How many bytes were written (1)
*/
writeTwosComplementByte(value) {
return (0, common_1.wrapForPromise)(1, this.push((value & 0b11111111) >>> 0));
}
/**
* Write twos complements to the buffer (one byte each)
* @param values The numbers to encode
* @returns How many bytes were written (Same as values.length)
*/
writeTwosComplementByteArray(values) {
return (0, common_1.wrapForAsyncCallArr)(this.writeTwosComplementByte.bind(this), values.map((value) => [value]), values.length);
}
/**
* Write a float to the buffer
* @param value The float to write
* @returns How many bytes were written (4)
*/
writeFloat(value) {
common_1.float32Array[0] = value;
// Typed arrays are endian-dependent, so if the computer is little-endian, the output will be in little-endian format
if (common_1.isBigEndian) {
return (0, common_1.wrapForPromise)(4, this.writeUint8ArrayEndian(common_1.uint8Float32ArrayView));
}
else {
return (0, common_1.wrapForPromise)(4, this.writeUint8ArrayBackwardsEndian(common_1.uint8Float32ArrayView));
}
}
/**
* Write a double float to the buffer
* @param value The double float to write
* @returns How many bytes were written (8)
*/
writeDouble(value) {
common_1.float64Array[0] = value;
if (common_1.isBigEndian) {
return (0, common_1.wrapForPromise)(8, this.writeUint8ArrayEndian(common_1.uint8Float64ArrayView));
}
else {
return (0, common_1.wrapForPromise)(8, this.writeUint8ArrayBackwardsEndian(common_1.uint8Float64ArrayView));
}
}
/**
* Write a utf8 string to the buffer
* @param value
* @param mutf8 If true, write in java's mutf8 format instead. This was build for parsing java's .class files, so no complaining about it being a java-specific format.
* @returns How many bytes were written
*/
writeString(value, mutf8 = false) {
let encoded;
if (mutf8 === true) {
encoded = (0, utf8tools_1.encodeMutf8)(value);
}
else {
encoded = (0, utf8tools_1.encodeUtf8)(value);
}
return (0, common_1.wrapForPromise)(this.writeUint8Array(encoded), encoded.length);
}
/**
* Calculate the minimum length of a signed ones's complement in bytes.
* WARNING: No signed two's complements above 4278190079 or below -4278190079 (2^31 - 1) are supported, so this will not work for those.
* This is due to the limitations of bitwise operators. You can write numbers higher than that via writeSignedOnesComplementBigint, but this function will not work for them.
* @remarks
* This function calculates the minimum number of bytes needed to represent an signed one's in binary format.
* It uses the `Math.clz32` function to count the number of leading zeros in the binary representation of the value.
* It subtracts this from 33 (equivilent to the number of bits in the signed one's complement +1 to account for the sign) to get the number of bits needed.
* The result is rounded up to the nearest byte.
* @param value The value to check
* @returns The calculated minimum length in bytes
*/
minLengthOfSignedOnesComplement(value) {
// Length of the number as a unsigned int+1 is the bytes (to take into account the sign bit)
// This is then converted to bytes
return Math.ceil((33 - Math.clz32(Math.abs(value))) / 8);
}
/**
* Encode and write a signed one's complement
* @param value The number to encode
* @param bytes How many bytes to make the encoded value
* @returns How many bytes were written (Same as bytes parameter if provided)
*/
writeSignedOnesComplement(value, bytes) {
return (0, common_1.wrapForPromise)(this.writeUnsignedInt(value < 0
? (value - 1) & (constants.allOnes >>> (32 - bytes * 8))
: // Rely on the user not to use to big of a value
value /* & (constants.allOnes >>> (32 - bytes * 8))*/, bytes), bytes);
}
/**
* Encode and write a signed ones complement (from a bigint)
* @param value The number to encode
* @param bytes How many bytes to make the encoded value
* @returns How many bytes were written (Same as bytes parameter)
*/
writeSignedOnesComplementBigint(value, bytes) {
return (0, common_1.wrapForPromise)(this.writeUnsignedIntBigint(value < 0n
? (value - 1n) & ~(-1n << BigInt(32 - bytes * 8))
: // Rely on the user not to use to big of a value
value /* & (constants.allOnes >>> (32 - bytes * 8))*/, bytes), bytes);
}
/**
* Encode and write a signed ones complement (one byte)
* @param value The number to encode
* @returns How many bytes were written (1)
*/
writeSignedOnesComplementByte(value) {
return (0, common_1.wrapForPromise)(this.push(value < 0 ? (value - 1) & 0xff : value), 1);
}
/**
* Encode and write a signed ones complements
* @param values The numbers to encode
* @returns How many bytes were written (Same as values.length)
*/
writeSignedOnesComplementByteArray(values) {
return (0, common_1.wrapForAsyncCallArr)(this.writeSignedOnesComplementByte.bind(this), values.map((value) => [value]), values.length);
}
/**
* Calculate the minimum length of a signed integer in bytes.
* WARNING: No signed integers above 4278190079 or below -4278190079 (2^31 - 1) are supported, so this will not work for those.
* This is due to the limitations of bitwise operators. You can write numbers higher than that via writeSignedIntegerBigint, but this function will not work for them.
* @remarks
* This function calculates the minimum number of bytes needed to represent a signed integer in binary format.
* It uses the `Math.clz32` function to count the number of leading zeros in the binary representation of the value.
* It subtracts this from 33 (equivilent to the number of bits in the signed integer +1 to account for the sign) to get the number of bits needed.
* The result is rounded up to the nearest byte.
* @param value The value to check
* @returns The calculated minimum length in bytes
*/
minLengthOfSignedInteger(value) {
// Length of the number as a unsigned int+1 is the bytes (to take into account the sign bit)
// This is then converted to bytes
return Math.ceil((33 - Math.clz32(Math.abs(value))) / 8);
}
/**
* Encode and write a signed integer
* @param value The number to encode
* @param bytes How many bytes to make the encoded value
* @returns How many bytes were written (Same as bytes parameter if provided)
*/
writeSignedInteger(value, bytes) {
const absValue = Math.abs(value);
const bits = bytes * 8;
return (0, common_1.wrapForPromise)(this.writeUnsignedInt(value < 0
? // Rely on the user not to use to big of a value
absValue | (1 << (bits - 1)) // & (constants.allOnes >>> (32 - bits))
: value, bytes), bytes);
}
/**
* Encode and write a signed integer (from a bigint)
* @param value The number to encode
* @param bytes How many bytes to make the encoded value
* @returns How many bytes were written (Same as bytes parameter)
*/
writeSignedIntegerBigint(value, bytes) {
// const oneLiner = (value,bytes) => value < 0n ? -value | (1n << (BigInt(bytes*8) - 1n)) : value;
const bits = BigInt(bytes * 8);
return (0, common_1.wrapForPromise)(this.writeUnsignedIntBigint(value < 0n
? // Rely on the user not to use to big of a value
-value | (1n << (bits - 1n)) // & ~(-1n << bits)
: value, bytes), bytes);
}
/**
* Encode and write a signed integer (one byte)
* @param value The number to encode
* @returns How many bytes were written (1)
*/
writeSignedIntegerByte(value) {
return (0, common_1.wrapForPromise)(this.push(value < 0 ? 0b10000000 | -value : value), 1);
}
/**
* Encode and write signed integers (one byte)
* @param values The numbers to encode
* @returns How many bytes were written (Same as values.length)
*/
writeSignedIntegerByteArray(values) {
return (0, common_1.wrapForAsyncCallArr)(this.writeSignedIntegerByte.bind(this), values.map((value) => [value]), values.length);
}
}
exports.writableBufferBase = writableBufferBase;
_writableBufferBase_isLe = new WeakMap();
class writableBuffer extends writableBufferBase {
get buffer() {
return (0, common_1.joinUint8Arrays)([
...__classPrivateFieldGet(this, _writableBuffer_buffers, "f").slice(0, -1),
__classPrivateFieldGet(this, _writableBuffer_buffers, "f")[__classPrivateFieldGet(this, _writableBuffer_buffers, "f").length - 1].subarray(0, __classPrivateFieldGet(this, _writableBuffer_used, "f")),
], this.length);
}
set buffer(value) {
if (value instanceof writableBuffer &&
__classPrivateFieldGet(value, _writableBuffer_chunkSize, "f") === __classPrivateFieldGet(this, _writableBuffer_chunkSize, "f")) {
__classPrivateFieldSet(this, _writableBuffer_buffers, __classPrivateFieldGet(value, _writableBuffer_buffers, "f"), "f");
__classPrivateFieldSet(this, _writableBuffer_used, __classPrivateFieldGet(value, _writableBuffer_used, "f"), "f");
}
else {
__classPrivateFieldSet(this, _writableBuffer_buffers, [new Uint8Array(__classPrivateFieldGet(this, _writableBuffer_chunkSize, "f"))], "f");
__classPrivateFieldSet(this, _writableBuffer_used, 0, "f");
if (value instanceof writableBufferBase) {
// Processing goes into getting this value, so don't grab it twice
const buffer = value.buffer;
if (buffer instanceof Uint8Array) {
this.writeUint8Array(buffer);
return;
}
}
this.writeUint8Array(value instanceof Uint8Array
? value
: new Uint8Array(value));
}
}
get length() {
return (__classPrivateFieldGet(this, _writableBuffer_chunkSize, "f") * __classPrivateFieldGet(this, _writableBuffer_buffers, "f").length - (__classPrivateFieldGet(this, _writableBuffer_chunkSize, "f") - __classPrivateFieldGet(this, _writableBuffer_used, "f")));
}
constructor(chunkSize = 2000) {
super();
_writableBuffer_chunkSize.set(this, void 0);
_writableBuffer_buffers.set(this, void 0);
_writableBuffer_used.set(this, 0);
__classPrivateFieldSet(this, _writableBuffer_chunkSize, chunkSize, "f");
__classPrivateFieldSet(this, _writableBuffer_buffers, [new Uint8Array(chunkSize)], "f");
}
push(value) {
var _a, _b;
if (__classPrivateFieldGet(this, _writableBuffer_used, "f") === __classPrivateFieldGet(this, _writableBuffer_chunkSize, "f")) {
__classPrivateFieldSet(this, _writableBuffer_used, 0, "f");
__classPrivateFieldGet(this, _writableBuffer_buffers, "f").unshift(new Uint8Array(__classPrivateFieldGet(this, _writableBuffer_chunkSize, "f")));
}
__classPrivateFieldGet(this, _writableBuffer_buffers, "f")[0][__classPrivateFieldSet(this, _writableBuffer_used, (_b = __classPrivateFieldGet(this, _writableBuffer_used, "f"), _a = _b++, _b), "f"), _a] = value;
}
writeUint8Array(value) {
let bytesLeft = value.length;
let index = 0;
while (bytesLeft > 0) {
if (__classPrivateFieldGet(this, _writableBuffer_used, "f") === __classPrivateFieldGet(this, _writableBuffer_chunkSize, "f")) {
__classPrivateFieldSet(this, _writableBuffer_used, 0, "f");
__classPrivateFieldGet(this, _writableBuffer_buffers, "f").unshift(new Uint8Array(__classPrivateFieldGet(this, _writableBuffer_chunkSize, "f")));
}
const bytesToWrite = Math.min(bytesLeft, __classPrivateFieldGet(this, _writableBuffer_chunkSize, "f") - __classPrivateFieldGet(this, _writableBuffer_used, "f"));
__classPrivateFieldGet(this, _writableBuffer_buffers, "f")[0].set(value.subarray(index, index + bytesToWrite), __classPrivateFieldGet(this, _writableBuffer_used, "f"));
__classPrivateFieldSet(this, _writableBuffer_used, __classPrivateFieldGet(this, _writableBuffer_used, "f") + bytesToWrite, "f");
index += bytesToWrite;
bytesLeft -= bytesToWrite;
}
}
writeUint8ArrayBackwards(value) {
// Don't mutate the origional value
this.writeUint8Array(value.slice(0).reverse());
}
writeArray(value) {
let bytesLeft = value.length;
let index = 0;
while (bytesLeft > 0) {
if (__classPrivateFieldGet(this, _writableBuffer_used, "f") === __classPrivateFieldGet(this, _writableBuffer_chunkSize, "f")) {
__classPrivateFieldSet(this, _writableBuffer_used, 0, "f");
__classPrivateFieldGet(this, _writableBuffer_buffers, "f").unshift(new Uint8Array(__classPrivateFieldGet(this, _writableBuffer_chunkSize, "f")));
}
const bytesToWrite = Math.min(bytesLeft, __classPrivateFieldGet(this, _writableBuffer_chunkSize, "f") - __classPrivateFieldGet(this, _writableBuffer_used, "f"));
__classPrivateFieldGet(this, _writableBuffer_buffers, "f")[0].set(value.slice(index, index + bytesToWrite), __classPrivateFieldGet(this, _writableBuffer_used, "f"));
__classPrivateFieldSet(this, _writableBuffer_used, __classPrivateFieldGet(this, _writableBuffer_used, "f") + bytesToWrite, "f");
index += bytesToWrite;
bytesLeft -= bytesToWrite;
}
}
writeArrayBackwards(value) {
this.writeArray(value.slice(0).reverse());
}
}
exports.writableBuffer = writableBuffer;
_writableBuffer_chunkSize = new WeakMap(), _writableBuffer_buffers = new WeakMap(), _writableBuffer_used = new WeakMap();
exports.writableBufferLE = (0, common_1.addDefaultEndianness)(writableBuffer, true);
class writableBufferFixedSize extends writableBufferBase {
get buffer() {
return __classPrivateFieldGet(this, _writableBufferFixedSize_buffer, "f").subarray(0, __classPrivateFieldGet(this, _writableBufferFixedSize_used, "f"));
}
set buffer(value) {
if (value.length > __classPrivateFieldGet(this, _writableBufferFixedSize_buffer, "f").length) {
throw new Error("Buffer does not have capacity to fit the new buffer's contents");
}
// Don't check for writableBufferFixedSize because copying the buffer is what we do anyway and using the same one can result in issues if both instances are used
if (value instanceof writableBufferBase) {
// Processing goes into getting this value, so don't grab it twice
const buffer = value.buffer;
if (buffer instanceof Uint8Array) {
__classPrivateFieldGet(this, _writableBufferFixedSize_buffer, "f").set(buffer, 0);
__classPrivateFieldSet(this, _writableBufferFixedSize_used, buffer.length, "f");
return;
}
}
__classPrivateFieldGet(this, _writableBufferFixedSize_buffer, "f").set(value, 0);
__classPrivateFieldSet(this, _writableBufferFixedSize_used, value.length, "f");
}
get length() {
return __classPrivateFieldGet(this, _writableBufferFixedSize_used, "f");
}
get maxLength() {
return __classPrivateFieldGet(this, _writableBufferFixedSize_buffer, "f").length;
}
set maxLength(value) {
if (value < __classPrivateFieldGet(this, _writableBufferFixedSize_used, "f")) {
throw new Error("Cannot set maxLength to less than used length");
}
const oldBuffer = this.buffer;
__classPrivateFieldSet(this, _writableBufferFixedSize_buffer, new Uint8Array(value), "f");
__classPrivateFieldGet(this, _writableBufferFixedSize_buffer, "f").set(oldBuffer);
}
constructor(maxLength = 2000) {
super();
_writableBufferFixedSize_buffer.set(this, void 0);
_writableBufferFixedSize_used.set(this, 0);
__classPrivateFieldSet(this, _writableBufferFixedSize_buffer, new Uint8Array(maxLength), "f");
}
/**
* Reset the buffer
*/
reset() {
__classPrivateFieldSet(this, _writableBufferFixedSize_used, 0, "f");
}
push(value) {
var _a, _b;
if (__classPrivateFieldGet(this, _writableBufferFixedSize_used, "f") === __classPrivateFieldGet(this, _writableBufferFixedSize_buffer, "f").length) {
throw new Error("Buffer does not have capacity to write the data");
}
__classPrivateFieldGet(this, _writableBufferFixedSize_buffer, "f")[__classPrivateFieldSet(this, _writableBufferFixedSize_used, (_b = __classPrivateFieldGet(this, _writableBufferFixedSize_used, "f"), _a = _b++, _b), "f"), _a] = value;
}
writeUint8Array(value) {
if (__classPrivateFieldGet(this, _writableBufferFixedSize_used, "f") + value.length > __classPrivateFieldGet(this, _writableBufferFixedSize_buffer, "f").length) {
throw new Error("Buffer does not have capacity to write the data");
}
__classPrivateFieldGet(this, _writableBufferFixedSize_buffer, "f").set(value, __classPrivateFieldGet(this, _writableBufferFixedSize_used, "f"));
__classPrivateFieldSet(this, _writableBufferFixedSize_used, __classPrivateFieldGet(this, _writableBufferFixedSize_used, "f") + value.length, "f");
}
writeUint8ArrayBackwards(value) {
// Don't mutate the origional value
this.writeUint8Array(value.slice(0).reverse());
}
writeArray(value) {
if (__classPrivateFieldGet(this, _writableBufferFixedSize_used, "f") + value.length > __classPrivateFieldGet(this, _writableBufferFixedSize_buffer, "f").length) {
throw new Error("Buffer does not have capacity to write the data");
}
__classPrivateFieldGet(this, _writableBufferFixedSize_buffer, "f").set(value, __classPrivateFieldGet(this, _writableBufferFixedSize_used, "f"));
__classPrivateFieldSet(this, _writableBufferFixedSize_used, __classPrivateFieldGet(this, _writableBufferFixedSize_used, "f") + value.length, "f");
}
writeArrayBackwards(value) {
// Don't mutate the origional value
this.writeArray(value.slice(0).reverse());
}
writeWriteableBuffer(value) {
if (value.length > __classPrivateFieldGet(this, _writableBufferFixedSize_buffer, "f").length - __classPrivateFieldGet(this, _writableBufferFixedSize_used, "f")) {
throw new Error("Buffer does not have capacity to write the data");
}
return this.writeUint8Array(value.buffer);
}
}
exports.writableBufferFixedSize = writableBufferFixedSize;
_writableBufferFixedSize_buffer = new WeakMap(), _writableBufferFixedSize_used = new WeakMap();
exports.writableBufferFixedSizeLE = (0, common_1.addDefaultEndianness)(writableBufferFixedSize, true);