/*
 * 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
 */

package com.pullenti.unitext.internal.zip;

// 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
public class PendingBuffer {

    private byte[] buffer_;

    private int start;

    private int end;

    private int bits;

    private int m_BitCount;

    public PendingBuffer(int bufferSize) {
        buffer_ = new byte[bufferSize];
    }

    public void reset() {
        start = (end = (m_BitCount = 0));
    }

    public void writeByte(int value) {
        buffer_[end++] = (byte)value;
    }

    public void writeShort(int value) {
        buffer_[end++] = (byte)value;
        buffer_[end++] = (byte)((value >> 8));
    }

    public void writeInt(int value) {
        buffer_[end++] = (byte)value;
        buffer_[end++] = (byte)((value >> 8));
        buffer_[end++] = (byte)((value >> 16));
        buffer_[end++] = (byte)((value >> 24));
    }

    public void writeBlock(byte[] block, int offset, int length) {
        for(int iii = 0; iii < (length); iii++) buffer_[(end) + iii] = block[(offset) + iii];
        end += length;
    }

    public int getBitCount() {
        return m_BitCount;
    }


    public void alignToByte() {
        if (m_BitCount > 0) {
            buffer_[end++] = (byte)bits;
            if (m_BitCount > 8) 
                buffer_[end++] = (byte)((((int)bits) >>> 8));
        }
        bits = (int)0;
        m_BitCount = 0;
    }

    public void writeBits(int b, int count) {
        bits |= ((int)(b << m_BitCount));
        m_BitCount += count;
        if (m_BitCount >= 16) {
            buffer_[end++] = (byte)bits;
            buffer_[end++] = (byte)((((int)bits) >>> 8));
            bits >>= ((int)16);
            m_BitCount -= 16;
        }
    }

    public void writeShortMSB(int s) {
        buffer_[end++] = (byte)((s >> 8));
        buffer_[end++] = (byte)s;
    }

    public boolean isFlushed() {
        return end == 0;
    }


    public int flush(byte[] output, int offset, int length) {
        if (m_BitCount >= 8) {
            buffer_[end++] = (byte)bits;
            bits >>= ((int)8);
            m_BitCount -= 8;
        }
        if (length > (end - start)) {
            length = end - start;
            for(int iii = 0; iii < (length); iii++) output[(offset) + iii] = buffer_[(start) + iii];
            start = 0;
            end = 0;
        }
        else {
            for(int iii = 0; iii < (length); iii++) output[(offset) + iii] = buffer_[(start) + iii];
            start += length;
        }
        return length;
    }

    public byte[] toByteArray() {
        byte[] result = new byte[end - start];
        for(int iii = 0; iii < (result.length); iii++) result[(0) + iii] = buffer_[(start) + iii];
        start = 0;
        end = 0;
        return result;
    }
    public PendingBuffer() {
        this(4096);
    }
}
