Package ca.corbett.extras.io
Class DownloadManager
java.lang.Object
ca.corbett.extras.io.DownloadManager
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 Summary
FieldsModifier and TypeFieldDescriptionstatic final intstatic final int -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes the HTTP client.createDownloadThread(URL url, File targetDir, DownloadListener listener) Creates and returns a DownloadThread suitable for executing the given download.voiddownloadFile(URL url, DownloadListener listener) Downloads a file from the given URL and saves it in the system temp directory.voiddownloadFile(URL url, File targetDir, DownloadListener listener) Downloads a file from the given URL and saves it to the specified path.static StringgetFileExtension(String filename) If the given String represents a filename with an extension, this will return the final dot character and the extension.static StringgetFilenameComponent(String path) Looks for the last / character in the given path string and returns everything after it.booleanReturns true if at least one download is currently running.voidSends a kill() request to all active download threads.
-
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
Downloads a file from the given URL and saves it in the system temp directory. -
downloadFile
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 filelistener- 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
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
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
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 ""
-