Class Downloads

java.lang.Object
com.marcpg.libpg.web.Downloads

public final class Downloads extends Object
Provides utility methods for downloading files in various ways.
Please keep in mind that these methods won't reach the speeds of very optimized download methods that web browsers for example use.
Since:
0.0.5
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    The default buffer size that's used in download(URL, File), if the buffer size isn't specified.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    download(URL url, File destination)
    Downloads a file from a specified URL and saves it to a system file using byte buffers, for improved speed and reliability.
    static void
    download(URL url, File destination, int bufferSize)
    Downloads a file from a specified URL and buffer size and saves it to a system file using byte buffers, for improved speed and reliability.
    static void
    simpleDownload(@NotNull URL url, File destination)
    Downloads a file from a specified URL and saves it to a system file using a simple output stream without any buffers.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • Downloads

      public Downloads()
  • Method Details

    • download

      public static void download(URL url, File destination) throws IOException
      Downloads a file from a specified URL and saves it to a system file using byte buffers, for improved speed and reliability. When the connection is reset while the download is still running, it will stop and throw an IO Exception.
      Parameters:
      url - The file's path as a URL.
      destination - Where the downloaded file should be stored.
      Throws:
      IOException - if there is an error while downloading the file, such as a connection reset.
      See Also:
    • download

      public static void download(URL url, File destination, int bufferSize) throws IOException
      Downloads a file from a specified URL and buffer size and saves it to a system file using byte buffers, for improved speed and reliability. When the connection is reset while the download is still running, it will stop and throw an IO Exception.
      The buffer size impacts performance and memory usage. A higher buffer size is faster in most cases, but also uses more memory.
      Parameters:
      url - The file's path as a URL.
      destination - Where the downloaded file should be stored.
      bufferSize - The size of the buffer to use. Bigger buffers may be faster, but also use more memory.
      Throws:
      IOException - if there is an error while downloading the file, such as a connection reset.
      See Also:
    • simpleDownload

      public static void simpleDownload(@NotNull @NotNull URL url, File destination) throws IOException
      Downloads a file from a specified URL and saves it to a system file using a simple output stream without any buffers. In some cases, this can turn out faster than download(URL, File), but in most, it will not be faster, as it doesn't use buffers.
      Parameters:
      url - The file's path as a URL.
      destination - Where the downloaded file should be stored.
      Throws:
      IOException - if there is an error while downloading the file, such as a connection reset.