Class FileDownloader

java.lang.Object
de.gustavblass.commons.FileDownloader

public class FileDownloader extends Object
A utility class that downloads files from the internet.
See Also:
  • Field Details

    • LOG

      private static final org.apache.logging.log4j.Logger LOG
    • userAgent

      @Nullable private @Nullable String userAgent
      The user-agent string to be sent with HTTP requests.
    • doNotTrack

      private boolean doNotTrack
      Whether to send the Do-Not-Track header with HTTP requests.
    • globalPrivacyControl

      private boolean globalPrivacyControl
      Whether to send the Global-Privacy-Control header with HTTP requests.
  • Constructor Details

    • FileDownloader

      public FileDownloader()
  • Method Details

    • setUserAgent

      public void setUserAgent(@NonNull @NonNull String userAgent) throws de.gustavblass.commons.exceptions.IllegalArgumentException
      Updates the userAgent string to be sent with HTTP requests.
      Parameters:
      userAgent - The new user agent string. Must not be empty or blank.
      Throws:
      de.gustavblass.commons.exceptions.IllegalArgumentException - If the user agent string is empty or blank.
    • resetUserAgent

      public void resetUserAgent()
      Deletes the custom userAgent string previously set.
    • getUserAgent

      @NonNull public @NonNull Optional<String> getUserAgent()
      Retrieves the custom userAgent string to be sent with HTTP requests.
      Returns:
      The currently set user agent. Empty if none is set.
    • enableDoNotTrack

      public void enableDoNotTrack()
      Enables that the doNotTrack header is sent with any HTTP requests.
    • disableDoNotTrack

      public void disableDoNotTrack()
      Disables that the doNotTrack header is sent with any HTTP requests.
    • enableGlobalPrivacyControl

      public void enableGlobalPrivacyControl()
      Enables that the globalPrivacyControl header is sent with any HTTP requests.
    • disableGlobalPrivacyControl

      public void disableGlobalPrivacyControl()
      Disables that the globalPrivacyControl header is sent with any HTTP requests.
    • download

      public void download(@NonNull @NonNull URL url, @NonNull @NonNull File target) throws IOException, de.gustavblass.commons.exceptions.IllegalArgumentException, InterruptedException

      Retrieves a file of arbitrary format from the server available at the given web address to the specified destination.

      Sends the custom userAgent string if set. Otherwise, the default user agent of the Java HTTP client is used.

      Sends the Do-Not-Track signal if doNotTrack is set to true. Similarly, sends the Global-Privacy-Control signal if globalPrivacyControl is set to true.

      Parameters:
      url - The publicly available web address of the file to be downloaded. Must not require authentication, JavaScript execution, user interaction, cookies or a browser engine.
      target - The file to which the downloaded content will be written. Must be a file, not a directory.
      Throws:
      de.gustavblass.commons.exceptions.IllegalArgumentException - If the target file exists but is a directory or if the path to the target file is invalid.
      IOException - If an error occurs while downloading the file.
      InterruptedException - If the Thread is interrupted while waiting for the download to finish.
    • readFile

      private static Optional<byte[]> readFile(@NonNull @NonNull File file)
      Extracts the content of the given local file.
      Parameters:
      file - The file to be read. May be of any format, i.e. plain text (TXT, JSON, CSV etc.) or binary (PNG, PDF etc.)
      Returns:
      The content of the file. Empty if the file does not exist or an error occurs while reading it.
    • writeFile

      private static void writeFile(byte[] content, @NonNull @NonNull File target)
      Saves the content to the given local file.
      Parameters:
      content - The data to be written to the file. May be of any format, i.e. plain text (TXT, JSON, CSV etc.). Will not be modified.
      target - The file to which the given data will be written.