/*
 * 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 hard disks.
 */
public class DiskArchiveStorage extends BaseArchiveStorage {

    /**
     * Initializes a new instance of the <see cref="DiskArchiveStorage"/> class.
     * @param file The file.
     * @param _updateMode The update mode.
     */
    public DiskArchiveStorage(ZipFile file, FileUpdateMode _updateMode) throws Exception {
        super(_updateMode);
        if (file.getName() == null) 
            throw new Exception("Cant handle non file archives");
        fileName_ = file.getName();
    }

    /**
     * Gets a temporary output <see cref="Stream"/> for performing updates on.
     * @return Returns the temporary output stream.
     */
    @Override
    public com.pullenti.unisharp.Stream getTemporaryOutput() {
        try {
            if (temporaryName_ != null) {
                temporaryName_ = getTempFileName(temporaryName_, true);
                temporaryStream_ = new com.pullenti.unisharp.FileStream(temporaryName_, "rw", "");
            }
            else {
                temporaryName_ = "temp.bin";
                temporaryStream_ = new com.pullenti.unisharp.FileStream(temporaryName_, "rw", "");
            }
            return temporaryStream_;
        } catch (Exception ex123) {
            return null;
        }
    }

    /**
     * Converts a 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");
            com.pullenti.unisharp.Stream result = null;
            String moveTempName = getTempFileName(fileName_, false);
            boolean newFileCreated = false;
            try {
                temporaryStream_.close();
                com.pullenti.unisharp.Utils.moveFile(fileName_, moveTempName);
                com.pullenti.unisharp.Utils.moveFile(temporaryName_, fileName_);
                newFileCreated = true;
                (new java.io.File(moveTempName)).delete();
                result = new com.pullenti.unisharp.FileStream(fileName_, "r", "");
            } catch (Exception ex556) {
                result = null;
                if (!newFileCreated) {
                    com.pullenti.unisharp.Utils.moveFile(moveTempName, fileName_);
                    (new java.io.File(temporaryName_)).delete();
                }
                throw ex556;
            }
            return result;
        } catch (Exception ex123) {
            return null;
        }
    }

    /**
     * Make a temporary copy of a 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 {
            stream.close();
            temporaryName_ = getTempFileName(fileName_, true);
            com.pullenti.unisharp.Utils.copyFile(fileName_, temporaryName_);
            temporaryStream_ = new com.pullenti.unisharp.FileStream(temporaryName_, "rw", "");
            return temporaryStream_;
        } catch (Exception ex123) {
            return null;
        }
    }

    /**
     * Return a stream suitable for performing direct updates on the original source.
     * @param stream The current stream.
     * @return Returns a stream suitable for direct updating.
     */
    @Override
    public com.pullenti.unisharp.Stream openForDirectUpdate(com.pullenti.unisharp.Stream stream) {
        try {
            com.pullenti.unisharp.Stream result;
            if ((stream == null) || !stream.canWrite()) {
                if (stream != null) 
                    stream.close();
                return new com.pullenti.unisharp.FileStream(fileName_, "rw", "");
            }
            else 
                result = stream;
            return result;
        } catch (Exception ex123) {
            return null;
        }
    }

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

    private static String getTempFileName(String original, boolean makeTempFile) {
        String result = null;
        if (original == null) 
            result = "temp.bin";
        else {
            int counter = 0;
            int suffixSeed = java.time.LocalDateTime.now().getSecond();
            while (result == null) {
                counter += 1;
                String newName = (original + "." + suffixSeed + counter + ".tmp");
                if (!((new java.io.File(newName)).exists() && (new java.io.File(newName)).isFile())) {
                    if (makeTempFile) {
                        try {
                            try (com.pullenti.unisharp.FileStream stream = new com.pullenti.unisharp.FileStream(newName, "rw", "t")) {
                            }
                            result = newName;
                        } catch (Exception ex557) {
                            suffixSeed = java.time.LocalDateTime.now().getSecond();
                        }
                    }
                    else 
                        result = newName;
                }
            }
        }
        return result;
    }

    private com.pullenti.unisharp.Stream temporaryStream_;

    private String fileName_;

    private String temporaryName_;
    public DiskArchiveStorage() {
        super();
    }
}
