Interface Downloader

  • All Superinterfaces:
    java.lang.AutoCloseable

    public interface Downloader
    extends java.lang.AutoCloseable
    Some sort of downloader for files.

    Handles fetching strings and binary content from remotes as well.

    Access may be cached.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Interface Description
      static class  Downloader.ResolveMode
      A mode to control usage of any potential locale cache
    • Method Summary

      All Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      java.nio.file.Path baseDir()
      Get the base directory used for this downloader.
      void close()  
      java.util.concurrent.CompletableFuture<ResolutionResult<java.nio.file.Path>> download​(java.net.URL source, java.lang.String destination)
      Download a file to the provided relative location.
      java.util.concurrent.CompletableFuture<ResolutionResult<java.nio.file.Path>> downloadAndValidate​(java.net.URL source, java.lang.String destination, HashAlgorithm algorithm, java.lang.String hash)  
      java.util.concurrent.CompletableFuture<ResolutionResult<byte[]>> readBytes​(java.net.URL source, java.lang.String relativePath)
      Read the contents of source as a byte[].
      java.util.concurrent.CompletableFuture<ResolutionResult<byte[]>> readBytesAndValidate​(java.net.URL source, java.lang.String relativePath, HashAlgorithm algorithm, java.lang.String hash)
      Read the contents of source as a byte[].
      java.util.concurrent.CompletableFuture<ResolutionResult<java.lang.String>> readString​(java.net.URL source, java.lang.String relativePath)
      Read the contents of source as a String.
      java.util.concurrent.CompletableFuture<ResolutionResult<java.lang.String>> readStringAndValidate​(java.net.URL source, java.lang.String relativePath, HashAlgorithm algorithm, java.lang.String hash)
      Read the contents of source as a String.
      Downloader withBaseDir​(java.nio.file.Path override)
      Return a new downloader with a new base directory, but sharing this downloader's resources.
    • Method Detail

      • baseDir

        java.nio.file.Path baseDir()
        Get the base directory used for this downloader.
        Returns:
        the base directory
      • withBaseDir

        Downloader withBaseDir​(java.nio.file.Path override)
        Return a new downloader with a new base directory, but sharing this downloader's resources.

        Closing the returned downloader must not have any effect on this instance.

        Parameters:
        override - the new base directory
        Returns:
        a derived downloader
      • readString

        java.util.concurrent.CompletableFuture<ResolutionResult<java.lang.String>> readString​(java.net.URL source,
                                                                                              java.lang.String relativePath)
        Read the contents of source as a String.

        Encoding will be determined based on response headers, defaulting to UTF-8.

        Parameters:
        source - the URL to download from
        relativePath - the location to use as a cache key for the result
        Returns:
        a future providing the completed result
      • readStringAndValidate

        java.util.concurrent.CompletableFuture<ResolutionResult<java.lang.String>> readStringAndValidate​(java.net.URL source,
                                                                                                         java.lang.String relativePath,
                                                                                                         HashAlgorithm algorithm,
                                                                                                         java.lang.String hash)
        Read the contents of source as a String.

        The downloaded content and any cached information will be validated against the provided hash.

        Parameters:
        source - the URL to download from
        relativePath - the location to use as a cache key for the result
        algorithm - the hash algorithm to test with
        hash - the expected hash, as a string of hex digits
        Returns:
        a future providing the resolved result
      • readBytes

        java.util.concurrent.CompletableFuture<ResolutionResult<byte[]>> readBytes​(java.net.URL source,
                                                                                   java.lang.String relativePath)
        Read the contents of source as a byte[].
        Parameters:
        source - the URL to download from
        relativePath - the location to use as a cache key for the result
        Returns:
        a future providing the completed result
      • readBytesAndValidate

        java.util.concurrent.CompletableFuture<ResolutionResult<byte[]>> readBytesAndValidate​(java.net.URL source,
                                                                                              java.lang.String relativePath,
                                                                                              HashAlgorithm algorithm,
                                                                                              java.lang.String hash)
        Read the contents of source as a byte[].

        The downloaded content and any cached information will be validated against the provided hash.

        Parameters:
        source - the URL to download from
        relativePath - the location to use as a cache key for the result
        algorithm - the hash algorithm to test with
        hash - the expected hash, as a string of hex digits
        Returns:
        a future providing the resolved result
      • download

        java.util.concurrent.CompletableFuture<ResolutionResult<java.nio.file.Path>> download​(java.net.URL source,
                                                                                              java.lang.String destination)
        Download a file to the provided relative location.

        If the file already exists, it may not be overwritten, depending on the downloader's caching policy.

        Parameters:
        source - the URL to download from.
        destination - a relative path to download to
        Returns:
        a future returning the downloaded patch once a download is complete
      • downloadAndValidate

        java.util.concurrent.CompletableFuture<ResolutionResult<java.nio.file.Path>> downloadAndValidate​(java.net.URL source,
                                                                                                         java.lang.String destination,
                                                                                                         HashAlgorithm algorithm,
                                                                                                         java.lang.String hash)
      • close

        void close()
            throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Throws:
        java.io.IOException