/*
 * 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 <see cref="IArchiveStorage"/> implementation suitable for in memory streams.
 */
public class MemoryArchiveStorage extends BaseArchiveStorage {

    /**
     * Initializes a new instance of the <see cref="MemoryArchiveStorage"/> class.
     * @param _updateMode The <see cref="FileUpdateMode"/> to use
     */
    public MemoryArchiveStorage(FileUpdateMode _updateMode) {
        super(_updateMode);
    }

    public com.pullenti.unisharp.MemoryStream getFinalStream() {
        return finalStream_;
    }


    /**
     * Gets the temporary output <see cref="Stream"/>
     * @return Returns the temporary output stream.
     */
    @Override
    public com.pullenti.unisharp.Stream getTemporaryOutput() {
        temporaryStream_ = new com.pullenti.unisharp.MemoryStream();
        return temporaryStream_;
    }

    /**
     * Converts the temporary <see cref="Stream"/> to its final form.
     * @return Returns a <see cref="Stream"/> that can be used to read 
     * the final storage for the archive.
     */
    @Override
    public com.pullenti.unisharp.Stream convertTemporaryToFinal() {
        try {
            if (temporaryStream_ == null) 
                throw new Exception("No temporary stream has been created");
            finalStream_ = new com.pullenti.unisharp.MemoryStream(temporaryStream_.toByteArray());
            return finalStream_;
        } catch (Exception ex123) {
            return null;
        }
    }

    /**
     * Make a temporary copy of the original stream.
     * @param stream The <see cref="Stream"/> to copy.
     * @return Returns a temporary output <see cref="Stream"/> that is a copy of the input.
     */
    @Override
    public com.pullenti.unisharp.Stream makeTemporaryCopy(com.pullenti.unisharp.Stream stream) {
        try {
            temporaryStream_ = new com.pullenti.unisharp.MemoryStream();
            stream.setPosition(0L);
            StreamUtils.copy(stream, temporaryStream_, new byte[4096]);
            return temporaryStream_;
        } catch (Exception ex123) {
            return null;
        }
    }

    /**
     * Return a stream suitable for performing direct updates on the original source.
     * @param stream The original source stream
     * @return Returns a stream suitable for direct updating.
     */
    @Override
    public com.pullenti.unisharp.Stream openForDirectUpdate(com.pullenti.unisharp.Stream stream) {
        try {
            if ((stream == null) || !stream.canWrite()) {
                com.pullenti.unisharp.MemoryStream result = new com.pullenti.unisharp.MemoryStream();
                if (stream != null) {
                    stream.setPosition(0L);
                    StreamUtils.copy(stream, result, new byte[4096]);
                    stream.close();
                }
                return result;
            }
            else 
                return stream;
        } catch (Exception ex123) {
            return null;
        }
    }

    /**
     * Disposes this instance.
     */
    @Override
    public void close() {
        try {
            if (temporaryStream_ != null) {
                temporaryStream_.close();
                temporaryStream_ = null;
            }
        } catch (Exception ex123) {
        }
    }

    private com.pullenti.unisharp.MemoryStream temporaryStream_;

    private com.pullenti.unisharp.MemoryStream finalStream_;
    public MemoryArchiveStorage() {
        this(FileUpdateMode.DIRECT);
    }
}
