Class DownloadManager

java.lang.Object
ca.corbett.extras.io.DownloadManager

public class DownloadManager extends Object
Manages file downloads via http and reports on their progress, completion status, or error status. You could of course just manually instantiate DownloadThread instances and fire them off, but this manager class creates and owns a single, reusable, thread-safe HttpClient that makes launching multiple download requests much cleaner. It also keeps track of downloads in progress so that we can offer methods like isDownloadInProgress() and stopAllDownloads().
Since:
swing-extras 2.5
Author:
scorbo2
  • Field Details

    • CONNECTION_TIMEOUT_SECONDS

      public static final int CONNECTION_TIMEOUT_SECONDS
      See Also:
    • DOWNLOAD_TIMEOUT_SECONDS

      public static final int DOWNLOAD_TIMEOUT_SECONDS
      See Also:
  • Constructor Details

    • DownloadManager

      public DownloadManager()
  • Method Details

    • downloadFile

      public void downloadFile(URL url, DownloadListener listener)
      Downloads a file from the given URL and saves it in the system temp directory.
    • downloadFile

      public void downloadFile(URL url, File targetDir, DownloadListener listener)
      Downloads a file from the given URL and saves it to the specified path.
      Parameters:
      url - The URL to download from (supported protocols: http, https, file)
      targetDir - Where to save the file
      listener - An optional DownloadListener to receive progress/failure/completion notifications.
    • isDownloadInProgress

      public boolean isDownloadInProgress()
      Returns true if at least one download is currently running.
    • stopAllDownloads

      public void stopAllDownloads()
      Sends a kill() request to all active download threads. Does not guarantee that they will stop immediately. Any download currently in progress will report a download failure.
    • createDownloadThread

      public DownloadThread createDownloadThread(URL url, File targetDir, DownloadListener listener)
      Creates and returns a DownloadThread suitable for executing the given download. You can use the downloadFile() wrapper method instead, to both create and automatically start the thread.
    • close

      public void close()
      Closes the HTTP client.
    • getFileExtension

      public static String getFileExtension(String filename)
      If the given String represents a filename with an extension, this will return the final dot character and the extension. Otherwise, an empty string is returned. Examples:
      • getFileExtension("hello.txt"); // returns ".txt"
      • getFileExtension("hello.txt.jpg"); // returns ".jpg"
      • getFileExtension("That. Does not make sense"); // returns ". Does not make sense"
      • getFileExtension("hello"); // returns ""
    • getFilenameComponent

      public static String getFilenameComponent(String path)
      Looks for the last / character in the given path string and returns everything after it. This can be used for URLs or for linux-style filesystem paths. Examples:
      • getFilenameComponent("/path/to/file.txt"); // returns "file.txt"
      • getFilenameComponent("no slash character"); // returns ""
      • getFilenameComponent("/slash/at/end/"); // return ""