# DataSourceUtil

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/DataSourceUtil.java+class:androidx.media3.datasource.DataSourceUtil)

*** ** * ** ***


```
@UnstableApi
public final class DataSourceUtil
```

<br />

*** ** * ** ***

Utility methods for `https://developer.android.com/reference/androidx/media3/datasource/DataSource`.

## Summary

| ### Public methods |
|---|---|
| `static void` | `https://developer.android.com/reference/androidx/media3/datasource/DataSourceUtil#closeQuietly(androidx.media3.datasource.DataSource)(@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/media3/datasource/DataSource dataSource)` Closes a `https://developer.android.com/reference/androidx/media3/datasource/DataSource`, suppressing any `https://developer.android.com/reference/java/io/IOException.html` that may occur. |
| `static byte[]` | `https://developer.android.com/reference/androidx/media3/datasource/DataSourceUtil#readExactly(androidx.media3.datasource.DataSource,int)(https://developer.android.com/reference/androidx/media3/datasource/DataSource dataSource, int length)` Reads `length` bytes from the specified opened `https://developer.android.com/reference/androidx/media3/datasource/DataSource`, and returns a byte array containing the read data. |
| `static byte[]` | `https://developer.android.com/reference/androidx/media3/datasource/DataSourceUtil#readToEnd(androidx.media3.datasource.DataSource)(https://developer.android.com/reference/androidx/media3/datasource/DataSource dataSource)` Reads data from the specified opened `https://developer.android.com/reference/androidx/media3/datasource/DataSource` until it ends, and returns a byte array containing the read data. |

## Public methods

### closeQuietly

```
public static void closeQuietly(@Nullable DataSource dataSource)
```

Closes a `https://developer.android.com/reference/androidx/media3/datasource/DataSource`, suppressing any `https://developer.android.com/reference/java/io/IOException.html` that may occur.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/media3/datasource/DataSource dataSource` | The `https://developer.android.com/reference/androidx/media3/datasource/DataSource` to close. |

### readExactly

```
public static byte[] readExactly(DataSource dataSource, int length)
```

Reads `length` bytes from the specified opened `https://developer.android.com/reference/androidx/media3/datasource/DataSource`, and returns a byte array containing the read data.

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/datasource/DataSource dataSource` | The source from which to read. |
| `int length` | The number of bytes to read. |

| Returns |
|---|---|
| `byte[]` | The read data. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/io/IOException.html` | If an error occurs reading from the source. |
| `https://developer.android.com/reference/java/lang/IllegalStateException.html` | If the end of the source was reached before `length` bytes could be read. |

### readToEnd

```
public static byte[] readToEnd(DataSource dataSource)
```

Reads data from the specified opened `https://developer.android.com/reference/androidx/media3/datasource/DataSource` until it ends, and returns a byte array containing the read data.

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/datasource/DataSource dataSource` | The source from which to read. |

| Returns |
|---|---|
| `byte[]` | The concatenation of all read data. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/io/IOException.html` | If an error occurs reading from the source. |