All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mindsnacks.zinc.classes.jobs.AbstractZincDownloadFileJob Maven / Gradle / Ivy

There is a newer version: 1.6.2
Show newest version
package com.mindsnacks.zinc.classes.jobs;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

/**
 * User: NachoSoto
 * Date: 9/4/13
 */
public abstract class AbstractZincDownloadFileJob extends AbstractZincDownloadJob {
    private final File mFile;
    private final boolean mOverride;

    public AbstractZincDownloadFileJob(final ZincRequestExecutor requestExecutor,
                                       final URL url,
                                       final File root,
                                       final String child,
                                       final boolean override) {
        super(requestExecutor, url, File.class);

        mFile = new File(root, child);
        mOverride = override;
    }

    @Override
    public final File run() throws DownloadFileError {
        if (shouldDownloadFile()) {
            final InputStream inputStream = mRequestExecutor.get(mUrl);

            try {
                writeFile(inputStream, mFile);
            } catch (IOException e) {
                throw new DownloadFileError("Error writing to file '" + mFile.getAbsolutePath() + "'", e);
            }
        } else {
            logMessage("not downloading file - was already there.");
        }

        return mFile;
    }

    private boolean shouldDownloadFile() {
        return mOverride || !mFile.exists();
    }

    abstract protected void writeFile(final InputStream inputStream, final File outputFile) throws IOException;
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy