Class DownloadHelper
java.lang.Object
gaiasky.util.DownloadHelper
Useful utilities that help with downloads and connectivity checks.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidcheckInternetConnection(Runnable success, Runnable fail) Checks for an internet connection and runs the appropriate callback depending on the outcome.static com.badlogic.gdx.Net.HttpRequestdownloadFile(String url, com.badlogic.gdx.files.FileHandle to, boolean offlineMode, ProgressRunnable progressDownload, ProgressRunnable progressHashResume, Consumer<String> finish, Runnable fail, Runnable cancel) Downloads a file from the specified URL, with support for resuming interrupted downloads.static voidsendRequest(com.badlogic.gdx.Net.HttpRequest request, String url, boolean resume, com.badlogic.gdx.files.FileHandle to, long startSize, ProgressRunnable progressDownload, ProgressRunnable progressHashResume, Consumer<String> finish, Runnable fail, Runnable cancel) Send the givenNet.HttpRequestwith the given parameters.static voidsendTestRequest(com.badlogic.gdx.Net.HttpRequest request, String url, BiConsumer<String, Integer> finish, Runnable fail) Sends a simple HTTP HEAD request to the given URL to test the connection.static voidtestConnection(String url, Consumer<String> finish, Runnable fail) Tests if the provided URL is reachable.
-
Constructor Details
-
DownloadHelper
public DownloadHelper()
-
-
Method Details
-
checkInternetConnection
-
downloadFile
public static com.badlogic.gdx.Net.HttpRequest downloadFile(String url, com.badlogic.gdx.files.FileHandle to, boolean offlineMode, ProgressRunnable progressDownload, ProgressRunnable progressHashResume, Consumer<String> finish, Runnable fail, Runnable cancel) Downloads a file from the specified URL, with support for resuming interrupted downloads. A new thread is spawned to handle the download, and various progress and completion callbacks are provided. If the URL points to a local file (using the 'file://' protocol), it simply copies the file locally.- Parameters:
url- The URL from which to download the file, or a local file path prefixed with 'file://'.to- The targetFileHandlewhere the downloaded file will be saved.offlineMode- A flag indicating whether the download should proceed in offline mode. If true, the download will be skipped, and thefailcallback will be executed.progressDownload- AProgressRunnablethat reports the progress of the download (may be null).progressHashResume- AProgressRunnablethat reports the progress of resuming the download (may be null).finish- AConsumer<String>callback that is invoked upon successful completion of the download. It receives an empty string if the file is downloaded successfully or a hash if the download was resumed.fail- ARunnablecallback that is invoked if the download fails or is canceled.cancel- ARunnablecallback that is invoked if the download is canceled by the user.- Returns:
- An
Net.HttpRequestobject representing the HTTP request for the download. It can be used to cancel or modify the request if needed. - Throws:
IllegalArgumentException- if the provided URL is not valid or cannot be parsed.
-
testConnection
Tests if the provided URL is reachable. If reachable, runs the finish callback. If not reachable, runs the fail callback. This method handles both files and directories.For file URLs, it checks if the file exists and is readable. For HTTP URLs, it checks if the server is responsive to a HEAD request. If the URL points to a directory, it checks if the server redirects to a default file or responds with a directory listing.
- Parameters:
url- The URL to test, or a local file path prefixed with 'file://'.finish- AConsumer<String>callback that is invoked upon successful completion of the download. It receives a status message for possible logging.fail- ARunnablecallback that is invoked if the download fails or is canceled.
-
sendRequest
public static void sendRequest(com.badlogic.gdx.Net.HttpRequest request, String url, boolean resume, com.badlogic.gdx.files.FileHandle to, long startSize, ProgressRunnable progressDownload, ProgressRunnable progressHashResume, Consumer<String> finish, Runnable fail, Runnable cancel) Send the givenNet.HttpRequestwith the given parameters.- Parameters:
request- The request.url- The URL.resume- Whether to resume past downloads.to- File to save the response.startSize- Partial start size.progressDownload- Runnable to run during download progress.progressHashResume- Runnable to run during hashing progress.finish- Runnable to run if the download finished successfully.fail- Runnable to run if the download failed.cancel- Runnable to run if the download was cancelled.
-
sendTestRequest
public static void sendTestRequest(com.badlogic.gdx.Net.HttpRequest request, String url, BiConsumer<String, Integer> finish, Runnable fail) Sends a simple HTTP HEAD request to the given URL to test the connection. If the server responds successfully (status code 2xx), the finish callback is called. If the server responds with an error or the request fails, the fail callback is called.- Parameters:
request- The request.url- The URL.finish- ABiConsumercallback that runs if the connection is successful, with a message and the status code.fail- ARunnablecallback that runs if the connection fails.
-