# DataSpec

Artifact: [androidx.media3:media3-datasource](https://developer.android.com/jetpack/androidx/releases/media3) [View Source](https://cs.android.com/search?q=file:androidx/media3/datasource/DataSpec.java+class:androidx.media3.datasource.DataSpec)

*** ** * ** ***


```
@UnstableApi
public final class DataSpec
```

<br />

*** ** * ** ***

Defines a region of data in a resource.

## Summary

| ### Nested types |
|---|
| `public final class https://developer.android.com/reference/androidx/media3/datasource/DataSpec.Builder` Builds `https://developer.android.com/reference/androidx/media3/datasource/DataSpec` instances. |
| `@https://developer.android.com/reference/java/lang/annotation/Documented.html @https://developer.android.com/reference/java/lang/annotation/Retention.html(value = https://developer.android.com/reference/java/lang/annotation/RetentionPolicy.html#SOURCE) @https://developer.android.com/reference/java/lang/annotation/Target.html(value = https://developer.android.com/reference/java/lang/annotation/ElementType.html#TYPE_USE) @https://developer.android.com/reference/androidx/annotation/IntDef(flag = true, value = ) public annotation https://developer.android.com/reference/androidx/media3/datasource/DataSpec.Flags` The flags that apply to any request for data. |
| `@https://developer.android.com/reference/java/lang/annotation/Documented.html @https://developer.android.com/reference/java/lang/annotation/Retention.html(value = https://developer.android.com/reference/java/lang/annotation/RetentionPolicy.html#SOURCE) @https://developer.android.com/reference/java/lang/annotation/Target.html(value = https://developer.android.com/reference/java/lang/annotation/ElementType.html#TYPE_USE) @https://developer.android.com/reference/androidx/annotation/IntDef(value = ) public annotation https://developer.android.com/reference/androidx/media3/datasource/DataSpec.HttpMethod` HTTP methods supported by ExoPlayer `https://developer.android.com/reference/androidx/media3/datasource/HttpDataSource`s. |

| ### Constants |
|---|---|
| `static final int` | `https://developer.android.com/reference/androidx/media3/datasource/DataSpec#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. |
| `static final int` | `https://developer.android.com/reference/androidx/media3/datasource/DataSpec#FLAG_ALLOW_GZIP() = 1` Allows an underlying network stack to request that the server use gzip compression. |
| `static final int` | `https://developer.android.com/reference/androidx/media3/datasource/DataSpec#FLAG_DONT_CACHE_IF_LENGTH_UNKNOWN() = 2` Prevents caching if the length cannot be resolved when the `https://developer.android.com/reference/androidx/media3/datasource/DataSource` is opened. |
| `static final int` | `https://developer.android.com/reference/androidx/media3/datasource/DataSpec#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). |
| `static final int` | `https://developer.android.com/reference/androidx/media3/datasource/DataSpec#HTTP_METHOD_GET() = 1` HTTP GET method. |
| `static final int` | `https://developer.android.com/reference/androidx/media3/datasource/DataSpec#HTTP_METHOD_HEAD() = 3` HTTP HEAD method. |
| `static final int` | `https://developer.android.com/reference/androidx/media3/datasource/DataSpec#HTTP_METHOD_POST() = 2` HTTP POST method. |

| ### Public fields |
|---|---|
| `final long` | `https://developer.android.com/reference/androidx/media3/datasource/DataSpec#absoluteStreamPosition()` **This field is deprecated.** Use `https://developer.android.com/reference/androidx/media3/datasource/DataSpec#position()` except for specific use cases where the absolute position within the resource is required within a `https://developer.android.com/reference/androidx/media3/datasource/DataSource` chain. <br /> |
| `final @https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/java/lang/Object.html` | `https://developer.android.com/reference/androidx/media3/datasource/DataSpec#customData()` Application specific data. |
| `final int` | `@https://developer.android.com/reference/androidx/media3/datasource/DataSpec.Flags https://developer.android.com/reference/androidx/media3/datasource/DataSpec#flags()` Request `https://developer.android.com/reference/androidx/media3/datasource/DataSpec.Flags`. |
| `final @https://developer.android.com/reference/androidx/annotation/Nullable byte[]` | `https://developer.android.com/reference/androidx/media3/datasource/DataSpec#httpBody()` The HTTP request body, null otherwise. |
| `final int` | `@https://developer.android.com/reference/androidx/media3/datasource/DataSpec.HttpMethod https://developer.android.com/reference/androidx/media3/datasource/DataSpec#httpMethod()` The HTTP method to use when requesting the data. |
| `final https://developer.android.com/reference/java/util/Map.html<https://developer.android.com/reference/java/lang/String.html, https://developer.android.com/reference/java/lang/String.html>` | `https://developer.android.com/reference/androidx/media3/datasource/DataSpec#httpRequestHeaders()` Additional HTTP headers to use when requesting the data. |
| `final @https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/media3/datasource/DataSpec#key()` A key that uniquely identifies the resource. |
| `final long` | `https://developer.android.com/reference/androidx/media3/datasource/DataSpec#length()` The length of the data, or `https://developer.android.com/reference/androidx/media3/common/C#LENGTH_UNSET()`. |
| `final @https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/media3/datasource/DataSpec#location()` A logical identifier of the location (e.g., a service location, pathway, CDN identifier etc.) from which the resource is loaded, or `null` if the location is not known or not applicable. |
| `final long` | `https://developer.android.com/reference/androidx/media3/datasource/DataSpec#position()` The position of the data when read from `https://developer.android.com/reference/androidx/media3/datasource/DataSpec#uri()`. |
| `final https://developer.android.com/reference/android/net/Uri.html` | `https://developer.android.com/reference/androidx/media3/datasource/DataSpec#uri()` A `https://developer.android.com/reference/android/net/Uri.html` from which data belonging to the resource can be read. |
| `final long` | `https://developer.android.com/reference/androidx/media3/datasource/DataSpec#uriPositionOffset()` The offset of the data located at `https://developer.android.com/reference/androidx/media3/datasource/DataSpec#uri()` within the resource. |

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/media3/datasource/DataSpec#DataSpec(android.net.Uri)(https://developer.android.com/reference/android/net/Uri.html uri)` Constructs an instance. |
| `https://developer.android.com/reference/androidx/media3/datasource/DataSpec#DataSpec(android.net.Uri,long,long)(https://developer.android.com/reference/android/net/Uri.html uri, long position, long length)` Constructs an instance. |
| `[DataSpec](https://developer.android.com/reference/androidx/media3/datasource/DataSpec#DataSpec(android.net.Uri,long,long,java.lang.String))(https://developer.android.com/reference/android/net/Uri.html uri, long position, long length, @https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/java/lang/String.html key)` **This method is deprecated.** Use `https://developer.android.com/reference/androidx/media3/datasource/DataSpec.Builder`. <br /> |

| ### Public methods |
|---|---|
| `https://developer.android.com/reference/androidx/media3/datasource/DataSpec.Builder` | `https://developer.android.com/reference/androidx/media3/datasource/DataSpec#buildUpon()()` Returns a `https://developer.android.com/reference/androidx/media3/datasource/DataSpec.Builder` initialized with the values of this instance. |
| `https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/media3/datasource/DataSpec#getHttpMethodString()()` Returns the uppercase HTTP method name (e.g., "GET", "POST", "HEAD") corresponding to the `https://developer.android.com/reference/androidx/media3/datasource/DataSpec#httpMethod()`. |
| `static https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/media3/datasource/DataSpec#getStringForHttpMethod(int)(@https://developer.android.com/reference/androidx/media3/datasource/DataSpec.HttpMethod int httpMethod)` Returns an uppercase HTTP method name (e.g., "GET", "POST", "HEAD") corresponding to the given `https://developer.android.com/reference/androidx/media3/datasource/DataSpec.HttpMethod`. |
| `boolean` | `https://developer.android.com/reference/androidx/media3/datasource/DataSpec#isFlagSet(int)(@https://developer.android.com/reference/androidx/media3/datasource/DataSpec.Flags int flag)` Returns whether the given flag is set. |
| `https://developer.android.com/reference/androidx/media3/datasource/DataSpec` | `https://developer.android.com/reference/androidx/media3/datasource/DataSpec#subrange(long)(long offset)` Returns a data spec that represents a subrange of the data defined by this DataSpec. |
| `https://developer.android.com/reference/androidx/media3/datasource/DataSpec` | `https://developer.android.com/reference/androidx/media3/datasource/DataSpec#subrange(long,long)(long offset, long length)` Returns a data spec that represents a subrange of the data defined by this DataSpec. |
| `https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/media3/datasource/DataSpec#toString()()` |
| `https://developer.android.com/reference/androidx/media3/datasource/DataSpec` | `https://developer.android.com/reference/androidx/media3/datasource/DataSpec#withAdditionalHeaders(java.util.Map<java.lang.String,java.lang.String>)( https://developer.android.com/reference/java/util/Map.html<https://developer.android.com/reference/java/lang/String.html, https://developer.android.com/reference/java/lang/String.html> additionalHttpRequestHeaders )` Returns a copy this data spec with additional HTTP request headers. |
| `https://developer.android.com/reference/androidx/media3/datasource/DataSpec` | `https://developer.android.com/reference/androidx/media3/datasource/DataSpec#withRequestHeaders(java.util.Map<java.lang.String,java.lang.String>)(https://developer.android.com/reference/java/util/Map.html<https://developer.android.com/reference/java/lang/String.html, https://developer.android.com/reference/java/lang/String.html> httpRequestHeaders)` Returns a copy of this data spec with the specified HTTP request headers. |
| `https://developer.android.com/reference/androidx/media3/datasource/DataSpec` | `https://developer.android.com/reference/androidx/media3/datasource/DataSpec#withUri(android.net.Uri)(https://developer.android.com/reference/android/net/Uri.html uri)` 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 `https://developer.android.com/reference/androidx/media3/datasource/DataSource` is used to request data with this flag set, and if the `https://developer.android.com/reference/androidx/media3/datasource/DataSource` does make a network request, then the value returned from `https://developer.android.com/reference/androidx/media3/datasource/DataSource#open(androidx.media3.datasource.DataSpec)` will typically be `https://developer.android.com/reference/androidx/media3/common/C#LENGTH_UNSET()`. The data read from `https://developer.android.com/reference/androidx/media3/common/DataReader#read(byte[],int,int)` 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 `https://developer.android.com/reference/androidx/media3/datasource/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).

### HTTP_METHOD_GET

```
public static final int HTTP_METHOD_GET = 1
```

HTTP GET method.

### HTTP_METHOD_HEAD

```
public static final int HTTP_METHOD_HEAD = 3
```

HTTP HEAD method.

### HTTP_METHOD_POST

```
public static final int HTTP_METHOD_POST = 2
```

HTTP POST method.

## Public fields

### absoluteStreamPosition

```
public final long absoluteStreamPosition
```

> [!CAUTION]
> **This field is deprecated.**   
>
> Use `https://developer.android.com/reference/androidx/media3/datasource/DataSpec#position()` except for specific use cases where the absolute position within the resource is required within a `https://developer.android.com/reference/androidx/media3/datasource/DataSource` chain. Where the absolute position is required, use `uriPositionOffset + position`.

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 `https://developer.android.com/reference/androidx/media3/datasource/DataSpec` instances. The custom data should be immutable.

### flags

```
@DataSpec.Flags
public final int flags
```

Request `https://developer.android.com/reference/androidx/media3/datasource/DataSpec.Flags`.

### 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 `https://developer.android.com/reference/androidx/media3/datasource/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 `https://developer.android.com/reference/androidx/media3/datasource/HttpDataSource` implementations. In particular, this means the following headers are not included:

- `Range`: `https://developer.android.com/reference/androidx/media3/datasource/HttpDataSource` implementations derive the `Range` header from `https://developer.android.com/reference/androidx/media3/datasource/DataSpec#position()` and `https://developer.android.com/reference/androidx/media3/datasource/DataSpec#length()`.
- `Accept-Encoding`: `https://developer.android.com/reference/androidx/media3/datasource/HttpDataSource` implementations derive the `
  Accept-Encoding` header based on whether `https://developer.android.com/reference/androidx/media3/datasource/DataSpec#flags()` includes `https://developer.android.com/reference/androidx/media3/datasource/DataSpec#FLAG_ALLOW_GZIP()`.
- `User-Agent`: `https://developer.android.com/reference/androidx/media3/datasource/HttpDataSource` implementations set the `User-Agent` header directly.
- Other headers set at the `https://developer.android.com/reference/androidx/media3/datasource/HttpDataSource` layer. I.e., headers set using `https://developer.android.com/reference/androidx/media3/datasource/HttpDataSource#setRequestProperty(java.lang.String,java.lang.String)`, and using `https://developer.android.com/reference/androidx/media3/datasource/HttpDataSource.Factory#setDefaultRequestProperties(java.util.Map<java.lang.String,java.lang.String>)`.

### 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.

### length

```
public final long length
```

The length of the data, or `https://developer.android.com/reference/androidx/media3/common/C#LENGTH_UNSET()`.

### location

```
public final @Nullable String location
```

A logical identifier of the location (e.g., a service location, pathway, CDN identifier etc.) from which the resource is loaded, or `null` if the location is not known or not applicable.

### position

```
public final long position
```

The position of the data when read from `https://developer.android.com/reference/androidx/media3/datasource/DataSpec#uri()`.

### uri

```
public final Uri uri
```

A `https://developer.android.com/reference/android/net/Uri.html` from which data belonging to the resource can be read.

### uriPositionOffset

```
public final long uriPositionOffset
```

The offset of the data located at `https://developer.android.com/reference/androidx/media3/datasource/DataSpec#uri()` within the resource.

Equal to 0 unless `https://developer.android.com/reference/androidx/media3/datasource/DataSpec#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 `https://developer.android.com/reference/androidx/media3/datasource/DataSpec#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 `https://developer.android.com/reference/androidx/media3/datasource/DataSource` chain. One example is when a `https://developer.android.com/reference/androidx/media3/datasource/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

### DataSpec

```
public DataSpec(Uri uri)
```

Constructs an instance.

| Parameters |
|---|---|
| `https://developer.android.com/reference/android/net/Uri.html uri` | `https://developer.android.com/reference/androidx/media3/datasource/DataSpec#uri()`. |

### DataSpec

```
public DataSpec(Uri uri, long position, long length)
```

Constructs an instance.

| Parameters |
|---|---|
| `https://developer.android.com/reference/android/net/Uri.html uri` | `https://developer.android.com/reference/androidx/media3/datasource/DataSpec#uri()`. |
| `long position` | `https://developer.android.com/reference/androidx/media3/datasource/DataSpec#position()`. |
| `long length` | `https://developer.android.com/reference/androidx/media3/datasource/DataSpec#length()`. |

### DataSpec

```
public [DataSpec](https://developer.android.com/reference/androidx/media3/datasource/DataSpec#DataSpec(android.net.Uri,long,long,java.lang.String))(Uri uri, long position, long length, @Nullable String key)
```

> [!CAUTION]
> **This method is deprecated.**   
>
> Use `https://developer.android.com/reference/androidx/media3/datasource/DataSpec.Builder`.

Constructs an instance.

| Parameters |
|---|---|
| `https://developer.android.com/reference/android/net/Uri.html uri` | `https://developer.android.com/reference/androidx/media3/datasource/DataSpec#uri()`. |
| `long position` | `https://developer.android.com/reference/androidx/media3/datasource/DataSpec#position()`. |
| `long length` | `https://developer.android.com/reference/androidx/media3/datasource/DataSpec#length()`. |
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/java/lang/String.html key` | `https://developer.android.com/reference/androidx/media3/datasource/DataSpec#key()`. |

## Public methods

### buildUpon

```
public DataSpec.Builder buildUpon()
```

Returns a `https://developer.android.com/reference/androidx/media3/datasource/DataSpec.Builder` initialized with the values of this instance.

### getHttpMethodString

```
public String getHttpMethodString()
```

Returns the uppercase HTTP method name (e.g., "GET", "POST", "HEAD") corresponding to the `https://developer.android.com/reference/androidx/media3/datasource/DataSpec#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 `https://developer.android.com/reference/androidx/media3/datasource/DataSpec.HttpMethod`.

### isFlagSet

```
public boolean isFlagSet(@DataSpec.Flags int flag)
```

Returns whether the given flag is set.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/media3/datasource/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 |
|---|---|
| `https://developer.android.com/reference/androidx/media3/datasource/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 |
|---|---|
| `https://developer.android.com/reference/androidx/media3/datasource/DataSpec` | A data spec that represents a subrange of the data defined by this DataSpec. |

### toString

```
public String toString()
```

### 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.

| Parameters |
|---|---|
| `https://developer.android.com/reference/java/util/Map.html<https://developer.android.com/reference/java/lang/String.html, https://developer.android.com/reference/java/lang/String.html> additionalHttpRequestHeaders` | The additional HTTP request headers. |

| Returns |
|---|---|
| `https://developer.android.com/reference/androidx/media3/datasource/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.

| Parameters |
|---|---|
| `https://developer.android.com/reference/java/util/Map.html<https://developer.android.com/reference/java/lang/String.html, https://developer.android.com/reference/java/lang/String.html> httpRequestHeaders` | The HTTP request headers. |

| Returns |
|---|---|
| `https://developer.android.com/reference/androidx/media3/datasource/DataSpec` | The copied data spec with the specified HTTP request headers. |

### withUri

```
public DataSpec withUri(Uri uri)
```

Returns a copy of this data spec with the specified Uri.

| Parameters |
|---|---|
| `https://developer.android.com/reference/android/net/Uri.html uri` | The new source `https://developer.android.com/reference/android/net/Uri.html`. |

| Returns |
|---|---|
| `https://developer.android.com/reference/androidx/media3/datasource/DataSpec` | The copied data spec with the specified Uri. |