Package com.google.android.exoplayer2.ext.cronet

## Class CronetDataSource

- java.lang.Object
- - com.google.android.exoplayer2.upstream.BaseDataSource
  - - com.google.android.exoplayer2.ext.cronet.CronetDataSource

- All Implemented Interfaces:
    `DataReader`, `DataSource`, `HttpDataSource`

      public class CronetDataSource
      extends BaseDataSource
      implements HttpDataSource

  DataSource without intermediate buffer based on Cronet API set using UrlRequest.

  Note: HTTP request headers will be set using all parameters passed via (in order of decreasing priority) the `dataSpec`, `setRequestProperty(java.lang.String, java.lang.String)` and the default parameters used to construct the instance.

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class | Description
    --- | --- | ---
    `static class` | `CronetDataSource.Factory` | `DataSource.Factory` for `CronetDataSource` instances.
    `static class` | `CronetDataSource.OpenException` | Thrown when an error is encountered when trying to open a `CronetDataSource`.

    - ### Nested classes/interfaces inherited from interface com.google.android.exoplayer2.upstream.HttpDataSource

      `HttpDataSource.BaseFactory, HttpDataSource.CleartextNotPermittedException, HttpDataSource.HttpDataSourceException, HttpDataSource.InvalidContentTypeException, HttpDataSource.InvalidResponseCodeException, HttpDataSource.RequestProperties`

  - ### Field Summary

    Fields
    Modifier and Type | Field | Description
    --- | --- | ---
    `static int` | `DEFAULT_CONNECT_TIMEOUT_MILLIS` | The default connection timeout, in milliseconds.
    `static int` | `DEFAULT_READ_TIMEOUT_MILLIS` | The default read timeout, in milliseconds.

    - ### Fields inherited from interface com.google.android.exoplayer2.upstream.HttpDataSource

      `REJECT_PAYWALL_TYPES`

  - ### Constructor Summary

    Constructors
    Modifier | Constructor | Description
    --- | --- | ---
    `` | `CronetDataSource​(org.chromium.net.CronetEngine cronetEngine, Executor executor)` | Deprecated. Use `CronetDataSource.Factory` instead.
    `` | `CronetDataSource​(org.chromium.net.CronetEngine cronetEngine, Executor executor, int connectTimeoutMs, int readTimeoutMs, boolean resetTimeoutOnRedirects, HttpDataSource.RequestProperties defaultRequestProperties)` | Deprecated. Use `CronetDataSource.Factory` instead.
    `` | `CronetDataSource​(org.chromium.net.CronetEngine cronetEngine, Executor executor, int connectTimeoutMs, int readTimeoutMs, boolean resetTimeoutOnRedirects, HttpDataSource.RequestProperties defaultRequestProperties, boolean handleSetCookieRequests)` | Deprecated. Use `CronetDataSource.Factory` instead.
    `protected` | `CronetDataSource​(org.chromium.net.CronetEngine cronetEngine, Executor executor, int requestPriority, int connectTimeoutMs, int readTimeoutMs, boolean resetTimeoutOnRedirects, boolean handleSetCookieRequests, String userAgent, HttpDataSource.RequestProperties defaultRequestProperties, Predicate<String> contentTypePredicate, boolean keepPostFor302Redirects)` |
    `` | `CronetDataSource​(org.chromium.net.CronetEngine cronetEngine, Executor executor, Predicate<String> contentTypePredicate)` | Deprecated. Use `CronetDataSource.Factory` instead.
    `` | `CronetDataSource​(org.chromium.net.CronetEngine cronetEngine, Executor executor, Predicate<String> contentTypePredicate, int connectTimeoutMs, int readTimeoutMs, boolean resetTimeoutOnRedirects, HttpDataSource.RequestProperties defaultRequestProperties)` | Deprecated. Use `CronetDataSource.Factory` instead.
    `` | `CronetDataSource​(org.chromium.net.CronetEngine cronetEngine, Executor executor, Predicate<String> contentTypePredicate, int connectTimeoutMs, int readTimeoutMs, boolean resetTimeoutOnRedirects, HttpDataSource.RequestProperties defaultRequestProperties, boolean handleSetCookieRequests)` | Deprecated. Use `CronetDataSource.Factory` instead.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods Deprecated Methods
    Modifier and Type | Method | Description
    --- | --- | ---
    `protected org.chromium.net.UrlRequest.Builder` | `buildRequestBuilder​(DataSpec dataSpec)` |
    `void` | `clearAllRequestProperties()` | Clears all request headers that were set by `HttpDataSource.setRequestProperty(String, String)`.
    `void` | `clearRequestProperty​(String name)` | Clears the value of a request header.
    `void` | `close()` | Closes the source.
    `protected org.chromium.net.UrlRequest` | `getCurrentUrlRequest()` | Returns current `UrlRequest`.
    `protected org.chromium.net.UrlResponseInfo` | `getCurrentUrlResponseInfo()` | Returns current `UrlResponseInfo`.
    `int` | `getResponseCode()` | When the source is open, returns the HTTP response status code associated with the last `HttpDataSource.open(com.google.android.exoplayer2.upstream.DataSpec)` call.
    `Map<String,​List<String>>` | `getResponseHeaders()` | When the source is open, returns the response headers associated with the last `DataSource.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.
    `int` | `read​(byte[] buffer, int offset, int length)` | Reads up to `length` bytes of data from the input.
    `int` | `read​(ByteBuffer buffer)` | Reads up to `buffer.remaining()` bytes of data and stores them into `buffer`, starting at `buffer.position()`.
    `void` | `setContentTypePredicate​(Predicate<String> contentTypePredicate)` | Deprecated.
    `void` | `setRequestProperty​(String name, String value)` | Sets the value of a request header.

    - ### Methods inherited from class com.google.android.exoplayer2.upstream.BaseDataSource

      `addTransferListener, bytesTransferred, transferEnded, transferInitializing, transferStarted`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

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

      `addTransferListener`

- - ### Field Detail

    - #### DEFAULT_CONNECT_TIMEOUT_MILLIS

          public static final int DEFAULT_CONNECT_TIMEOUT_MILLIS

      The default connection timeout, in milliseconds.
      See Also:
        Constant Field Values

    - #### DEFAULT_READ_TIMEOUT_MILLIS

          public static final int DEFAULT_READ_TIMEOUT_MILLIS

      The default read timeout, in milliseconds.
      See Also:
        Constant Field Values

  - ### Constructor Detail

    - #### CronetDataSource

          @Deprecated
          public CronetDataSource​(org.chromium.net.CronetEngine cronetEngine,
                                  Executor executor)

      Deprecated.
      Use `CronetDataSource.Factory` instead.

    - #### CronetDataSource

          @Deprecated
          public CronetDataSource​(org.chromium.net.CronetEngine cronetEngine,
                                  Executor executor,
                                  int connectTimeoutMs,
                                  int readTimeoutMs,
                                  boolean resetTimeoutOnRedirects,
                                  @Nullable
                                  HttpDataSource.RequestProperties defaultRequestProperties)

      Deprecated.
      Use `CronetDataSource.Factory` instead.

    - #### CronetDataSource

          @Deprecated
          public CronetDataSource​(org.chromium.net.CronetEngine cronetEngine,
                                  Executor executor,
                                  int connectTimeoutMs,
                                  int readTimeoutMs,
                                  boolean resetTimeoutOnRedirects,
                                  @Nullable
                                  HttpDataSource.RequestProperties defaultRequestProperties,
                                  boolean handleSetCookieRequests)

      Deprecated.
      Use `CronetDataSource.Factory` instead.

    - #### CronetDataSource

          @Deprecated
          public CronetDataSource​(org.chromium.net.CronetEngine cronetEngine,
                                  Executor executor,
                                  @Nullable
                                  Predicate<String> contentTypePredicate)

      Deprecated.
      Use `CronetDataSource.Factory` instead.

    - #### CronetDataSource

          @Deprecated
          public CronetDataSource​(org.chromium.net.CronetEngine cronetEngine,
                                  Executor executor,
                                  @Nullable
                                  Predicate<String> contentTypePredicate,
                                  int connectTimeoutMs,
                                  int readTimeoutMs,
                                  boolean resetTimeoutOnRedirects,
                                  @Nullable
                                  HttpDataSource.RequestProperties defaultRequestProperties)

      Deprecated.
      Use `CronetDataSource.Factory` instead.

    - #### CronetDataSource

          @Deprecated
          public CronetDataSource​(org.chromium.net.CronetEngine cronetEngine,
                                  Executor executor,
                                  @Nullable
                                  Predicate<String> contentTypePredicate,
                                  int connectTimeoutMs,
                                  int readTimeoutMs,
                                  boolean resetTimeoutOnRedirects,
                                  @Nullable
                                  HttpDataSource.RequestProperties defaultRequestProperties,
                                  boolean handleSetCookieRequests)

      Deprecated.
      Use `CronetDataSource.Factory` instead.

    - #### CronetDataSource

          protected CronetDataSource​(org.chromium.net.CronetEngine cronetEngine,
                                     Executor executor,
                                     int requestPriority,
                                     int connectTimeoutMs,
                                     int readTimeoutMs,
                                     boolean resetTimeoutOnRedirects,
                                     boolean handleSetCookieRequests,
                                     @Nullable
                                     String userAgent,
                                     @Nullable
                                     HttpDataSource.RequestProperties defaultRequestProperties,
                                     @Nullable
                                     Predicate<String> contentTypePredicate,
                                     boolean keepPostFor302Redirects)

  - ### Method Detail

    - #### setContentTypePredicate

          @Deprecated
          public void setContentTypePredicate​(@Nullable
                                              Predicate<String> contentTypePredicate)

      Deprecated.
      Sets a content type `Predicate`. If a content type is rejected by the predicate then a `HttpDataSource.InvalidContentTypeException` is thrown from `open(DataSpec)`.
      Parameters:
        `contentTypePredicate` - The content type `Predicate`, or `null` to clear a predicate that was previously set.

    - #### setRequestProperty

          public void setRequestProperty​(String name,
                                         String value)

      Description copied from interface: `HttpDataSource`
      Sets the value of a request header. The value will be used for subsequent connections established by the source.

      Note: If the same header is set as a default parameter in the `HttpDataSource.Factory`, then the header value set with this method should be preferred when connecting with the data source. See `HttpDataSource.open(com.google.android.exoplayer2.upstream.DataSpec)`.

      Specified by:
        `setRequestProperty` in interface `HttpDataSource`
      Parameters:
        `name` - The name of the header field.
        `value` - The value of the field.

    - #### clearRequestProperty

          public void clearRequestProperty​(String name)

      Description copied from interface: `HttpDataSource`
      Clears the value of a request header. The change will apply to subsequent connections established by the source.
      Specified by:
        `clearRequestProperty` in interface `HttpDataSource`
      Parameters:
        `name` - The name of the header field.

    - #### clearAllRequestProperties

          public void clearAllRequestProperties()

      Description copied from interface: `HttpDataSource`
      Clears all request headers that were set by `HttpDataSource.setRequestProperty(String, String)`.
      Specified by:
        `clearAllRequestProperties` in interface `HttpDataSource`

    - #### getResponseCode

          public int getResponseCode()

      Description copied from interface: `HttpDataSource`
      When the source is open, returns the HTTP response status code associated with the last `HttpDataSource.open(com.google.android.exoplayer2.upstream.DataSpec)` call. Otherwise, returns a negative value.
      Specified by:
        `getResponseCode` in interface `HttpDataSource`

    - #### getResponseHeaders

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

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

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

      Specified by:
        `getResponseHeaders` in interface `DataSource`
      Specified by:
        `getResponseHeaders` in interface `HttpDataSource`

    - #### getUri

          @Nullable
          public Uri getUri()

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

    - #### open

          public long open​(DataSpec dataSpec)
                    throws HttpDataSource.HttpDataSourceException

      Description copied from interface: `HttpDataSource`
      Opens the source to read the specified data.

      Note: `HttpDataSource` implementations are advised to set request headers passed via (in order of decreasing priority) the `dataSpec`, `HttpDataSource.setRequestProperty(java.lang.String, java.lang.String)` and the default parameters set in the `HttpDataSource.Factory`.

      Specified by:
        `open` in interface `DataSource`
      Specified by:
        `open` in interface `HttpDataSource`
      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:
        `HttpDataSource.HttpDataSourceException`

    - #### read

          public int read​(byte[] buffer,
                          int offset,
                          int length)
                   throws HttpDataSource.HttpDataSourceException

      Description copied from interface: `DataReader`
      Reads up to `length` bytes of data from the input.

      If `readLength` is zero then 0 is returned. Otherwise, if no data is available because the end of the opened range has been reached, then `C.RESULT_END_OF_INPUT` is returned. Otherwise, the call will block until at least one byte of data has been read and the number of bytes read is returned.

      Specified by:
        `read` in interface `DataReader`
      Specified by:
        `read` in interface `HttpDataSource`
      Parameters:
        `buffer` - A target array into which data should be written.
        `offset` - The offset into the target array at which to write.
        `length` - The maximum number of bytes to read from the input.
      Returns:
        The number of bytes read, or `C.RESULT_END_OF_INPUT` if the input has ended. This may be less than `length` because the end of the input (or available data) was reached, the method was interrupted, or the operation was aborted early for another reason.
      Throws:
        `HttpDataSource.HttpDataSourceException`

    - #### read

          public int read​(ByteBuffer buffer)
                   throws HttpDataSource.HttpDataSourceException

      Reads up to `buffer.remaining()` bytes of data and stores them into `buffer`, starting at `buffer.position()`. Advances the position of the buffer by the number of bytes read and returns this length.

      If there is an error, a `HttpDataSource.HttpDataSourceException` is thrown and the contents of `buffer` should be ignored. If the exception has error code `HttpDataSourceException.TYPE_READ`, note that Cronet may continue writing into `buffer` after the method has returned. Thus the caller should not attempt to reuse the buffer.

      If `buffer.remaining()` is zero then 0 is returned. Otherwise, if no data is available because the end of the opened range has been reached, then `C.RESULT_END_OF_INPUT` is returned. Otherwise, the call will block until at least one byte of data has been read and the number of bytes read is returned.

      Passed buffer must be direct ByteBuffer. If you have a non-direct ByteBuffer, consider the alternative read method with its backed array.

      Parameters:
        `buffer` - The ByteBuffer into which the read data should be stored. Must be a direct ByteBuffer.
      Returns:
        The number of bytes read, or `C.RESULT_END_OF_INPUT` if no data is available because the end of the opened range has been reached.
      Throws:
        `HttpDataSource.HttpDataSourceException` - If an error occurs reading from the source.
        `IllegalArgumentException` - If `buffer` is not a direct ByteBuffer.

    - #### close

          public void close()

      Description copied from interface: `DataSource`
      Closes the source. This method must be called even if the corresponding call to `DataSource.open(DataSpec)` threw an `IOException`.
      Specified by:
        `close` in interface `DataSource`
      Specified by:
        `close` in interface `HttpDataSource`

    - #### getCurrentUrlRequest

          @Nullable
          protected org.chromium.net.UrlRequest getCurrentUrlRequest()

      Returns current `UrlRequest`. May be null if the data source is not opened.

    - #### getCurrentUrlResponseInfo

          @Nullable
          protected org.chromium.net.UrlResponseInfo getCurrentUrlResponseInfo()

      Returns current `UrlResponseInfo`. May be null if the data source is not opened.

    - #### buildRequestBuilder

          protected org.chromium.net.UrlRequest.Builder buildRequestBuilder​(DataSpec dataSpec)
                                                                     throws IOException

      Throws:
        `IOException`