Package com.google.android.exoplayer2.upstream

## Interface DataSource

- All Superinterfaces:
    `DataReader`
  All Known Subinterfaces:
    `HttpDataSource`
  All Known Implementing Classes:
    `AesCipherDataSource`, `AssetDataSource`, `BaseDataSource`, `ByteArrayDataSource`, `CacheDataSource`, `ContentDataSource`, `CronetDataSource`, `DataSchemeDataSource`, `DefaultDataSource`, `DefaultHttpDataSource`, `FakeDataSource`, `FileDataSource`, `OkHttpDataSource`, `PlaceholderDataSource`, `PriorityDataSource`, `RawResourceDataSource`, `ResolvingDataSource`, `RtmpDataSource`, `StatsDataSource`, `TeeDataSource`, `UdpDataSource`

      public interface DataSource
      extends DataReader

  Reads data from URI-identified resources.

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Interface | Description
    --- | --- | ---
    `static interface` | `DataSource.Factory` | A factory for `DataSource` instances.

  - ### Method Summary

    All Methods Instance Methods Abstract Methods Default Methods
    Modifier and Type | Method | Description
    --- | --- | ---
    `void` | `addTransferListener​(TransferListener transferListener)` | Adds a `TransferListener` to listen to data transfers.
    `void` | `close()` | Closes the source.
    `default Map<String,​List<String>>` | `getResponseHeaders()` | When the source is open, returns the response headers associated with the last `open(com.google.android.exoplayer2.upstream.DataSpec)` call.
    `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.

    - ### Methods inherited from interface com.google.android.exoplayer2.upstream.DataReader

      `read`

- - ### Method Detail

    - #### addTransferListener

          void addTransferListener​(TransferListener transferListener)

      Adds a `TransferListener` to listen to data transfers. This method is not thread-safe.
      Parameters:
        `transferListener` - A `TransferListener`.

    - #### open

          long open​(DataSpec dataSpec)
             throws IOException

      Opens the source to read the specified data. If an `IOException` is thrown, callers must still call `close()` to ensure that any partial effects of the invocation are cleaned up.

      The following edge case behaviors apply:

      - If the `requested position` is within the resource, but the `requested length` extends beyond the end of the resource, then `open(com.google.android.exoplayer2.upstream.DataSpec)` will succeed and data from the requested position to the end of the resource will be made available through `DataReader.read(byte[], int, int)`.
      - If the `requested position` is equal to the length of the resource, then `open(com.google.android.exoplayer2.upstream.DataSpec)` will succeed, and `DataReader.read(byte[], int, int)` will immediately return `C.RESULT_END_OF_INPUT`.
      - If the `requested position` is greater than the length of the resource, then `open(com.google.android.exoplayer2.upstream.DataSpec)` will throw an `IOException` for which `DataSourceException.isCausedByPositionOutOfRange(java.io.IOException)` will be `true`.
      Parameters:
        `dataSpec` - Defines the data to be read.
      Returns:
        The number of bytes that can be read from the opened source. For unbounded requests (i.e., requests where `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`.
      Throws:
        `IOException` - If an error occurs opening the source. `DataSourceException` can be thrown or used as a cause of the thrown exception to specify the reason of the error.

    - #### getUri

          @Nullable
          Uri getUri()

      When the source is open, returns the `Uri` from which data is being read. The returned `Uri` will be identical to the one passed `open(DataSpec)` in the `DataSpec` unless redirection has occurred. If redirection has occurred, the `Uri` after redirection is returned.
      Returns:
        The `Uri` from which data is being read, or null if the source is not open.

    - #### getResponseHeaders

          default Map<String,​List<String>> getResponseHeaders()

      When the source is open, returns the response headers associated with the last `open(com.google.android.exoplayer2.upstream.DataSpec)` call. Otherwise, returns an empty map.

      Key look-up in the returned map is case-insensitive.

    - #### close

          void close()
              throws IOException

      Closes the source. This method must be called even if the corresponding call to `open(DataSpec)` threw an `IOException`.
      Throws:
        `IOException` - If an error occurs closing the source.