Class DataSpec
- java.lang.Object
-
- com.google.android.exoplayer2.upstream.DataSpec
-
public final class DataSpec extends Object
Defines a region of data in a resource.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
DataSpec.Builder
BuildsDataSpec
instances.static interface
DataSpec.Flags
The flags that apply to any request for data.static interface
DataSpec.HttpMethod
HTTP methods supported by ExoPlayerHttpDataSource
s.
-
Field Summary
Fields Modifier and Type Field Description long
absoluteStreamPosition
Deprecated.Useposition
except for specific use cases where the absolute position within the resource is required within aDataSource
chain.Object
customData
Application specific data.static int
FLAG_ALLOW_CACHE_FRAGMENTATION
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 int
FLAG_ALLOW_GZIP
Allows an underlying network stack to request that the server use gzip compression.static int
FLAG_DONT_CACHE_IF_LENGTH_UNKNOWN
Prevents caching if the length cannot be resolved when theDataSource
is opened.static int
FLAG_MIGHT_NOT_USE_FULL_NETWORK_SPEED
Indicates there are known external factors that might prevent the data from being loaded at full network speed (e.g.int
flags
Requestflags
.static int
HTTP_METHOD_GET
HTTP GET method.static int
HTTP_METHOD_HEAD
HTTP HEAD method.static int
HTTP_METHOD_POST
HTTP POST method.byte[]
httpBody
The HTTP request body, null otherwise.int
httpMethod
The HTTP method to use when requesting the data.Map<String,String>
httpRequestHeaders
Additional HTTP headers to use when requesting the data.String
key
A key that uniquely identifies the resource.long
length
The length of the data, orC.LENGTH_UNSET
.long
position
The position of the data when read fromuri
.Uri
uri
AUri
from which data belonging to the resource can be read.long
uriPositionOffset
The offset of the data located aturi
within the resource.
-
Constructor Summary
Constructors Constructor Description DataSpec(Uri uri)
Constructs an instance.DataSpec(Uri uri, byte[] postBody, long absoluteStreamPosition, long position, long length, String key, int flags)
Deprecated.UseDataSpec.Builder
.DataSpec(Uri uri, int flags)
Deprecated.UseDataSpec.Builder
.DataSpec(Uri uri, int httpMethod, byte[] httpBody, long absoluteStreamPosition, long position, long length, String key, int flags)
Deprecated.UseDataSpec.Builder
.DataSpec(Uri uri, int httpMethod, byte[] httpBody, long absoluteStreamPosition, long position, long length, String key, int flags, Map<String,String> httpRequestHeaders)
Deprecated.UseDataSpec.Builder
.DataSpec(Uri uri, long position, long length)
Constructs an instance.DataSpec(Uri uri, long absoluteStreamPosition, long position, long length, String key, int flags)
Deprecated.UseDataSpec.Builder
.DataSpec(Uri uri, long position, long length, String key)
Deprecated.UseDataSpec.Builder
.DataSpec(Uri uri, long position, long length, String key, int flags)
Deprecated.UseDataSpec.Builder
.DataSpec(Uri uri, long position, long length, String key, int flags, Map<String,String> httpRequestHeaders)
Deprecated.UseDataSpec.Builder
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description DataSpec.Builder
buildUpon()
Returns aDataSpec.Builder
initialized with the values of this instance.String
getHttpMethodString()
Returns the uppercase HTTP method name (e.g., "GET", "POST", "HEAD") corresponding to thehttpMethod
.static String
getStringForHttpMethod(int httpMethod)
Returns an uppercase HTTP method name (e.g., "GET", "POST", "HEAD") corresponding to the givenDataSpec.HttpMethod
.boolean
isFlagSet(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(Map<String,String> additionalHttpRequestHeaders)
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
withUri(Uri uri)
Returns a copy of this data spec with the specified Uri.
-
-
-
Field Detail
-
FLAG_ALLOW_GZIP
public static final int FLAG_ALLOW_GZIP
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 theDataSource
does make a network request, then the value returned fromDataSource.open(DataSpec)
will typically beC.LENGTH_UNSET
. The data read fromDataReader.read(byte[], int, int)
will be the decompressed data.- See Also:
- Constant Field Values
-
FLAG_DONT_CACHE_IF_LENGTH_UNKNOWN
public static final int FLAG_DONT_CACHE_IF_LENGTH_UNKNOWN
Prevents caching if the length cannot be resolved when theDataSource
is opened.- See Also:
- Constant Field Values
-
FLAG_ALLOW_CACHE_FRAGMENTATION
public static final int FLAG_ALLOW_CACHE_FRAGMENTATION
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).- See Also:
- Constant Field Values
-
FLAG_MIGHT_NOT_USE_FULL_NETWORK_SPEED
public static final int FLAG_MIGHT_NOT_USE_FULL_NETWORK_SPEED
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).- See Also:
- Constant Field Values
-
HTTP_METHOD_GET
public static final int HTTP_METHOD_GET
HTTP GET method.- See Also:
- Constant Field Values
-
HTTP_METHOD_POST
public static final int HTTP_METHOD_POST
HTTP POST method.- See Also:
- Constant Field Values
-
HTTP_METHOD_HEAD
public static final int HTTP_METHOD_HEAD
HTTP HEAD method.- See Also:
- Constant Field Values
-
uriPositionOffset
public final long uriPositionOffset
The offset of the data located aturi
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. Ifuri
points to a local file that contains just bytes [200-300], then this field will be set to200
.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 aDataSource
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.
-
httpMethod
@HttpMethod public final int httpMethod
The HTTP method to use when requesting the data. This value will be ignored by non-HTTPDataSource
implementations.
-
httpBody
@Nullable public final byte[] httpBody
The HTTP request body, null otherwise. If the body is non-null, thenhttpBody.length
will be non-zero.
-
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
:HttpDataSource
implementations derive theRange
header fromposition
andlength
.Accept-Encoding
:HttpDataSource
implementations derive theAccept-Encoding
header based on whetherflags
includesFLAG_ALLOW_GZIP
.User-Agent
:HttpDataSource
implementations set theUser-Agent
header directly.- Other headers set at the
HttpDataSource
layer. I.e., headers set usingHttpDataSource.setRequestProperty(String, String)
, and usingHttpDataSource.Factory.setDefaultRequestProperties(Map)
.
-
absoluteStreamPosition
@Deprecated public final long absoluteStreamPosition
Deprecated.Useposition
except for specific use cases where the absolute position within the resource is required within aDataSource
chain. Where the absolute position is required, useuriPositionOffset + position
.The absolute position of the data in the resource.
-
position
public final long position
The position of the data when read fromuri
.
-
length
public final long length
The length of the data, orC.LENGTH_UNSET
.
-
key
@Nullable public final 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.
-
-
Constructor Detail
-
DataSpec
public DataSpec(Uri uri, long position, long length)
Constructs an instance.
-
DataSpec
@Deprecated public DataSpec(Uri uri, @Flags int flags)
Deprecated.UseDataSpec.Builder
.Constructs an instance.
-
DataSpec
@Deprecated public DataSpec(Uri uri, long position, long length, @Nullable String key)
Deprecated.UseDataSpec.Builder
.Constructs an instance.
-
DataSpec
@Deprecated public DataSpec(Uri uri, long position, long length, @Nullable String key, @Flags int flags)
Deprecated.UseDataSpec.Builder
.Constructs an instance.
-
DataSpec
@Deprecated public DataSpec(Uri uri, long position, long length, @Nullable String key, @Flags int flags, Map<String,String> httpRequestHeaders)
Deprecated.UseDataSpec.Builder
.Constructs an instance.
-
DataSpec
@Deprecated public DataSpec(Uri uri, long absoluteStreamPosition, long position, long length, @Nullable String key, @Flags int flags)
Deprecated.UseDataSpec.Builder
.Constructs an instance whereuriPositionOffset
may be non-zero.
-
DataSpec
@Deprecated public DataSpec(Uri uri, @Nullable byte[] postBody, long absoluteStreamPosition, long position, long length, @Nullable String key, @Flags int flags)
Deprecated.UseDataSpec.Builder
. Note that the httpMethod must be set explicitly for the Builder.Construct a instance whereuriPositionOffset
may be non-zero. ThehttpMethod
is inferred frompostBody
. IfpostBody
is non-null thenhttpMethod
is set toHTTP_METHOD_POST
. IfpostBody
is null thenhttpMethod
is set toHTTP_METHOD_GET
.- Parameters:
uri
-uri
.postBody
-httpBody
The body of the HTTP request, which is also used to infer thehttpMethod
.absoluteStreamPosition
- The sum ofuriPositionOffset
andposition
.position
-position
.length
-length
.key
-key
.flags
-flags
.
-
DataSpec
@Deprecated public DataSpec(Uri uri, @HttpMethod int httpMethod, @Nullable byte[] httpBody, long absoluteStreamPosition, long position, long length, @Nullable String key, @Flags int flags)
Deprecated.UseDataSpec.Builder
.Construct a instance whereuriPositionOffset
may be non-zero.- Parameters:
uri
-uri
.httpMethod
-httpMethod
.httpBody
-httpBody
.absoluteStreamPosition
- The sum ofuriPositionOffset
andposition
.position
-position
.length
-length
.key
-key
.flags
-flags
.
-
DataSpec
@Deprecated public DataSpec(Uri uri, @HttpMethod int httpMethod, @Nullable byte[] httpBody, long absoluteStreamPosition, long position, long length, @Nullable String key, @Flags int flags, Map<String,String> httpRequestHeaders)
Deprecated.UseDataSpec.Builder
.Construct a instance whereuriPositionOffset
may be non-zero.- Parameters:
uri
-uri
.httpMethod
-httpMethod
.httpBody
-httpBody
.absoluteStreamPosition
- The sum ofuriPositionOffset
andposition
.position
-position
.length
-length
.key
-key
.flags
-flags
.httpRequestHeaders
-httpRequestHeaders
.
-
-
Method Detail
-
getStringForHttpMethod
public static String getStringForHttpMethod(@HttpMethod int httpMethod)
Returns an uppercase HTTP method name (e.g., "GET", "POST", "HEAD") corresponding to the givenDataSpec.HttpMethod
.
-
isFlagSet
public boolean isFlagSet(@Flags int flag)
Returns whether the given flag is set.- Parameters:
flag
- Flag to be checked if it is set.
-
getHttpMethodString
public final String getHttpMethodString()
Returns the uppercase HTTP method name (e.g., "GET", "POST", "HEAD") corresponding to thehttpMethod
.
-
buildUpon
public DataSpec.Builder buildUpon()
Returns aDataSpec.Builder
initialized with the values of this instance.
-
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:
offset
- The offset of the subrange.- Returns:
- 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:
offset
- The offset of the subrange.length
- The length of the subrange.- Returns:
- A data spec that represents a subrange of the data defined by this DataSpec.
-
withUri
public DataSpec withUri(Uri uri)
Returns a copy of this data spec with the specified Uri.- Parameters:
uri
- The new sourceUri
.- Returns:
- The copied data spec with the specified Uri.
-
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.- Parameters:
httpRequestHeaders
- The HTTP request headers.- Returns:
- The copied data spec with the specified HTTP request headers.
-
withAdditionalHeaders
public DataSpec withAdditionalHeaders(Map<String,String> additionalHttpRequestHeaders)
Returns a copy this data spec with additional HTTP request headers. Headers inadditionalHttpRequestHeaders
will overwrite any headers already in the data spec that have the same keys.- Parameters:
additionalHttpRequestHeaders
- The additional HTTP request headers.- Returns:
- The copied data spec with the additional HTTP request headers.
-
-