net.technicpack.launchercore.util.DownloadUtils.java Source code

Java tutorial

Introduction

Here is the source code for net.technicpack.launchercore.util.DownloadUtils.java

Source

/*
 * This file is part of Technic Launcher Core.
 * Copyright (C) 2013 Syndicate, LLC
 *
 * Technic Launcher Core is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Technic Launcher Core is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License,
 * as well as a copy of the GNU Lesser General Public License,
 * along with Technic Launcher Core.  If not, see <http://www.gnu.org/licenses/>.
 */

package net.technicpack.launchercore.util;

import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.commons.io.FileUtils;

import net.technicpack.launchercore.exception.DownloadException;
import net.technicpack.launchercore.util.Download.Result;

public class DownloadUtils {
    private static final int DOWNLOAD_RETRIES = 3;

    public static String getETag(String address) {
        String md5 = "";

        try {
            URL url = new URL(address);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setDoInput(true);
            conn.setDoOutput(false);
            System.setProperty("http.agent",
                    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.162 Safari/535.19");
            conn.setRequestProperty("User-Agent",
                    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.162 Safari/535.19");
            HttpURLConnection.setFollowRedirects(true);
            conn.setUseCaches(false);
            conn.setInstanceFollowRedirects(true);

            String eTag = conn.getHeaderField("ETag");
            if (eTag != null) {
                eTag = eTag.replaceAll("^\"|\"$", "");
                if (eTag.length() == 32) {
                    md5 = eTag;
                }
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return md5;
    }

    public static Download downloadFile(String url, String name, String output, File cache, String md5,
            DownloadListener listener) throws IOException {
        int tries = DOWNLOAD_RETRIES;
        File outputFile = null;
        Download download = null;
        while (tries > 0) {
            System.out.println("Starting download of " + url + ", with " + tries + " tries remaining");
            tries--;
            download = new Download(url, name, output);
            download.setListener(listener);
            download.run();
            if (download.getResult() != Result.SUCCESS) {
                if (download.getOutFile() != null) {
                    download.getOutFile().delete();
                }
                System.err.println("Download of " + url + " Failed!");
                if (listener != null) {
                    listener.stateChanged("Download failed, retries remaining: " + tries, 0F);
                }
            } else {
                if (md5 != null && !md5.isEmpty()) {
                    String resultMD5 = MD5Utils.getMD5(download.getOutFile());

                    System.out.println("Expected MD5: " + md5 + " Calculated MD5: " + resultMD5);
                    if (md5.equalsIgnoreCase(resultMD5)) {
                        outputFile = download.getOutFile();
                        break;
                    }
                } else {
                    outputFile = download.getOutFile();
                    break;
                }
            }
        }
        if (outputFile == null) {
            throw new DownloadException("Failed to download " + url,
                    download != null ? download.getException() : null);
        }
        if (cache != null) {
            FileUtils.copyFile(outputFile, cache);
        }
        return download;
    }

    public static Download downloadFile(String url, String name, String output, File cache) throws IOException {
        return downloadFile(url, name, output, cache, null, null);
    }

    public static Download downloadFile(String url, String name, String output) throws IOException {
        return downloadFile(url, name, output, null);
    }
}