Class DownloadHelper

java.lang.Object
gaiasky.util.DownloadHelper

public class DownloadHelper extends Object
Useful utilities that help with downloads and connectivity checks.
  • Constructor Details

    • DownloadHelper

      public DownloadHelper()
  • Method Details

    • checkInternetConnection

      public static void checkInternetConnection(Runnable success, Runnable fail)
      Checks for an internet connection and runs the appropriate callback depending on the outcome.
      Parameters:
      success - Success callback.
      fail - Failure callback.
    • 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 target FileHandle where 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 the fail callback will be executed.
      progressDownload - A ProgressRunnable that reports the progress of the download (may be null).
      progressHashResume - A ProgressRunnable that reports the progress of resuming the download (may be null).
      finish - A Consumer<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 - A Runnable callback that is invoked if the download fails or is canceled.
      cancel - A Runnable callback that is invoked if the download is canceled by the user.
      Returns:
      An Net.HttpRequest object 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

      public static void testConnection(String url, Consumer<String> finish, Runnable fail)
      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 - A Consumer<String> callback that is invoked upon successful completion of the download. It receives a status message for possible logging.
      fail - A Runnable callback 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 given Net.HttpRequest with 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 - A BiConsumer callback that runs if the connection is successful, with a message and the status code.
      fail - A Runnable callback that runs if the connection fails.