public final class DefaultUriDataSource extends java.lang.Object implements UriDataSource
UriDataSource
that supports multiple URI schemes. The supported schemes are:
Constructor and Description |
---|
DefaultUriDataSource(android.content.Context context,
java.lang.String userAgent)
Constructs a new instance.
|
DefaultUriDataSource(android.content.Context context,
TransferListener listener,
java.lang.String userAgent)
Constructs a new instance.
|
DefaultUriDataSource(android.content.Context context,
TransferListener listener,
java.lang.String userAgent,
boolean allowCrossProtocolRedirects)
Constructs a new instance, optionally configured to follow cross-protocol redirects.
|
DefaultUriDataSource(android.content.Context context,
TransferListener listener,
UriDataSource httpDataSource)
Constructs a new instance, using a provided
HttpDataSource for fetching remote data. |
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes the
DataSource . |
java.lang.String |
getUri()
When the source is open, returns the URI from which data is being read.
|
long |
open(DataSpec dataSpec)
Opens the
DataSource to read the specified data. |
int |
read(byte[] buffer,
int offset,
int readLength)
Reads up to
length bytes of data and stores them into buffer , starting at
index offset . |
public DefaultUriDataSource(android.content.Context context, java.lang.String userAgent)
The constructed instance will not follow cross-protocol redirects (i.e. redirects from HTTP to
HTTPS or vice versa) when fetching remote data. Cross-protocol redirects can be enabled by
using DefaultUriDataSource(Context, TransferListener, String, boolean)
and passing
true
as the final argument.
context
- A context.userAgent
- The User-Agent string that should be used when requesting remote data.public DefaultUriDataSource(android.content.Context context, TransferListener listener, java.lang.String userAgent)
The constructed instance will not follow cross-protocol redirects (i.e. redirects from HTTP to
HTTPS or vice versa) when fetching remote data. Cross-protocol redirects can be enabled by
using DefaultUriDataSource(Context, TransferListener, String, boolean)
and passing
true
as the final argument.
context
- A context.listener
- An optional TransferListener
.userAgent
- The User-Agent string that should be used when requesting remote data.public DefaultUriDataSource(android.content.Context context, TransferListener listener, java.lang.String userAgent, boolean allowCrossProtocolRedirects)
context
- A context.listener
- An optional TransferListener
.userAgent
- The User-Agent string that should be used when requesting remote data.allowCrossProtocolRedirects
- Whether cross-protocol redirects (i.e. redirects from HTTP
to HTTPS and vice versa) are enabled when fetching remote data..public DefaultUriDataSource(android.content.Context context, TransferListener listener, UriDataSource httpDataSource)
HttpDataSource
for fetching remote data.context
- A context.listener
- An optional TransferListener
.httpDataSource
- UriDataSource
to use for non-file URIs.public long open(DataSpec dataSpec) throws java.io.IOException
DataSource
DataSource
to read the specified data. Calls to DataSource.open(DataSpec)
and
DataSource.close()
must be balanced.
Note: If DataSource.open(DataSpec)
throws an IOException
, callers must still call
DataSource.close()
to ensure that any partial effects of the DataSource.open(DataSpec)
invocation
are cleaned up. Implementations of this class can assume that callers will call
DataSource.close()
in this case.
open
in interface DataSource
dataSpec
- Defines the data to be read.DataSpec.length
equals C.LENGTH_UNBOUNDED
) this value
is the resolved length of the request, or C.LENGTH_UNBOUNDED
if the length is still
unresolved. For all other requests, the value returned will be equal to the request's
DataSpec.length
.java.io.IOException
- If an error occurs opening the source.public int read(byte[] buffer, int offset, int readLength) throws java.io.IOException
DataSource
length
bytes of data and stores them into buffer
, starting at
index offset
.
This method blocks until at least one byte of data can be read, the end of the opened range is detected, or an exception is thrown.
read
in interface DataSource
buffer
- The buffer into which the read data should be stored.offset
- The start offset into buffer
at which data should be written.readLength
- The maximum number of bytes to read.C.RESULT_END_OF_INPUT
if the end of the opened
range is reached.java.io.IOException
- If an error occurs reading from the source.public java.lang.String getUri()
UriDataSource
If redirection occurred, the URI after redirection is the one returned.
getUri
in interface UriDataSource
public void close() throws java.io.IOException
DataSource
DataSource
.
Note: This method will be called even if the corresponding call to DataSource.open(DataSpec)
threw an IOException
. See DataSource.open(DataSpec)
for more details.
close
in interface DataSource
java.io.IOException
- If an error occurs closing the source.