Package com.google.android.exoplayer2.offline

## Class SegmentDownloader<M extends FilterableManifest<M>>

- java.lang.Object
- - com.google.android.exoplayer2.offline.SegmentDownloader<M>

- Type Parameters:
    `M` - The type of the manifest object.
  All Implemented Interfaces:
    `Downloader`
  Direct Known Subclasses:
    `DashDownloader`, `HlsDownloader`, `SsDownloader`

      public abstract class SegmentDownloader<M extends FilterableManifest<M>>
      extends Object
      implements Downloader

  Base class for multi segment stream downloaders.

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class | Description
    --- | --- | ---
    `protected static class` | `SegmentDownloader.Segment` | Smallest unit of content to be downloaded.

    - ### Nested classes/interfaces inherited from interface com.google.android.exoplayer2.offline.Downloader

      `Downloader.ProgressListener`

  - ### Constructor Summary

    Constructors
    Constructor | Description
    --- | ---
    `SegmentDownloader​(MediaItem mediaItem, ParsingLoadable.Parser<M> manifestParser, CacheDataSource.Factory cacheDataSourceFactory, Executor executor)` |

  - ### Method Summary

    All Methods Static Methods Instance Methods Abstract Methods Concrete Methods
    Modifier and Type | Method | Description
    --- | --- | ---
    `void` | `cancel()` | Permanently cancels the downloading by this downloader.
    `void` | `download​(Downloader.ProgressListener progressListener)` | Downloads the content.
    `protected <T> T` | `execute​(RunnableFutureTask<T,​?> runnable, boolean removing)` | Executes the provided `RunnableFutureTask`.
    `protected static DataSpec` | `getCompressibleDataSpec​(Uri uri)` |
    `protected M` | `getManifest​(DataSource dataSource, DataSpec dataSpec, boolean removing)` | Loads and parses a manifest.
    `protected abstract List<SegmentDownloader.Segment>` | `getSegments​(DataSource dataSource, M manifest, boolean removing)` | Returns a list of all downloadable `SegmentDownloader.Segment`s for a given manifest.
    `void` | `remove()` | Removes the content.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### SegmentDownloader

          public SegmentDownloader​(MediaItem mediaItem,
                                   ParsingLoadable.Parser<M> manifestParser,
                                   CacheDataSource.Factory cacheDataSourceFactory,
                                   Executor executor)

      Parameters:
        `mediaItem` - The `MediaItem` to be downloaded.
        `manifestParser` - A parser for manifests belonging to the media to be downloaded.
        `cacheDataSourceFactory` - A `CacheDataSource.Factory` for the cache into which the download will be written.
        `executor` - An `Executor` used to make requests for the media being downloaded. Providing an `Executor` that uses multiple threads will speed up the download by allowing parts of it to be executed in parallel.

  - ### Method Detail

    - #### download

          public final void download​(@Nullable
                                     Downloader.ProgressListener progressListener)
                              throws IOException,
                                     InterruptedException

      Description copied from interface: `Downloader`
      Downloads the content.

      If downloading fails, this method can be called again to resume the download. It cannot be called again after the download has been `canceled`.

      If downloading is canceled whilst this method is executing, then it is expected that it will return reasonably quickly. However, there are no guarantees about how the method will return, meaning that it can return without throwing, or by throwing any of its documented exceptions. The caller must use its own knowledge about whether downloading has been canceled to determine whether this is why the method has returned, rather than relying on the method returning in a particular way.

      Specified by:
        `download` in interface `Downloader`
      Parameters:
        `progressListener` - A listener to receive progress updates, or `null`.
      Throws:
        `IOException` - If the download failed to complete successfully.
        `InterruptedException` - If the download was interrupted.

    - #### cancel

          public void cancel()

      Description copied from interface: `Downloader`
      Permanently cancels the downloading by this downloader. The caller should also interrupt the downloading thread immediately after calling this method.

      Once canceled, `Downloader.download(com.google.android.exoplayer2.offline.Downloader.ProgressListener)` cannot be called again.

      Specified by:
        `cancel` in interface `Downloader`

    - #### remove

          public final void remove()

      Description copied from interface: `Downloader`
      Removes the content.
      Specified by:
        `remove` in interface `Downloader`

    - #### getManifest

          protected final M getManifest​(DataSource dataSource,
                                        DataSpec dataSpec,
                                        boolean removing)
                                 throws InterruptedException,
                                        IOException

      Loads and parses a manifest.
      Parameters:
        `dataSpec` - The manifest `DataSpec`.
        `removing` - Whether the manifest is being loaded as part of the download being removed.
      Returns:
        The loaded manifest.
      Throws:
        `InterruptedException` - If the thread on which the method is called is interrupted.
        `IOException` - If an error occurs during execution.

    - #### execute

          protected final <T> T execute​(RunnableFutureTask<T,​?> runnable,
                                        boolean removing)
                                 throws InterruptedException,
                                        IOException

      Executes the provided `RunnableFutureTask`.
      Parameters:
        `runnable` - The `RunnableFutureTask` to execute.
        `removing` - Whether the execution is part of the download being removed.
      Returns:
        The result.
      Throws:
        `InterruptedException` - If the thread on which the method is called is interrupted.
        `IOException` - If an error occurs during execution.

    - #### getSegments

          protected abstract List<SegmentDownloader.Segment> getSegments​(DataSource dataSource,
                                                                         M manifest,
                                                                         boolean removing)
                                                                  throws IOException,
                                                                         InterruptedException

      Returns a list of all downloadable `SegmentDownloader.Segment`s for a given manifest. Any required data should be loaded using `getManifest(com.google.android.exoplayer2.upstream.DataSource, com.google.android.exoplayer2.upstream.DataSpec, boolean)` or `execute(com.google.android.exoplayer2.util.RunnableFutureTask<T, ?>, boolean)`.
      Parameters:
        `dataSource` - The `DataSource` through which to load any required data.
        `manifest` - The manifest containing the segments.
        `removing` - Whether the segments are being obtained as part of a removal. If true then a partial segment list is returned in the case that a load error prevents all segments from being listed. If false then an `IOException` will be thrown in this case.
      Returns:
        The list of downloadable `SegmentDownloader.Segment`s.
      Throws:
        `IOException` - Thrown if `allowPartialIndex` is false and an execution error occurs, or if the media is not in a form that allows for its segments to be listed.
        `InterruptedException`

    - #### getCompressibleDataSpec

          protected static DataSpec getCompressibleDataSpec​(Uri uri)