"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.VectorBufferStream = void 0;
class VectorBufferStream {
    constructor(initialSize) {
        this.buffer = Buffer.allocUnsafe(initialSize);
        this.position = 0;
    }
    static fromBuffer(buffer) {
        const stream = new VectorBufferStream(0);
        stream.buffer = buffer;
        stream.position = 0;
        return stream;
    }
    getAsHexString() {
        return this.buffer.toString('hex');
    }
    getBufferLength() {
        return this.buffer.length;
    }
    isAtOrPastEnd() {
        return this.position >= this.buffer.length;
    }
    ensureCapacity(size) {
        if (this.position + size <= this.buffer.length) {
            return;
        }
        const newSize = Math.max(this.buffer.length * 2, this.position + size);
        const newBuffer = Buffer.allocUnsafe(newSize);
        this.buffer.copy(newBuffer);
        this.buffer = newBuffer;
    }
    getBuffer() {
        return this.buffer.subarray(0, this.position);
    }
    write(value, encoding) {
        if (typeof value === 'string') {
            encoding = encoding || 'utf8';
            const size = Buffer.byteLength(value, encoding);
            this.ensureCapacity(size);
            const written = this.buffer.write(value, this.position, size, encoding);
            this.position += written;
            //console.log(`size: ${size} written: ${written}`);
        }
        else {
            this.ensureCapacity(value.length);
            value.copy(this.buffer, this.position);
            this.position += value.length;
        }
    }
    writeInt8(value) {
        this.ensureCapacity(1);
        this.buffer.writeInt8(value, this.position);
        this.position += 1;
    }
    writeUInt8(value) {
        this.ensureCapacity(1);
        this.buffer.writeUInt8(value, this.position);
        this.position += 1;
    }
    writeString(value) {
        const size = Buffer.byteLength(value, 'utf8');
        this.writeUInt32(size); // Write the size of the string as a 32-bit integer
        this.ensureCapacity(size);
        this.buffer.write(value, this.position, size, 'utf8');
        this.position += size;
    }
    writeBuffer(value) {
        const size = value.length;
        this.writeUInt32(size); // Write the size of the buffer as a 32-bit integer
        this.ensureCapacity(size);
        value.copy(this.buffer, this.position);
        this.position += size;
    }
    writeInt16(value) {
        this.ensureCapacity(2);
        this.buffer.writeInt16LE(value, this.position);
        this.position += 2;
    }
    writeUInt16(value) {
        this.ensureCapacity(2);
        this.buffer.writeUInt16LE(value, this.position);
        this.position += 2;
    }
    writeInt32(value) {
        this.ensureCapacity(4);
        this.buffer.writeInt32LE(value, this.position);
        this.position += 4;
    }
    writeUInt32(value) {
        this.ensureCapacity(4);
        this.buffer.writeUInt32LE(value, this.position);
        this.position += 4;
    }
    writeBigInt64(value) {
        this.ensureCapacity(8);
        this.buffer.writeBigInt64LE(value, this.position);
        this.position += 8;
    }
    writeBigUInt64(value) {
        this.ensureCapacity(8);
        this.buffer.writeBigUInt64LE(value, this.position);
        this.position += 8;
    }
    writeFloat(value) {
        this.ensureCapacity(4);
        this.buffer.writeFloatLE(value, this.position);
        this.position += 4;
    }
    writeDouble(value) {
        this.ensureCapacity(8);
        this.buffer.writeDoubleLE(value, this.position);
        this.position += 8;
    }
    ///////////////READ METHODS////////////////////
    readString() {
        const size = this.readUInt32(); // Read the size of the string as a 32-bit integer
        const value = this.buffer.toString('utf8', this.position, this.position + size);
        this.position += size;
        return value;
    }
    readBuffer() {
        const size = this.readUInt32(); // Read the size of the buffer as a 32-bit unsigned integer
        //console.log(`buffer size: ${size}`);
        const value = this.buffer.slice(this.position, this.position + size);
        this.position += size;
        return value;
    }
    readUInt8() {
        const value = this.buffer.readUInt8(this.position);
        this.position += 1;
        return value;
    }
    readInt16() {
        const value = this.buffer.readInt16LE(this.position);
        this.position += 2;
        return value;
    }
    readUInt16() {
        const value = this.buffer.readUInt16LE(this.position);
        this.position += 2;
        return value;
    }
    readInt32() {
        const value = this.buffer.readInt32LE(this.position);
        this.position += 4;
        return value;
    }
    readUInt32() {
        const value = this.buffer.readUInt32LE(this.position);
        this.position += 4;
        return value;
    }
    readBigInt64() {
        const value = this.buffer.readBigInt64LE(this.position);
        this.position += 8;
        return value;
    }
    readBigUInt64() {
        const value = this.buffer.readBigUInt64LE(this.position);
        this.position += 8;
        return value;
    }
    readFloat() {
        const value = this.buffer.readFloatLE(this.position);
        this.position += 4;
        return value;
    }
    readDouble() {
        const value = this.buffer.readDoubleLE(this.position);
        this.position += 8;
        return value;
    }
    readFixedBuffer(length) {
        const value = this.buffer.slice(this.position, this.position + length);
        this.position += length;
        return value;
    }
    writeFixedBuffer(value) {
        this.ensureCapacity(value.length);
        value.copy(this.buffer, this.position);
        this.position += value.length;
    }
}
exports.VectorBufferStream = VectorBufferStream;
//# sourceMappingURL=VectorBufferStream.js.map