public class DefaultHttpDataSource extends 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)
constructor and passing true
as the final argument.
HttpDataSource.HttpDataSourceException, HttpDataSource.InvalidContentTypeException, HttpDataSource.InvalidResponseCodeException
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(String userAgent,
Predicate<String> contentTypePredicate) |
DefaultHttpDataSource(String userAgent,
Predicate<String> contentTypePredicate,
TransferListener listener) |
DefaultHttpDataSource(String userAgent,
Predicate<String> contentTypePredicate,
TransferListener listener,
int connectTimeoutMillis,
int readTimeoutMillis) |
DefaultHttpDataSource(String userAgent,
Predicate<String> contentTypePredicate,
TransferListener listener,
int connectTimeoutMillis,
int readTimeoutMillis,
boolean allowCrossProtocolRedirects) |
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 header fields that were set by
HttpDataSource.setRequestProperty(String, String) . |
void |
clearRequestProperty(String name)
Clears the value of a request header field.
|
void |
close()
Closes the
DataSource . |
protected HttpURLConnection |
getConnection()
Returns the current connection, or null if the source is not currently opened.
|
Map<String,List<String>> |
getResponseHeaders()
Gets the headers provided in the response.
|
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 . |
void |
setRequestProperty(String name,
String value)
Sets the value of a request header field.
|
public static final int DEFAULT_CONNECT_TIMEOUT_MILLIS
public static final int DEFAULT_READ_TIMEOUT_MILLIS
public DefaultHttpDataSource(String userAgent, Predicate<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(String userAgent, Predicate<String> contentTypePredicate, TransferListener 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(String userAgent, Predicate<String> contentTypePredicate, TransferListener 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(String userAgent, Predicate<String> contentTypePredicate, TransferListener listener, int connectTimeoutMillis, int readTimeoutMillis, boolean allowCrossProtocolRedirects)
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.public String getUri()
UriDataSource
If redirection occurred, the URI after redirection is the one returned.
getUri
in interface UriDataSource
public Map<String,List<String>> getResponseHeaders()
HttpDataSource
getResponseHeaders
in interface HttpDataSource
null
if response headers are unavailable.public void setRequestProperty(String name, String value)
HttpDataSource
setRequestProperty
in interface HttpDataSource
name
- The name of the header field.value
- The value of the field.public void clearRequestProperty(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
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
open
in interface HttpDataSource
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
.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
.
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
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 the end of the opened
range is reached.HttpDataSource.HttpDataSourceException
public void close() throws HttpDataSource.HttpDataSourceException
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
close
in interface HttpDataSource
HttpDataSource.HttpDataSourceException
protected final 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_UNBOUNDED
is returned.
C.LENGTH_UNBOUNDED
.