/*
 * SDK Pullenti Unitext, version 4.33, fabruary 2026. Copyright (c) 2013-2026, Pullenti. All rights reserved. 
 * Non-Commercial Freeware and Commercial Software. 
 * This class is generated using the converter UniSharping (www.unisharping.ru) from Pullenti C# project. 
 * The latest version of the code is available on the site www.pullenti.ru
 */


// This class is general purpose class for writing data to a buffer.
// It allows you to write bits as well as bytes
// Based on DeflaterPending.java
class PendingBuffer {
    
    constructor(bufferSize = 4096) {
        this.buffer_ = null;
        this.start = 0;
        this.end = 0;
        this.bits = 0;
        this.m_BitCount = 0;
        this.buffer_ = new Uint8Array(bufferSize);
    }
    
    reset() {
        this.start = (this.end = (this.m_BitCount = 0));
    }
    
    writeByte(value) {
        this.buffer_[this.end++] = value;
    }
    
    writeShort(value) {
        this.buffer_[this.end++] = value;
        this.buffer_[this.end++] = (value >> 8);
    }
    
    writeInt(value) {
        this.buffer_[this.end++] = value;
        this.buffer_[this.end++] = (value >> 8);
        this.buffer_[this.end++] = (value >> 16);
        this.buffer_[this.end++] = (value >> 24);
    }
    
    writeBlock(block, offset, length) {
        for(let iii = 0; iii < (length); iii++) this.buffer_[(this.end) + iii] = block[(offset) + iii];
        this.end += length;
    }
    
    get bitCount() {
        return this.m_BitCount;
    }
    
    alignToByte() {
        if (this.m_BitCount > 0) {
            this.buffer_[this.end++] = this.bits;
            if (this.m_BitCount > 8) 
                this.buffer_[this.end++] = ((this.bits) >>> 8);
        }
        this.bits = 0;
        this.m_BitCount = 0;
    }
    
    writeBits(b, count) {
        this.bits |= (b << this.m_BitCount);
        this.m_BitCount += count;
        if (this.m_BitCount >= 16) {
            this.buffer_[this.end++] = this.bits;
            this.buffer_[this.end++] = ((this.bits) >>> 8);
            this.bits >>= (16);
            this.m_BitCount -= 16;
        }
    }
    
    writeShortMSB(s) {
        this.buffer_[this.end++] = (s >> 8);
        this.buffer_[this.end++] = s;
    }
    
    get isFlushed() {
        return this.end === 0;
    }
    
    flush(output, offset, length) {
        if (this.m_BitCount >= 8) {
            this.buffer_[this.end++] = this.bits;
            this.bits >>= (8);
            this.m_BitCount -= 8;
        }
        if (length > (this.end - this.start)) {
            length = this.end - this.start;
            for(let iii = 0; iii < (length); iii++) output[(offset) + iii] = this.buffer_[(this.start) + iii];
            this.start = 0;
            this.end = 0;
        }
        else {
            for(let iii = 0; iii < (length); iii++) output[(offset) + iii] = this.buffer_[(this.start) + iii];
            this.start += length;
        }
        return length;
    }
    
    toByteArray() {
        let result = new Uint8Array(this.end - this.start);
        for(let iii = 0; iii < (result.length); iii++) result[(0) + iii] = this.buffer_[(this.start) + iii];
        this.start = 0;
        this.end = 0;
        return result;
    }
}


module.exports = PendingBuffer