public class DefaultHttpDataSource
extends java.lang.Object
implements com.google.android.exoplayer.upstream.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.
com.google.android.exoplayer.upstream.HttpDataSource.HttpDataSourceException, com.google.android.exoplayer.upstream.HttpDataSource.InvalidContentTypeException, com.google.android.exoplayer.upstream.HttpDataSource.InvalidResponseCodeException| Modifier and Type | Field and Description |
|---|---|
protected java.net.HttpURLConnection |
connection |
protected com.google.android.exoplayer.upstream.DataSpec |
dataSpec |
static int |
DEFAULT_CONNECT_TIMEOUT_MILLIS
The default connection timeout, in milliseconds.
|
static int |
DEFAULT_READ_TIMEOUT_MILLIS
The default read timeout, in milliseconds.
|
| Constructor and Description |
|---|
DefaultHttpDataSource(java.lang.String userAgent,
com.google.android.exoplayer.util.Predicate<java.lang.String> contentTypePredicate) |
DefaultHttpDataSource(java.lang.String userAgent,
com.google.android.exoplayer.util.Predicate<java.lang.String> contentTypePredicate,
com.google.android.exoplayer.upstream.TransferListener listener) |
DefaultHttpDataSource(java.lang.String userAgent,
com.google.android.exoplayer.util.Predicate<java.lang.String> contentTypePredicate,
com.google.android.exoplayer.upstream.TransferListener listener,
int connectTimeoutMillis,
int readTimeoutMillis) |
DefaultHttpDataSource(java.lang.String userAgent,
com.google.android.exoplayer.util.Predicate<java.lang.String> contentTypePredicate,
com.google.android.exoplayer.upstream.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() |
void |
clearRequestProperty(java.lang.String name) |
void |
close() |
protected void |
closeConnectionQuietly()
Closes the current connection quietly, if there is one.
|
protected java.net.HttpURLConnection |
getConnection()
Returns the current connection, or null if the source is not currently opened.
|
protected static long |
getContentLength(java.net.HttpURLConnection connection)
Attempts to extract the length of the content from the response headers of an open connection.
|
java.util.Map<java.lang.String,java.util.List<java.lang.String>> |
getResponseHeaders() |
java.lang.String |
getUri() |
protected static java.net.URL |
handleRedirect(java.net.URL originalUrl,
java.lang.String location)
Handles a redirect.
|
protected int |
interceptResponseCode(int responseCode) |
protected java.net.HttpURLConnection |
makeConnection(com.google.android.exoplayer.upstream.DataSpec dataSpec)
Establishes a connection, following redirects to do so where permitted.
|
protected java.net.HttpURLConnection |
makeConnection(java.net.URL url,
byte[] postBody,
long position,
long length,
boolean allowGzip,
boolean followRedirects)
Configures a connection and opens it.
|
long |
open(com.google.android.exoplayer.upstream.DataSpec dataSpec) |
int |
read(byte[] buffer,
int offset,
int readLength) |
protected int |
readInternal(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) |
protected void |
skipInternal()
Skips any bytes that need skipping.
|
public static final int DEFAULT_CONNECT_TIMEOUT_MILLIS
public static final int DEFAULT_READ_TIMEOUT_MILLIS
protected com.google.android.exoplayer.upstream.DataSpec dataSpec
protected java.net.HttpURLConnection connection
public DefaultHttpDataSource(java.lang.String userAgent,
com.google.android.exoplayer.util.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,
com.google.android.exoplayer.util.Predicate<java.lang.String> contentTypePredicate,
com.google.android.exoplayer.upstream.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(java.lang.String userAgent,
com.google.android.exoplayer.util.Predicate<java.lang.String> contentTypePredicate,
com.google.android.exoplayer.upstream.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(java.lang.String userAgent,
com.google.android.exoplayer.util.Predicate<java.lang.String> contentTypePredicate,
com.google.android.exoplayer.upstream.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 java.lang.String getUri()
getUri in interface com.google.android.exoplayer.upstream.UriDataSourcepublic java.util.Map<java.lang.String,java.util.List<java.lang.String>> getResponseHeaders()
getResponseHeaders in interface com.google.android.exoplayer.upstream.HttpDataSourcepublic void setRequestProperty(java.lang.String name,
java.lang.String value)
setRequestProperty in interface com.google.android.exoplayer.upstream.HttpDataSourcepublic void clearRequestProperty(java.lang.String name)
clearRequestProperty in interface com.google.android.exoplayer.upstream.HttpDataSourcepublic void clearAllRequestProperties()
clearAllRequestProperties in interface com.google.android.exoplayer.upstream.HttpDataSourceprotected int interceptResponseCode(int responseCode)
throws com.box.androidsdk.content.BoxException,
com.google.android.exoplayer.upstream.HttpDataSource.HttpDataSourceException
com.box.androidsdk.content.BoxExceptioncom.google.android.exoplayer.upstream.HttpDataSource.HttpDataSourceExceptionpublic long open(com.google.android.exoplayer.upstream.DataSpec dataSpec)
throws com.google.android.exoplayer.upstream.HttpDataSource.HttpDataSourceException
open in interface com.google.android.exoplayer.upstream.DataSourceopen in interface com.google.android.exoplayer.upstream.HttpDataSourcecom.google.android.exoplayer.upstream.HttpDataSource.HttpDataSourceExceptionpublic int read(byte[] buffer,
int offset,
int readLength)
throws com.google.android.exoplayer.upstream.HttpDataSource.HttpDataSourceException
read in interface com.google.android.exoplayer.upstream.DataSourceread in interface com.google.android.exoplayer.upstream.HttpDataSourcecom.google.android.exoplayer.upstream.HttpDataSource.HttpDataSourceExceptionpublic void close()
throws com.google.android.exoplayer.upstream.HttpDataSource.HttpDataSourceException
close in interface com.google.android.exoplayer.upstream.DataSourceclose in interface com.google.android.exoplayer.upstream.HttpDataSourcecom.google.android.exoplayer.upstream.HttpDataSource.HttpDataSourceExceptionprotected 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_UNBOUNDED is returned.
C.LENGTH_UNBOUNDED.protected java.net.HttpURLConnection makeConnection(com.google.android.exoplayer.upstream.DataSpec dataSpec)
throws java.io.IOException
dataSpec - The data spec associated with this connection.java.io.IOException - throws IOException if there is an error creating connection.protected java.net.HttpURLConnection makeConnection(java.net.URL url,
byte[] postBody,
long position,
long length,
boolean allowGzip,
boolean followRedirects)
throws java.io.IOException
url - The url to connect to.postBody - The body data for a POST request.position - The byte offset of the requested data.length - The length of the requested data, or C.LENGTH_UNBOUNDED.allowGzip - Whether to allow the use of gzip.followRedirects - Whether to follow redirects.java.io.IOException - exception thrown if there is a problem writing to connection.protected static java.net.URL handleRedirect(java.net.URL originalUrl,
java.lang.String location)
throws java.io.IOException
originalUrl - The original URL.location - The Location header in the response.java.io.IOException - If redirection isn't possible.protected static long getContentLength(java.net.HttpURLConnection connection)
connection - The open connection.C.LENGTH_UNBOUNDED.protected void skipInternal()
throws java.io.IOException
This implementation is based roughly on libcore.io.Streams.skipByReading().
java.io.InterruptedIOException - If the thread is interrupted during the operation.java.io.EOFException - If the end of the input stream is reached before the bytes are skipped.java.io.IOExceptionprotected int readInternal(byte[] buffer,
int offset,
int readLength)
throws java.io.IOException
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.
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.protected void closeConnectionQuietly()