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

const Stream = require("./../../../unisharp/Stream");

/**
 * A <see cref="PartialInputStream"/> is an <see cref="InflaterInputStream"/> 
 * whose data is only a part or subsection of a file.
 */
class PartialInputStream extends Stream {
    
    /**
     * Initialise a new instance of the <see cref="PartialInputStream"/> class.
     * @param zipFile The <see cref="ZipFile"/> containing the underlying stream to use for IO.
     * @param start The start of the partial data.
     * @param _length The length of the partial data.
     */
    constructor(zipFile, start, _length) {
        super();
        this.m_ZipFile = null;
        this.m_BaseStream = null;
        this.m_Start = 0;
        this.m_Length = 0;
        this.m_ReadPos = 0;
        this.m_End = 0;
        this.m_Start = start;
        this.m_Length = _length;
        this.m_ZipFile = zipFile;
        this.m_BaseStream = this.m_ZipFile.m_BaseStream;
        this.m_ReadPos = start;
        this.m_End = start + _length;
    }
    
    /**
     * Read a byte from this stream.
     * @return Returns the byte read or -1 on end of stream.
     */
    readByte() {
        if (this.m_ReadPos >= this.m_End) 
            return -1;
        this.m_BaseStream.seek(this.m_ReadPos++, 0);
        return this.m_BaseStream.readByte();
    }
    
    /**
     * Close this <see cref="PartialInputStream">partial input stream</see>.
     */
    close() {
        
    }
    
    /**
     * Reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
     * @param buffer An array of bytes. When this method returns, the buffer contains the specified byte array with the values between offset and (offset + count - 1) replaced by the bytes read from the current source.
     * @param offset The zero-based byte offset in buffer at which to begin storing the data read from the current stream.
     * @param count The maximum number of bytes to be read from the current stream.
     * @return The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached.
     */
    read(buffer, offset, count) {
        if (count > (this.m_End - this.m_ReadPos)) {
            count = (this.m_End - this.m_ReadPos);
            if (count === 0) 
                return 0;
        }
        this.m_BaseStream.seek(this.m_ReadPos, 0);
        let readCount = this.m_BaseStream.read(buffer, offset, count);
        if (readCount > 0) 
            this.m_ReadPos += readCount;
        return readCount;
    }
    
    /**
     * Writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
     * @param buffer An array of bytes. This method copies count bytes from buffer to the current stream.
     * @param offset The zero-based byte offset in buffer at which to begin copying bytes to the current stream.
     * @param count The number of bytes to be written to the current stream.
     */
    write(buffer, offset, count) {
        throw new Error("Not supported");
    }
    
    /**
     * When overridden in a derived class, sets the length of the current stream.
     * @param value The desired length of the current stream in bytes.
     */
    setLength(value) {
        throw new Error("Not supported");
    }
    
    /**
     * When overridden in a derived class, sets the position within the current stream.
     * @param offset A byte offset relative to the origin parameter.
     * @param origin A value of type <see cref="T:System.IO.SeekOrigin"></see> indicating the reference point used to obtain the new position.
     * @return The new position within the current stream.
     */
    seek(offset, origin) {
        let newPos = this.m_ReadPos;
        switch (origin) { 
        case 0: {
            newPos = this.m_Start + (offset);
            break;
        }
        case 1: {
            newPos = this.m_ReadPos + (offset);
            break;
        }
        case 2: {
            newPos = this.m_End + (offset);
            break;
        }
        }
        if (newPos < this.m_Start) 
            throw new Error("Negative position is invalid");
        if (newPos >= this.m_End) 
            throw new Error("Cannot seek past end");
        this.m_ReadPos = newPos;
        return this.m_ReadPos;
    }
    
    /**
     * Clears all buffers for this stream and causes any buffered data to be written to the underlying device.
     */
    flush() {
        
    }
    
    get position() {
        return this.m_ReadPos - this.m_Start;
    }
    set position(value) {
        let newPos = this.m_Start + (value);
        if (newPos < this.m_Start) 
            throw new Error("Negative position is invalid");
        if (newPos >= this.m_End) 
            throw new Error("Cannot seek past end");
        this.m_ReadPos = newPos;
        return value;
    }
    
    get length() {
        return this.m_Length;
    }
    
    get canWrite() {
        return false;
    }
    
    get canSeek() {
        return true;
    }
    
    get canRead() {
        return true;
    }
}


module.exports = PartialInputStream