public class OkHttpDataSource extends Object implements HttpDataSource
HttpDataSource that delegates to Square's Call.Factory.HttpDataSource.BaseFactory, HttpDataSource.Factory, HttpDataSource.HttpDataSourceException, HttpDataSource.InvalidContentTypeException, HttpDataSource.InvalidResponseCodeException, HttpDataSource.RequestPropertiesREJECT_PAYWALL_TYPES| Constructor and Description |
|---|
OkHttpDataSource(okhttp3.Call.Factory callFactory,
String userAgent,
Predicate<String> contentTypePredicate) |
OkHttpDataSource(okhttp3.Call.Factory callFactory,
String userAgent,
Predicate<String> contentTypePredicate,
TransferListener<? super OkHttpDataSource> listener) |
OkHttpDataSource(okhttp3.Call.Factory callFactory,
String userAgent,
Predicate<String> contentTypePredicate,
TransferListener<? super OkHttpDataSource> listener,
okhttp3.CacheControl cacheControl,
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(String name)
Clears the value of a request header.
|
void |
close()
Closes the source.
|
Map<String,List<String>> |
getResponseHeaders()
Returns the headers provided in the response, or
null if response headers are
unavailable. |
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(String name,
String value)
Sets the value of a request header.
|
public OkHttpDataSource(@NonNull okhttp3.Call.Factory callFactory, @Nullable String userAgent, @Nullable Predicate<String> contentTypePredicate)
callFactory - A Call.Factory (typically an OkHttpClient) for use
by the source.userAgent - An optional User-Agent string.contentTypePredicate - An optional Predicate. If a content type is rejected by the
predicate then a InvalidContentTypeException} is thrown from open(DataSpec).public OkHttpDataSource(@NonNull okhttp3.Call.Factory callFactory, @Nullable String userAgent, @Nullable Predicate<String> contentTypePredicate, @Nullable TransferListener<? super OkHttpDataSource> listener)
callFactory - A Call.Factory (typically an OkHttpClient) for use
by the source.userAgent - An optional User-Agent string.contentTypePredicate - An optional Predicate. If a content type is rejected by the
predicate then a InvalidContentTypeException is thrown from
open(DataSpec).listener - An optional listener.public OkHttpDataSource(@NonNull okhttp3.Call.Factory callFactory, @Nullable String userAgent, @Nullable Predicate<String> contentTypePredicate, @Nullable TransferListener<? super OkHttpDataSource> listener, @Nullable okhttp3.CacheControl cacheControl, @Nullable HttpDataSource.RequestProperties defaultRequestProperties)
callFactory - A Call.Factory (typically an OkHttpClient) for use
by the source.userAgent - An optional User-Agent string.contentTypePredicate - An optional Predicate. If a content type is rejected by the
predicate then a InvalidContentTypeException is thrown from
open(DataSpec).listener - An optional listener.cacheControl - An optional CacheControl for setting the Cache-Control header.defaultRequestProperties - The optional default RequestProperties to be sent to
the server as HTTP headers on every request.public Uri getUri()
DataSourceUri 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 DataSourceUri from which data is being read, or null if the source is not open.public Map<String,List<String>> getResponseHeaders()
HttpDataSourcenull if response headers are
unavailable.getResponseHeaders in interface HttpDataSourcepublic void setRequestProperty(String name, String value)
HttpDataSourcesetRequestProperty in interface HttpDataSourcename - The name of the header field.value - The value of the field.public void clearRequestProperty(String name)
HttpDataSourceclearRequestProperty in interface HttpDataSourcename - The name of the header field.public void clearAllRequestProperties()
HttpDataSourceHttpDataSource.setRequestProperty(String, String).clearAllRequestProperties in interface HttpDataSourcepublic 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 DataSourceopen in interface HttpDataSourcedataSpec - 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.HttpDataSourceExceptionpublic int read(byte[] buffer,
int offset,
int readLength)
throws HttpDataSource.HttpDataSourceException
DataSourcelength 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 DataSourceread in interface HttpDataSourcebuffer - 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.HttpDataSourceExceptionpublic 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 DataSourceclose in interface HttpDataSourceHttpDataSource.HttpDataSourceExceptionprotected 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.