Interface Downloader

  • All Known Implementing Classes:
    LocalFileAwareDownloader

    public interface Downloader
    Implementations provide a general mechanism for downloading files.
    • Method Detail

      • downloadAndStream

        @Nullable
        java.io.InputStream downloadAndStream​(@NonNull
                                              java.net.URL url,
                                              @NonNull
                                              ProgressIndicator indicator)
                                       throws java.io.IOException
        Gets a stream associated with the content at the given URL. This could be achieved by downloading the file completely, streaming it directly, or some combination.
        Parameters:
        url - The URL to fetch.
        indicator - Facility for showing download progress and logging.
        Returns:
        An InputStream corresponding to the specified content, or null if the download is cancelled.
        Throws:
        java.io.IOException
      • downloadFully

        @Nullable
        java.nio.file.Path downloadFully​(@NonNull
                                         java.net.URL url,
                                         @NonNull
                                         ProgressIndicator indicator)
                                  throws java.io.IOException
        Downloads the content at the given URL to a temporary file and returns a handle to that file.
        Parameters:
        url - The URL to fetch.
        indicator - Facility for showing download progress and logging.
        Returns:
        The temporary file, or null if the download is cancelled.
        Throws:
        java.io.IOException
      • downloadFully

        void downloadFully​(@NonNull
                           java.net.URL url,
                           @NonNull
                           java.nio.file.Path target,
                           @Nullable
                           Checksum checksum,
                           @NonNull
                           ProgressIndicator indicator)
                    throws java.io.IOException
        Downloads the content at the given URL to the given file. Calling this method instructs the downloader to fetch the content from the original server and bypass any caches.

        For example, in case of HTTP connections, calling this method may be equivalent to adding "Cache-Control: no-cache" and "Pragma: no-cache" headers to the request, thus instructing the network facilities that no caching is expected to take place (e.g., on proxy servers).

        Parameters:
        url - The URL to fetch.
        target - The location to download to.
        checksum - If specified, first check target to see if the given checksum matches the existing file. If so, returns immediately.
        indicator - Facility for showing download progress and logging.
        Throws:
        java.io.IOException
      • downloadFullyWithCaching

        default void downloadFullyWithCaching​(@NonNull
                                              java.net.URL url,
                                              @NonNull
                                              java.nio.file.Path target,
                                              @Nullable
                                              Checksum checksum,
                                              @NonNull
                                              ProgressIndicator indicator)
                                       throws java.io.IOException
        Downloads the content at the given URL to the given file, using caching techniques if possible. Which specific caching techniques to use is defined by the implementation. As far as the contract goes, calling this method instructs the downloader that the caller does not require the content to be fetched from the original server, and a previously cached copy may be re-used.

        Note that calling this method does not guarantee caching or a better performance: it is an implementation detail how exactly this hint is used. For example, when checking for software updates availability, the caller will know that no caching must be used, so it should not call this method, and call downloadFully(java.net.URL, com.android.repository.api.ProgressIndicator) instead. On the other hand, for a large download which is not expected to change on the original server, the caller typically would not mind against intrinsic optimizations and caching, so in that case it should call this method.

        Parameters:
        url - The URL to fetch.
        target - The location to download to.
        checksum - If specified, first check target to see if the given checksum matches the existing file. If so, returns immediately.
        indicator - Facility for showing download progress and logging.
        Throws:
        java.io.IOException
      • setDownloadIntermediatesLocation

        default void setDownloadIntermediatesLocation​(@NonNull
                                                      java.nio.file.Path intermediatesLocation)
        For implementations that support resuming partial downloads, sets the directory location where to download intermediates (i.e. partial) files. If the intermediate directory belongs to a different file system and or partition, there will be an additional overhead of moving the intermediate file to its final destination after a full download is complete.

        If the directory is invalid (or inaccessible, or the file system is full), implementors will ignore this value and use the default behavior.

        By default, implementations use the same intermediate directory as the destination directory of the download.

        Parameters:
        intermediatesLocation - the path to use for download intermediates
      • hash

        @NonNull
        static java.lang.String hash​(@NonNull
                                     java.io.InputStream in,
                                     long fileSize,
                                     @NonNull
                                     java.lang.String algorithm,
                                     @NonNull
                                     ProgressIndicator progress)
                              throws java.io.IOException
        Hash the given input stream.
        Parameters:
        in - The stream to hash. It will be fully consumed but not closed.
        fileSize - The expected length of the stream, for progress display purposes.
        progress - The indicator will be updated with the expected completion fraction.
        Returns:
        The sha1 hash of the input stream.
        Throws:
        java.io.IOException - IF there's a problem reading from the stream.