Package com.google.android.exoplayer2.upstream.cache

## Class CacheDataSource.Factory

- java.lang.Object
- - com.google.android.exoplayer2.upstream.cache.CacheDataSource.Factory

- All Implemented Interfaces:
    `DataSource.Factory`
  Enclosing class:
    CacheDataSource

      public static final class CacheDataSource.Factory
      extends Object
      implements DataSource.Factory

  `DataSource.Factory` for `CacheDataSource` instances.

- - ### Constructor Summary

    Constructors
    Constructor | Description
    --- | ---
    `Factory()` |

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method | Description
    --- | --- | ---
    `CacheDataSource` | `createDataSource()` | Creates a `DataSource` instance.
    `CacheDataSource` | `createDataSourceForDownloading()` | Returns an instance suitable for downloading content.
    `CacheDataSource` | `createDataSourceForRemovingDownload()` | Returns an instance suitable for reading cached content as part of removing a download.
    `Cache` | `getCache()` | Returns the cache that will be used, or `null` if `setCache(com.google.android.exoplayer2.upstream.cache.Cache)` has yet to be called.
    `CacheKeyFactory` | `getCacheKeyFactory()` | Returns the `CacheKeyFactory` that will be used.
    `PriorityTaskManager` | `getUpstreamPriorityTaskManager()` | Returns the `PriorityTaskManager` that will bs used when requesting data from upstream, or `null` if there is none.
    `CacheDataSource.Factory` | `setCache​(Cache cache)` | Sets the cache that will be used.
    `CacheDataSource.Factory` | `setCacheKeyFactory​(CacheKeyFactory cacheKeyFactory)` | Sets the `CacheKeyFactory`.
    `CacheDataSource.Factory` | `setCacheReadDataSourceFactory​(DataSource.Factory cacheReadDataSourceFactory)` | Sets the `DataSource.Factory` for `DataSources` for reading from the cache.
    `CacheDataSource.Factory` | `setCacheWriteDataSinkFactory​(DataSink.Factory cacheWriteDataSinkFactory)` | Sets the `DataSink.Factory` for generating `DataSinks` for writing data to the cache.
    `CacheDataSource.Factory` | `setEventListener​(CacheDataSource.EventListener eventListener)` | Sets the {link EventListener} to which events are delivered.
    `CacheDataSource.Factory` | `setFlags​(@com.google.android.exoplayer2.upstream.cache.CacheDataSource.Flags int flags)` | Sets the `CacheDataSource.Flags`.
    `CacheDataSource.Factory` | `setUpstreamDataSourceFactory​(DataSource.Factory upstreamDataSourceFactory)` | Sets the `DataSource.Factory` for upstream `DataSources`, which are used to read data in the case of a cache miss.
    `CacheDataSource.Factory` | `setUpstreamPriority​(int upstreamPriority)` | Sets the priority to use when requesting data from upstream.
    `CacheDataSource.Factory` | `setUpstreamPriorityTaskManager​(PriorityTaskManager upstreamPriorityTaskManager)` | Sets an optional `PriorityTaskManager` to use when requesting data from upstream.

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

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

