interface DataSource : 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

A factory for DataSource instances.

Public functions

Unit

Adds a TransferListener to listen to data transfers.

Unit

Closes the source.

(Mutable)Map<String!, (Mutable)List<String!>!>!

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

Uri?

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

Long

Opens the source to read the specified data.

Inherited functions

From androidx.media3.common.DataReader
Int
read(buffer: ByteArray!, offset: Int, length: Int)

Reads up to length bytes of data from the input.

Public functions

addTransferListener

@UnstableApi
fun addTransferListener(transferListener: TransferListener!): Unit

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

Parameters
transferListener: TransferListener!

A TransferListener.

close

@UnstableApi
fun close(): Unit

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
fun getResponseHeaders(): (Mutable)Map<String!, (Mutable)List<String!>!>!

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
fun getUri(): Uri?

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
Uri?

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

open

@UnstableApi
fun open(dataSpec: DataSpec!): Long

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.