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

// An input buffer customised for use by <see cref="InflaterInputStream"/>
public class InflaterInputBuffer {

    public InflaterInputBuffer(com.pullenti.unisharp.Stream stream, int bufferSize) {
        inputStream = stream;
        if (bufferSize < 1024) 
            bufferSize = 1024;
        m_RawData = new byte[bufferSize];
        m_ClearText = m_RawData;
    }

    public int getRawLength() {
        return m_RawLength;
    }


    public byte[] getRawData() {
        return m_RawData;
    }


    public int getClearTextLength() {
        return m_ClearTextLength;
    }


    public byte[] getClearText() {
        return m_ClearText;
    }


    public int getAvailable() {
        return m_Available;
    }

    public int setAvailable(int value) {
        m_Available = value;
        return value;
    }


    public void setInflaterInput(Inflater inflater) throws IllegalArgumentException, IndexOutOfBoundsException, UnsupportedOperationException {
        if (m_Available > 0) {
            inflater.setInputEx(m_ClearText, m_ClearTextLength - m_Available, m_Available);
            m_Available = 0;
        }
    }

    public void fill() throws java.io.IOException {
        m_RawLength = 0;
        int toRead = m_RawData.length;
        while (toRead > 0) {
            int count = inputStream.read(m_RawData, m_RawLength, toRead);
            if (count <= 0) 
                break;
            m_RawLength += count;
            toRead -= count;
        }{
                m_ClearTextLength = m_RawLength;
            }
        m_Available = m_ClearTextLength;
    }

    public int readRawBuffer(byte[] buffer) throws IndexOutOfBoundsException, java.io.IOException {
        return this.readRawBufferEx(buffer, 0, buffer.length);
    }

    public int readRawBufferEx(byte[] outBuffer, int offset, int length) throws IndexOutOfBoundsException, java.io.IOException {
        if (length < 0) 
            throw new IndexOutOfBoundsException("length");
        int currentOffset = offset;
        int currentLength = length;
        while (currentLength > 0) {
            if (m_Available <= 0) {
                this.fill();
                if (m_Available <= 0) 
                    return 0;
            }
            int toCopy = Math.min(currentLength, m_Available);
            for(int iii = 0; iii < (toCopy); iii++) outBuffer[(currentOffset) + iii] = m_RawData[(m_RawLength - (m_Available)) + iii];
            currentOffset += toCopy;
            currentLength -= toCopy;
            m_Available -= toCopy;
        }
        return length;
    }

    public int readClearTextBuffer(byte[] outBuffer, int offset, int length) throws IndexOutOfBoundsException, java.io.IOException {
        if (length < 0) 
            throw new IndexOutOfBoundsException("length");
        int currentOffset = offset;
        int currentLength = length;
        while (currentLength > 0) {
            if (m_Available <= 0) {
                this.fill();
                if (m_Available <= 0) 
                    return 0;
            }
            int toCopy = Math.min(currentLength, m_Available);
            for(int iii = 0; iii < (toCopy); iii++) outBuffer[(currentOffset) + iii] = m_ClearText[(m_ClearTextLength - (m_Available)) + iii];
            currentOffset += toCopy;
            currentLength -= toCopy;
            m_Available -= toCopy;
        }
        return length;
    }

    public int readLeByte() throws Exception, java.io.IOException {
        if (m_Available <= 0) {
            this.fill();
            if (m_Available <= 0) 
                throw new Exception("EOF in header");
        }
        byte result = m_RawData[m_RawLength - m_Available];
        m_Available -= 1;
        return Byte.toUnsignedInt(result);
    }

    public int readLeShort() throws Exception, java.io.IOException {
        return this.readLeByte() | (this.readLeByte() << 8);
    }

    public int readLeInt() throws Exception, java.io.IOException {
        return this.readLeShort() | (this.readLeShort() << 16);
    }

    private int m_RawLength;

    private byte[] m_RawData;

    private int m_ClearTextLength;

    private byte[] m_ClearText;

    private int m_Available;

    private com.pullenti.unisharp.Stream inputStream;
    public InflaterInputBuffer() {
    }
}
