/*
 * @(#)Retriever.java
 *
 * Summary: used to retrieve names of selected files from 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 java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * used to retrieve names of selected files from a temporary cache for Commandline.
 * <p/>
 * You no longer need specify estimatedFiles.
 * Spit off into its own package.
 * <p/>
 * @noinspection WeakerAccess
 *
 * @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 2003
 */
class Retriever
    {
    // ------------------------------ FIELDS ------------------------------

    /**
     * cache containing binary UTF-8 Strings
     */
    private DataInputStream cache;

    private final File cacheFile;

    // --------------------------- CONSTRUCTORS ---------------------------

    /**
     * constructor
     *
     * @param cacheFile cache file used by Saver
     */
    Retriever( File cacheFile )
        {
        this.cacheFile = cacheFile;
        }

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

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

    /**
     * fetch the next line from the cache
     *
     * @return the next string from the cache.
     */
    String fetch()
        {
        try
            {
            return cache.readUTF();
            }
        catch ( IOException e )
            {
            throw new IllegalArgumentException( "Problem reading temporary cache of selected files." );
            }
        }

    /**
     * open the cache file for read
     */
    void open()
        {
        try
            {
            FileInputStream fis = new FileInputStream( cacheFile );
            BufferedInputStream bis = new BufferedInputStream( fis, 4096 );
            cache = new DataInputStream( bis );
            }
        catch ( IOException e )
            {
            throw new IllegalArgumentException( "Problem opening temporary cache of selected files." );
            }
        }
    }
