public interface DataSource extends DataReader

Known direct subclasses
AesCipherDataSource

A DataSource that decrypts the data read from an upstream source.

BaseDataSource

Base DataSource implementation to keep a list of TransferListeners.

CacheDataSource

A DataSource that reads and writes a Cache.

DefaultDataSource

A DataSource that supports multiple URI schemes.

HttpDataSource

An HTTP DataSource.

PlaceholderDataSource

A DataSource which provides no data.

PriorityDataSource

A DataSource that can be used as part of a task registered with a .

ResolvingDataSource

DataSource wrapper allowing just-in-time resolution of DataSpecs.

StatsDataSource

DataSource wrapper which keeps track of bytes transferred, redirected uris, and response headers.

TeeDataSource

Tees data into a DataSink as the data is read.

Known indirect subclasses
AssetDataSource

A DataSource for reading from a local asset.

ByteArrayDataSource

A DataSource for reading from a byte array.

ContentDataSource

A DataSource for reading from a content URI.

CronetDataSource

DataSource without intermediate buffer based on Cronet API set using UrlRequest.

DataSchemeDataSource

A DataSource for reading data URLs, as defined by RFC 2397.

DefaultHttpDataSource

An HttpDataSource that uses Android's HttpURLConnection.

FakeDataSource

A fake DataSource capable of simulating various scenarios.

FileDataSource

A DataSource for reading local files.

FileDescriptorDataSource

A DataSource for reading from a FileDescriptor.

HttpEngineDataSource

DataSource without intermediate buffer based on HttpEngine set using UrlRequest.

MediaDataSourceAdapter

A DataSource for reading from a MediaDataSource.

OkHttpDataSource

An HttpDataSource that delegates to Square's Call.Factory.

RawResourceDataSource

A DataSource for reading a raw resource.

RtmpDataSource

A Real-Time Messaging Protocol (RTMP) DataSource.

UdpDataSource

A UDP DataSource.


Reads data from URI-identified resources.

Summary

Nested types

public interface DataSource.Factory

A factory for DataSource instances.

Public methods

abstract void

Adds a TransferListener to listen to data transfers.

abstract void

Closes the source.

default Map<StringList<String>>

When the source is open, returns the response headers associated with the last open call.

abstract @Nullable Uri

When the source is open, returns the Uri from which data is being read.

abstract long

Opens the source to read the specified data.

Inherited methods

From androidx.media3.common.DataReader
abstract int
read(byte[] buffer, int offset, int length)

Reads up to length bytes of data from the input.

Public methods

addTransferListener

@UnstableApi
abstract void addTransferListener(TransferListener transferListener)

Adds a TransferListener to listen to data transfers. This method is not thread-safe.

Parameters
TransferListener transferListener

A TransferListener.

close

@UnstableApi
abstract void close()

Closes the source. This method must be called even if the corresponding call to open threw an IOException.

Throws
java.io.IOException

If an error occurs closing the source.

getResponseHeaders

@UnstableApi
default Map<StringList<String>> getResponseHeaders()

When the source is open, returns the response headers associated with the last open call. Otherwise, returns an empty map.

Key look-up in the returned map is case-insensitive.

getUri

@UnstableApi
abstract @Nullable Uri getUri()

When the source is open, returns the Uri from which data is being read. The returned Uri will be identical to the one passed open in the DataSpec unless redirection has occurred. If redirection has occurred, the Uri after redirection is returned.

Returns
@Nullable Uri

The Uri from which data is being read, or null if the source is not open.

open

@UnstableApi
abstract long open(DataSpec dataSpec)

Opens the source to read the specified data. If an IOException is thrown, callers must still call close to ensure that any partial effects of the invocation are cleaned up.

The following edge case behaviors apply:

Parameters
DataSpec dataSpec

Defines the data to be read.

Returns
long

The number of bytes that can be read from the opened source. For unbounded requests (i.e., requests where length equals LENGTH_UNSET) this value is the resolved length of the request, or LENGTH_UNSET if the length is still unresolved. For all other requests, the value returned will be equal to the request's length.

Throws
java.io.IOException

If an error occurs opening the source. DataSourceException can be thrown or used as a cause of the thrown exception to specify the reason of the error.