/*
 * @(#)Saver.java
 *
 * Summary: used to save names of selected files to a temporary cache for CommandLine.
 *
 * Copyright: (c) 2003-2010 Roedy Green, Canadian Mind Products, http://mindprod.com
 *
 * Licence: This software may be copied and used freely for any purpose but military.
 *          http://mindprod.com/contact/nonmil.html
 *
 * Requires: JDK 1.5+
 *
 * Created with: IntelliJ IDEA IDE.
 *
 * Version History:
 *  2.5 2009-02-28 - CommandLine now uses considerably less RAM by caching the list of files on disk.
 *                   ou no longer need specify estimatedFiles.
 *                   pit off into its own package.
 */
package com.mindprod.commandline;

import com.mindprod.hunkio.HunkIO;

import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * used to save names of selected files to a temporary cache for CommandLine.
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 2.5 2009-02-28 - CommandLine now uses considerably less RAM by caching the list of files on disk.
 *          ou no longer need specify estimatedFiles.
 *          pit off into its own package.
 * @since 2009-02-28
 */
class Saver
    {
    // ------------------------------ FIELDS ------------------------------

    /**
     * write binary UTF-8 Strings
     */
    private DataOutputStream cache;

    // -------------------------- OTHER METHODS --------------------------

    /**
     * close the cache
     */
    void close()
        {
        try
            {
            cache.close();
            }
        catch ( IOException e )
            {
            throw new IllegalArgumentException( "Problem closing temporary cache of selected files." );
            }
        }

    /**
     * crceate and open the cache to save selected file names to
     *
     * @return File where names will be saved
     */
    File open()
        {
        try
            {
            File cacheFile = HunkIO.createTempFile( "selectedfiles", ".temp", null );
            // Arrange to have file eventually deleted.
            // We can't delete it on Retriever.close, since we might iterate the files again.
            cacheFile.deleteOnExit();
            FileOutputStream fos = new FileOutputStream( cacheFile, false );
            BufferedOutputStream bos = new BufferedOutputStream( fos, 4096 /* buffsize in bytes */ );
            cache = new DataOutputStream( bos );
            return cacheFile;
            }
        catch ( IOException e )
            {
            throw new IllegalArgumentException( "Problem creating temporary cache of selected files." );
            }
        }

    /**
     * save one line in the cache
     *
     * @param line the name of the file to save
     */
    void save( String line )
        {
        try
            {
            cache.writeUTF( line );
            }
        catch ( IOException e )
            {
            throw new IllegalArgumentException( "Problem writing temporary cache of selected files." );
            }
        }
    }
