public class DefaultHttpDataSource
extends java.lang.Object
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.
| Modifier and Type | Field and Description |
|---|---|
protected java.net.HttpURLConnection |
connection |
protected 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,
<any> contentTypePredicate) |
DefaultHttpDataSource(java.lang.String userAgent,
<any> contentTypePredicate,
TransferListener listener) |
DefaultHttpDataSource(java.lang.String userAgent,
<any> contentTypePredicate,
TransferListener listener,
int connectTimeoutMillis,
int readTimeoutMillis) |
DefaultHttpDataSource(java.lang.String userAgent,
<any> 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() |
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(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(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 DataSpec dataSpec
protected java.net.HttpURLConnection connection
public DefaultHttpDataSource(java.lang.String userAgent,
<any> 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,
<any> 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(java.lang.String userAgent,
<any> 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(java.lang.String userAgent,
<any> 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 java.lang.String getUri()
public java.util.Map<java.lang.String,java.util.List<java.lang.String>> getResponseHeaders()
public void setRequestProperty(java.lang.String name,
java.lang.String value)
public void clearRequestProperty(java.lang.String name)
public void clearAllRequestProperties()
protected int interceptResponseCode(int responseCode)
throws BoxException,
HttpDataSourceException
BoxExceptionHttpDataSourceExceptionpublic long open(DataSpec dataSpec)
throws HttpDataSourceException
HttpDataSourceExceptionpublic int read(byte[] buffer,
int offset,
int readLength)
throws HttpDataSourceException
HttpDataSourceExceptionpublic void close()
throws HttpDataSourceException
HttpDataSourceExceptionprotected final java.net.HttpURLConnection getConnection()
protected final long bytesSkipped()
protected final long bytesRead()
protected final long bytesRemaining()
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.
protected java.net.HttpURLConnection makeConnection(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.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.java.io.IOException - If an error occurs reading from the source.protected void closeConnectionQuietly()