Class Downloader


  • public class Downloader
    extends Object
    • Field Detail

      • DEFAULT_CONNECTION_TIMEOUT

        public static final int DEFAULT_CONNECTION_TIMEOUT
        Default connection timeout in milliseconds when using FileUtils.copyURLToFile(URL, File, int, int)
        See Also:
        Constant Field Values
      • DEFAULT_READ_TIMEOUT

        public static final int DEFAULT_READ_TIMEOUT
        Default read timeout in milliseconds when using FileUtils.copyURLToFile(URL, File, int, int)
        See Also:
        Constant Field Values
    • Method Detail

      • download

        public static void download​(String name,
                                    URL url,
                                    File f,
                                    String targetMD5,
                                    int maxTries,
                                    int connectionTimeout,
                                    int readTimeout)
                             throws IOException
        Download the specified URL to the specified file, and verify that the target MD5 matches
        Parameters:
        name - Name (mainly for providing useful exceptions)
        url - URL to download
        f - Destination file
        targetMD5 - Expected MD5 for file
        maxTries - Maximum number of download attempts before failing and throwing an exception
        connectionTimeout - connection timeout in milliseconds, as used by FileUtils.copyURLToFile(URL, File, int, int)
        readTimeout - read timeout in milliseconds, as used by FileUtils.copyURLToFile(URL, File, int, int)
        Throws:
        IOException - If an error occurs during downloading
      • downloadAndExtract

        public static void downloadAndExtract​(String name,
                                              URL url,
                                              File f,
                                              File extractToDir,
                                              String targetMD5,
                                              int maxTries,
                                              int connectionTimeout,
                                              int readTimeout)
                                       throws IOException
        Download the specified URL to the specified file, verify that the MD5 matches, and then extract it to the specified directory.
        Note that the file must be an archive, with the correct file extension: .zip, .jar, .tar.gz, .tgz or .gz
        Parameters:
        name - Name (mainly for providing useful exceptions)
        url - URL to download
        f - Destination file
        extractToDir - Destination directory to extract all files
        targetMD5 - Expected MD5 for file
        maxTries - Maximum number of download attempts before failing and throwing an exception
        connectionTimeout - connection timeout in milliseconds, as used by FileUtils.copyURLToFile(URL, File, int, int)
        readTimeout - read timeout in milliseconds, as used by FileUtils.copyURLToFile(URL, File, int, int)
        Throws:
        IOException - If an error occurs during downloading
      • deleteIfCorrupted

        public static boolean deleteIfCorrupted​(File inputFile,
                                                String targetmd5)
                                         throws IOException
        Check if the input file is corrupted by verifying the target md5 hash. If corrupted, file is deleted
        Parameters:
        inputFile - the input file to check
        targetmd5 - the target md5 to test
        Throws:
        IOException
      • checkMD5OfFile

        public static boolean checkMD5OfFile​(String targetMD5,
                                             File file)
                                      throws IOException
        Check the MD5 of the specified file
        Parameters:
        targetMD5 - Expected MD5
        file - File to check
        Returns:
        True if MD5 matches, false otherwise
        Throws:
        IOException