CacheDataSource.Factory


public final class CacheDataSource.Factory implements DataSource.Factory


DataSource.Factory for CacheDataSource instances.

Summary

Public constructors

Public methods

CacheDataSource

Creates a DataSource instance.

CacheDataSource

Returns an instance suitable for downloading content.

CacheDataSource

Returns an instance suitable for reading cached content as part of removing a download.

@Nullable Cache

Returns the cache that will be used, or null if setCache has yet to be called.

CacheKeyFactory

Returns the CacheKeyFactory that will be used.

@Nullable PriorityTaskManager

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

CacheDataSource.Factory

Sets the cache that will be used.

CacheDataSource.Factory

Sets the CacheKeyFactory.

CacheDataSource.Factory

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

CacheDataSource.Factory

Sets the DataSink.Factory for generating DataSinks for writing data to the cache.

CacheDataSource.Factory

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

CacheDataSource.Factory

Sets the CacheDataSource.Flags.

CacheDataSource.Factory

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

CacheDataSource.Factory

Sets the priority to use when requesting data from upstream.

CacheDataSource.Factory

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

Public fields

cacheKeyFactory

public CacheKeyFactory cacheKeyFactory

upstreamPriorityTaskManager

public @Nullable PriorityTaskManager upstreamPriorityTaskManager

Public constructors

Factory

public Factory()

Public methods

createDataSource

public CacheDataSource createDataSource()

Creates a DataSource instance.

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:

Returns
CacheDataSource

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 FLAG_BLOCK_ON_CACHE is always set.
  • The task priority is overridden to be PRIORITY_DOWNLOAD.
Returns
CacheDataSource

An instance suitable for reading cached content as part of removing a download.

getCache

public @Nullable Cache getCache()

Returns the cache that will be used, or null if setCache has yet to be called.

getCacheKeyFactory

public CacheKeyFactory getCacheKeyFactory()

Returns the CacheKeyFactory that will be used.

getUpstreamPriorityTaskManager

public @Nullable PriorityTaskManager getUpstreamPriorityTaskManager()

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

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 cache

The cache that will be used.

Returns
CacheDataSource.Factory

This factory.

setCacheKeyFactory

@CanIgnoreReturnValue
public CacheDataSource.Factory setCacheKeyFactory(CacheKeyFactory cacheKeyFactory)

Sets the CacheKeyFactory.

The default is DEFAULT.

Parameters
CacheKeyFactory cacheKeyFactory

The CacheKeyFactory.

Returns
CacheDataSource.Factory

This factory.

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
DataSource.Factory cacheReadDataSourceFactory

The DataSource.Factory for reading from the cache.

Returns
CacheDataSource.Factory

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
@Nullable DataSink.Factory cacheWriteDataSinkFactory

The DataSink.Factory for generating DataSinks for writing data to the cache, or null to disable writing.

Returns
CacheDataSource.Factory

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
@Nullable CacheDataSource.EventListener eventListener

The EventListener.

Returns
CacheDataSource.Factory

This factory.

setFlags

@CanIgnoreReturnValue
public CacheDataSource.Factory setFlags(@CacheDataSource.Flags int flags)

Sets the CacheDataSource.Flags.

The default is 0.

Parameters
@CacheDataSource.Flags int flags

The CacheDataSource.Flags.

Returns
CacheDataSource.Factory

This factory.

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
@Nullable DataSource.Factory upstreamDataSourceFactory

The upstream DataSource for reading data not in the cache, or null to cause failure in the case of a cache miss.

Returns
CacheDataSource.Factory

This factory.

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.

The default is PRIORITY_PLAYBACK.

Parameters
int upstreamPriority

The priority to use when requesting data from upstream.

Returns
CacheDataSource.Factory

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
@Nullable PriorityTaskManager upstreamPriorityTaskManager

The upstream PriorityTaskManager.

Returns
CacheDataSource.Factory

This factory.