public class DefaultHttpDataSource extends java.lang.Object implements HttpDataSource
HttpDataSource
that uses Android's HttpURLConnection
.
By default this implementation will not follow cross-protocol redirects (i.e. redirects from
HTTP to HTTPS or vice versa). Cross-protocol redirects can be enabled by using the
#DefaultHttpDataSource(String, Predicate, TransferListener, int, int, boolean,
RequestProperties)
constructor and passing true
as the second last argument.
HttpDataSource.BaseFactory, HttpDataSource.Factory, HttpDataSource.HttpDataSourceException, HttpDataSource.InvalidContentTypeException, HttpDataSource.InvalidResponseCodeException, HttpDataSource.RequestProperties
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_CONNECT_TIMEOUT_MILLIS
The default connection timeout, in milliseconds.
|
static int |
DEFAULT_READ_TIMEOUT_MILLIS
The default read timeout, in milliseconds.
|
REJECT_PAYWALL_TYPES
Constructor and Description |
---|
DefaultHttpDataSource(java.lang.String userAgent,
Predicate<java.lang.String> contentTypePredicate) |
DefaultHttpDataSource(java.lang.String userAgent,
Predicate<java.lang.String> contentTypePredicate,
TransferListener<? super DefaultHttpDataSource> listener) |
DefaultHttpDataSource(java.lang.String userAgent,
Predicate<java.lang.String> contentTypePredicate,
TransferListener<? super DefaultHttpDataSource> listener,
int connectTimeoutMillis,
int readTimeoutMillis) |
DefaultHttpDataSource(java.lang.String userAgent,
Predicate<java.lang.String> contentTypePredicate,
TransferListener<? super DefaultHttpDataSource> listener,
int connectTimeoutMillis,
int readTimeoutMillis,
boolean allowCrossProtocolRedirects,
HttpDataSource.RequestProperties defaultRequestProperties) |
Modifier and Type | Method and Description |
---|---|
protected long |
bytesRead()
Returns the number of bytes that have been read since the most recent call to
open(DataSpec) . |
protected long |
bytesRemaining()
Returns the number of bytes that are still to be read for the current
DataSpec . |
protected long |
bytesSkipped()
Returns the number of bytes that have been skipped since the most recent call to
open(DataSpec) . |
void |
clearAllRequestProperties()
Clears all request headers that were set by
HttpDataSource.setRequestProperty(String, String) . |
void |
clearRequestProperty(java.lang.String name)
Clears the value of a request header.
|
void |
close()
Closes the source.
|
protected java.net.HttpURLConnection |
getConnection()
Returns the current connection, or null if the source is not currently opened.
|
java.util.Map<java.lang.String,java.util.List<java.lang.String>> |
getResponseHeaders()
Returns the headers provided in the response, or
null if response headers are
unavailable. |
android.net.Uri |
getUri()
When the source is open, returns the
Uri from which data is being read. |
long |
open(DataSpec dataSpec)
Opens the source 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 . |
void |
setRequestProperty(java.lang.String name,
java.lang.String value)
Sets the value of a request header.
|
public static final int DEFAULT_CONNECT_TIMEOUT_MILLIS
public static final int DEFAULT_READ_TIMEOUT_MILLIS
public DefaultHttpDataSource(java.lang.String userAgent, Predicate<java.lang.String> contentTypePredicate)
userAgent
- The User-Agent string that should be used.contentTypePredicate
- An optional Predicate
. If a content type is rejected by the
predicate then a HttpDataSource.InvalidContentTypeException
is thrown from
open(DataSpec)
.public DefaultHttpDataSource(java.lang.String userAgent, Predicate<java.lang.String> contentTypePredicate, TransferListener<? super DefaultHttpDataSource> listener)
userAgent
- The User-Agent string that should be used.contentTypePredicate
- An optional Predicate
. If a content type is rejected by the
predicate then a HttpDataSource.InvalidContentTypeException
is thrown from
open(DataSpec)
.listener
- An optional listener.public DefaultHttpDataSource(java.lang.String userAgent, Predicate<java.lang.String> contentTypePredicate, TransferListener<? super DefaultHttpDataSource> listener, int connectTimeoutMillis, int readTimeoutMillis)
userAgent
- The User-Agent string that should be used.contentTypePredicate
- An optional Predicate
. If a content type is rejected by the
predicate then a HttpDataSource.InvalidContentTypeException
is thrown from
open(DataSpec)
.listener
- An optional listener.connectTimeoutMillis
- The connection timeout, in milliseconds. A timeout of zero is
interpreted as an infinite timeout.readTimeoutMillis
- The read timeout, in milliseconds. A timeout of zero is interpreted
as an infinite timeout.public DefaultHttpDataSource(java.lang.String userAgent, Predicate<java.lang.String> contentTypePredicate, TransferListener<? super DefaultHttpDataSource> listener, int connectTimeoutMillis, int readTimeoutMillis, boolean allowCrossProtocolRedirects, HttpDataSource.RequestProperties defaultRequestProperties)
userAgent
- The User-Agent string that should be used.contentTypePredicate
- An optional Predicate
. If a content type is rejected by the
predicate then a HttpDataSource.InvalidContentTypeException
is thrown from
open(DataSpec)
.listener
- An optional listener.connectTimeoutMillis
- The connection timeout, in milliseconds. A timeout of zero is
interpreted as an infinite timeout. Pass DEFAULT_CONNECT_TIMEOUT_MILLIS
to use
the default value.readTimeoutMillis
- The read timeout, in milliseconds. A timeout of zero is interpreted
as an infinite timeout. Pass DEFAULT_READ_TIMEOUT_MILLIS
to use the default value.allowCrossProtocolRedirects
- Whether cross-protocol redirects (i.e. redirects from HTTP
to HTTPS and vice versa) are enabled.defaultRequestProperties
- The default request properties to be sent to the server as
HTTP headers or null
if not required.public android.net.Uri getUri()
DataSource
Uri
from which data is being read. The returned
Uri
will be identical to the one passed DataSource.open(DataSpec)
in the DataSpec
unless redirection has occurred. If redirection has occurred, the Uri
after redirection
is returned.getUri
in interface DataSource
Uri
from which data is being read, or null if the source is not open.public java.util.Map<java.lang.String,java.util.List<java.lang.String>> getResponseHeaders()
HttpDataSource
null
if response headers are
unavailable.getResponseHeaders
in interface HttpDataSource
public void setRequestProperty(java.lang.String name, java.lang.String value)
HttpDataSource
setRequestProperty
in interface HttpDataSource
name
- The name of the header field.value
- The value of the field.public void clearRequestProperty(java.lang.String name)
HttpDataSource
clearRequestProperty
in interface HttpDataSource
name
- The name of the header field.public void clearAllRequestProperties()
HttpDataSource
HttpDataSource.setRequestProperty(String, String)
.clearAllRequestProperties
in interface HttpDataSource
public long open(DataSpec dataSpec) throws HttpDataSource.HttpDataSourceException
DataSource
Note: If an IOException
is thrown, callers must still call DataSource.close()
to ensure
that any partial effects of the invocation are cleaned up.
open
in interface DataSource
open
in interface HttpDataSource
dataSpec
- Defines the data to be read.DataSpec.length
equals C.LENGTH_UNSET
) this value
is the resolved length of the request, or C.LENGTH_UNSET
if the length is still
unresolved. For all other requests, the value returned will be equal to the request's
DataSpec.length
.HttpDataSource.HttpDataSourceException
public int read(byte[] buffer, int offset, int readLength) throws HttpDataSource.HttpDataSourceException
DataSource
length
bytes of data and stores them into buffer
, starting at
index offset
.
If length
is zero then 0 is returned. Otherwise, if no data is available because the
end of the opened range has been reached, then C.RESULT_END_OF_INPUT
is returned.
Otherwise, the call will block until at least one byte of data has been read and the number of
bytes read is returned.
read
in interface DataSource
read
in interface HttpDataSource
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 no data is available
because the end of the opened range has been reached.HttpDataSource.HttpDataSourceException
public void close() throws HttpDataSource.HttpDataSourceException
DataSource
Note: This method must 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
close
in interface HttpDataSource
HttpDataSource.HttpDataSourceException
protected final java.net.HttpURLConnection getConnection()
protected final long bytesSkipped()
open(DataSpec)
.protected final long bytesRead()
open(DataSpec)
.protected final long bytesRemaining()
DataSpec
.
If the total length of the data being read is known, then this length minus bytesRead()
is returned. If the total length is unknown, C.LENGTH_UNSET
is returned.
C.LENGTH_UNSET
.