DataSpec
@UnstableApi
public final class DataSpec
Defines a region of data in a resource.
Summary
Nested types |
|---|
public final class DataSpec.BuilderBuilds |
@DocumentedThe flags that apply to any request for data. |
@DocumentedHTTP methods supported by ExoPlayer |
Constants |
|
|---|---|
static final int |
Allows fragmentation of this request into multiple cache files, meaning a cache eviction policy will be able to evict individual fragments of the data. |
static final int |
FLAG_ALLOW_GZIP = 1Allows an underlying network stack to request that the server use gzip compression. |
static final int |
Prevents caching if the length cannot be resolved when the |
static final int |
Indicates there are known external factors that might prevent the data from being loaded at full network speed (e.g. server throttling or unfinished live media chunks). |
static final int |
HTTP_METHOD_GET = 1HTTP GET method. |
static final int |
HTTP_METHOD_HEAD = 3HTTP HEAD method. |
static final int |
HTTP_METHOD_POST = 2HTTP POST method. |
Public fields |
|
|---|---|
final long |
This field is deprecated. Use |
final @Nullable Object |
Application specific data. |
final int |
Request |
final @Nullable byte[] |
The HTTP request body, null otherwise. |
final int |
The HTTP method to use when requesting the data. |
final Map<String, String> |
Additional HTTP headers to use when requesting the data. |
final @Nullable String |
A key that uniquely identifies the resource. |
final long |
The length of the data, or |
final long |
The position of the data when read from |
final Uri |
A |
final long |
The offset of the data located at |
Public constructors |
|---|
|
Constructs an instance. |
|
Constructs an instance. |
|
This method is deprecated. Use |
Public methods |
|
|---|---|
DataSpec.Builder |
Returns a |
final String |
Returns the uppercase HTTP method name (e.g., "GET", "POST", "HEAD") corresponding to the |
static String |
getStringForHttpMethod(@DataSpec.HttpMethod int httpMethod)Returns an uppercase HTTP method name (e.g., "GET", "POST", "HEAD") corresponding to the given |
boolean |
isFlagSet(@DataSpec.Flags int flag)Returns whether the given flag is set. |
DataSpec |
subrange(long offset)Returns a data spec that represents a subrange of the data defined by this DataSpec. |
DataSpec |
subrange(long offset, long length)Returns a data spec that represents a subrange of the data defined by this DataSpec. |
String |
toString() |
DataSpec |
withAdditionalHeaders(Returns a copy this data spec with additional HTTP request headers. |
DataSpec |
withRequestHeaders(Map<String, String> httpRequestHeaders)Returns a copy of this data spec with the specified HTTP request headers. |
DataSpec |
Returns a copy of this data spec with the specified Uri. |
Constants
FLAG_ALLOW_CACHE_FRAGMENTATION
public static final int FLAG_ALLOW_CACHE_FRAGMENTATION = 4
Allows fragmentation of this request into multiple cache files, meaning a cache eviction policy will be able to evict individual fragments of the data. Depending on the cache implementation, setting this flag may also enable more concurrent access to the data (e.g. reading one fragment whilst writing another).
FLAG_ALLOW_GZIP
public static final int FLAG_ALLOW_GZIP = 1
Allows an underlying network stack to request that the server use gzip compression.
Should not typically be set if the data being requested is already compressed (e.g. most audio and video requests). May be set when requesting other data.
When a DataSource is used to request data with this flag set, and if the DataSource does make a network request, then the value returned from open will typically be LENGTH_UNSET. The data read from read will be the decompressed data.
FLAG_DONT_CACHE_IF_LENGTH_UNKNOWN
public static final int FLAG_DONT_CACHE_IF_LENGTH_UNKNOWN = 2
Prevents caching if the length cannot be resolved when the DataSource is opened.
FLAG_MIGHT_NOT_USE_FULL_NETWORK_SPEED
public static final int FLAG_MIGHT_NOT_USE_FULL_NETWORK_SPEED = 8
Indicates there are known external factors that might prevent the data from being loaded at full network speed (e.g. server throttling or unfinished live media chunks).
Public fields
absoluteStreamPosition
public final long absoluteStreamPosition
The absolute position of the data in the resource.
customData
public final @Nullable Object customData
Application specific data.
This field is intended for advanced use cases in which applications require the ability to attach custom data to DataSpec instances. The custom data should be immutable.
httpBody
public final @Nullable byte[] httpBody
The HTTP request body, null otherwise. If the body is non-null, then httpBody.length will be non-zero.
httpMethod
@DataSpec.HttpMethod
public final int httpMethod
The HTTP method to use when requesting the data. This value will be ignored by non-HTTP DataSource implementations.
httpRequestHeaders
public final Map<String, String> httpRequestHeaders
Additional HTTP headers to use when requesting the data.
Note: This map is for additional headers specific to the data being requested. It does not include headers that are set directly by HttpDataSource implementations. In particular, this means the following headers are not included:
Range:HttpDataSourceimplementations derive theRangeheader frompositionandlength.Accept-Encoding:HttpDataSourceimplementations derive theAccept-Encodingheader based on whetherflagsincludesFLAG_ALLOW_GZIP.User-Agent:HttpDataSourceimplementations set theUser-Agentheader directly.- Other headers set at the
HttpDataSourcelayer. I.e., headers set usingsetRequestProperty, and usingsetDefaultRequestProperties.
key
public final @Nullable String key
A key that uniquely identifies the resource. Used for cache indexing. May be null if the data spec is not intended to be used in conjunction with a cache.
uriPositionOffset
public final long uriPositionOffset
The offset of the data located at uri within the resource.
Equal to 0 unless uri provides access to a subset of the resource. As an example, consider a resource that can be requested over the network and is 1000 bytes long. If uri points to a local file that contains just bytes [200-300], then this field will be set to 200.
This field can be ignored except for in specific circumstances where the absolute position in the resource is required in a DataSource chain. One example is when a DataSource needs to decrypt the content as it's read. In this case the absolute position in the resource is typically needed to correctly initialize the decryption algorithm.
Public constructors
Public methods
buildUpon
public DataSpec.Builder buildUpon()
Returns a DataSpec.Builder initialized with the values of this instance.
getHttpMethodString
public final String getHttpMethodString()
Returns the uppercase HTTP method name (e.g., "GET", "POST", "HEAD") corresponding to the httpMethod.
getStringForHttpMethod
public static String getStringForHttpMethod(@DataSpec.HttpMethod int httpMethod)
Returns an uppercase HTTP method name (e.g., "GET", "POST", "HEAD") corresponding to the given HttpMethod.
isFlagSet
public boolean isFlagSet(@DataSpec.Flags int flag)
Returns whether the given flag is set.
| Parameters | |
|---|---|
@DataSpec.Flags int flag |
Flag to be checked if it is set. |
subrange
public DataSpec subrange(long offset)
Returns a data spec that represents a subrange of the data defined by this DataSpec. The subrange includes data from the offset up to the end of this DataSpec.
| Parameters | |
|---|---|
long offset |
The offset of the subrange. |
| Returns | |
|---|---|
DataSpec |
A data spec that represents a subrange of the data defined by this DataSpec. |
subrange
public DataSpec subrange(long offset, long length)
Returns a data spec that represents a subrange of the data defined by this DataSpec.
| Parameters | |
|---|---|
long offset |
The offset of the subrange. |
long length |
The length of the subrange. |
| Returns | |
|---|---|
DataSpec |
A data spec that represents a subrange of the data defined by this DataSpec. |
withAdditionalHeaders
public DataSpec withAdditionalHeaders(
Map<String, String> additionalHttpRequestHeaders
)
Returns a copy this data spec with additional HTTP request headers. Headers in
additionalHttpRequestHeaders will overwrite any headers already in the data spec that have the same keys.
| Returns | |
|---|---|
DataSpec |
The copied data spec with the additional HTTP request headers. |
withRequestHeaders
public DataSpec withRequestHeaders(Map<String, String> httpRequestHeaders)
Returns a copy of this data spec with the specified HTTP request headers. Headers already in the data spec are not copied to the new instance.
| Returns | |
|---|---|
DataSpec |
The copied data spec with the specified HTTP request headers. |