- - ### Constructor Detail

    - #### Factory

          public Factory()

  - ### Method Detail

    - #### setCache

          @CanIgnoreReturnValue
          public CacheDataSource.Factory setCache​(Cache cache)

      Sets the cache that will be used.

      Must be called before the factory is used.

      Parameters:
        `cache` - The cache that will be used.
      Returns:
        This factory.

    - #### getCache

          @Nullable
          public Cache getCache()

      Returns the cache that will be used, or `null` if `setCache(com.google.android.exoplayer2.upstream.cache.Cache)` has yet to be called.

    - #### setCacheReadDataSourceFactory

          @CanIgnoreReturnValue
          public CacheDataSource.Factory setCacheReadDataSourceFactory​(DataSource.Factory cacheReadDataSourceFactory)

      Sets the `DataSource.Factory` for `DataSources` for reading from the cache.

      The default is a `FileDataSource.Factory` in its default configuration.

      Parameters:
        `cacheReadDataSourceFactory` - The `DataSource.Factory` for reading from the cache.
      Returns:
        This factory.

    - #### setCacheWriteDataSinkFactory

          @CanIgnoreReturnValue
          public CacheDataSource.Factory setCacheWriteDataSinkFactory​(@Nullable
                                                                      DataSink.Factory cacheWriteDataSinkFactory)

      Sets the `DataSink.Factory` for generating `DataSinks` for writing data to the cache. Passing `null` causes the cache to be read-only.

      The default is a `CacheDataSink.Factory` in its default configuration.

      Parameters:
        `cacheWriteDataSinkFactory` - The `DataSink.Factory` for generating `DataSinks` for writing data to the cache, or `null` to disable writing.
      Returns:
        This factory.

    - #### setCacheKeyFactory

          @CanIgnoreReturnValue
          public CacheDataSource.Factory setCacheKeyFactory​(CacheKeyFactory cacheKeyFactory)

      Sets the `CacheKeyFactory`.

      The default is `CacheKeyFactory.DEFAULT`.

      Parameters:
        `cacheKeyFactory` - The `CacheKeyFactory`.
      Returns:
        This factory.

    - #### getCacheKeyFactory

          public CacheKeyFactory getCacheKeyFactory()

      Returns the `CacheKeyFactory` that will be used.

    - #### setUpstreamDataSourceFactory

          @CanIgnoreReturnValue
          public CacheDataSource.Factory setUpstreamDataSourceFactory​(@Nullable
                                                                      DataSource.Factory upstreamDataSourceFactory)

      Sets the `DataSource.Factory` for upstream `DataSources`, which are used to read data in the case of a cache miss.

      The default is `null`, and so this method must be called before the factory is used in order for data to be read from upstream in the case of a cache miss.

      Parameters:
        `upstreamDataSourceFactory` - The upstream `DataSource` for reading data not in the cache, or `null` to cause failure in the case of a cache miss.
      Returns:
        This factory.

    - #### setUpstreamPriorityTaskManager

          @CanIgnoreReturnValue
          public CacheDataSource.Factory setUpstreamPriorityTaskManager​(@Nullable
                                                                        PriorityTaskManager upstreamPriorityTaskManager)

      Sets an optional `PriorityTaskManager` to use when requesting data from upstream.

      If set, reads from the upstream `DataSource` will only be allowed to proceed if there are no higher priority tasks registered to the `PriorityTaskManager`. If there exists a higher priority task then `PriorityTaskManager.PriorityTooLowException` will be thrown instead.

      Note that requests to `CacheDataSource` instances are intended to be used as parts of (possibly larger) tasks that are registered with the `PriorityTaskManager`, and hence `CacheDataSource` does not register a task by itself. This must be done by the surrounding code that uses the `CacheDataSource` instances.

      The default is `null`.

      Parameters:
        `upstreamPriorityTaskManager` - The upstream `PriorityTaskManager`.
      Returns:
        This factory.

    - #### getUpstreamPriorityTaskManager

          @Nullable
          public PriorityTaskManager getUpstreamPriorityTaskManager()

      Returns the `PriorityTaskManager` that will bs used when requesting data from upstream, or `null` if there is none.

    - #### setUpstreamPriority

          @CanIgnoreReturnValue
          public CacheDataSource.Factory setUpstreamPriority​(int upstreamPriority)

      Sets the priority to use when requesting data from upstream. The priority is only used if a `PriorityTaskManager` is set by calling `setUpstreamPriorityTaskManager(com.google.android.exoplayer2.util.PriorityTaskManager)`.

      The default is `C.PRIORITY_PLAYBACK`.

      Parameters:
        `upstreamPriority` - The priority to use when requesting data from upstream.
      Returns:
        This factory.

    - #### setFlags

          @CanIgnoreReturnValue
          public CacheDataSource.Factory setFlags​(@com.google.android.exoplayer2.upstream.cache.CacheDataSource.Flags int flags)

      Sets the `CacheDataSource.Flags`.

      The default is `0`.

      Parameters:
        `flags` - The `CacheDataSource.Flags`.
      Returns:
        This factory.

    - #### setEventListener

          @CanIgnoreReturnValue
          public CacheDataSource.Factory setEventListener​(@Nullable
                                                          CacheDataSource.EventListener eventListener)

      Sets the {link EventListener} to which events are delivered.

      The default is `null`.

      Parameters:
        `eventListener` - The `CacheDataSource.EventListener`.
      Returns:
        This factory.

    - #### createDataSource

          public CacheDataSource createDataSource()

      Description copied from interface: `DataSource.Factory`
      Creates a `DataSource` instance.
      Specified by:
        `createDataSource` in interface `DataSource.Factory`

    - #### createDataSourceForDownloading

          public CacheDataSource createDataSourceForDownloading()

      Returns an instance suitable for downloading content. The created instance is equivalent to one that would be created by `createDataSource()`, except:
      - The `CacheDataSource.FLAG_BLOCK_ON_CACHE` is always set.
      - The task priority is overridden to be `C.PRIORITY_DOWNLOAD`.
      Returns:
        An instance suitable for downloading content.

    - #### createDataSourceForRemovingDownload

          public CacheDataSource createDataSourceForRemovingDownload()

      Returns an instance suitable for reading cached content as part of removing a download. The created instance is equivalent to one that would be created by `createDataSource()`, except:
      - The upstream is overridden to be `null`, since when removing content we don't want to request anything that's not already cached.
      - The `CacheDataSource.FLAG_BLOCK_ON_CACHE` is always set.
      - The task priority is overridden to be `C.PRIORITY_DOWNLOAD`.
      Returns:
        An instance suitable for reading cached content as part of removing a download.