/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.android.exoplayer2.upstream;

import android.net.Uri;
import androidx.annotation.Nullable;
import com.google.android.exoplayer2.C;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/** Reads data from URI-identified resources. */
public interface DataSource extends DataReader {

  /**
   * A factory for {@link DataSource} instances.
   */
  interface Factory {

    /**
     * Creates a {@link DataSource} instance.
     */
    DataSource createDataSource();
  }

  /**
   * Adds a {@link TransferListener} to listen to data transfers. This method is not thread-safe.
   *
   * @param transferListener A {@link TransferListener}.
   */
  void addTransferListener(TransferListener transferListener);

  /**
   * Opens the source to read the specified data.
   * <p>
   * Note: If an {@link IOException} is thrown, callers must still call {@link #close()} to ensure
   * that any partial effects of the invocation are cleaned up.
   *
   * @param dataSpec Defines the data to be read.
   * @throws IOException If an error occurs opening the source. {@link DataSourceException} can be
   *     thrown or used as a cause of the thrown exception to specify the reason of the error.
   * @return The number of bytes that can be read from the opened source. For unbounded requests
   *     (i.e. requests where {@link DataSpec#length} equals {@link C#LENGTH_UNSET}) this value
   *     is the resolved length of the request, or {@link C#LENGTH_UNSET} if the length is still
   *     unresolved. For all other requests, the value returned will be equal to the request's
   *     {@link DataSpec#length}.
   */
  long open(DataSpec dataSpec) throws IOException;

  /**
   * When the source is open, returns the {@link Uri} from which data is being read. The returned
   * {@link Uri} will be identical to the one passed {@link #open(DataSpec)} in the {@link DataSpec}
   * unless redirection has occurred. If redirection has occurred, the {@link Uri} after redirection
   * is returned.
   *
   * @return The {@link Uri} from which data is being read, or null if the source is not open.
   */
  @Nullable Uri getUri();

  /**
   * When the source is open, returns the response headers associated with the last {@link #open}
   * call. Otherwise, returns an empty map.
   *
   * <p>Key look-up in the returned map is case-insensitive.
   */
  default Map<String, List<String>> getResponseHeaders() {
    return Collections.emptyMap();
  }

  /**
   * Closes the source.
   * <p>
   * Note: This method must be called even if the corresponding call to {@link #open(DataSpec)}
   * threw an {@link IOException}. See {@link #open(DataSpec)} for more details.
   *
   * @throws IOException If an error occurs closing the source.
   */
  void close() throws IOException;
}

@@ -1443,36 +1443,16 @@
 ed data.
-%0A   * %3Cp%3E%0A   * Note:
  If an %7B
@@ -1493,16 +1493,21 @@
 ers must
+%0A   *
  still c
@@ -1540,72 +1540,1005 @@
 sure
-%0A   * that any partial effects of the invocation are cleaned up.
+ that any partial effects of the invocation are cleaned%0A   * up.%0A   *%0A   * %3Cp%3EThe following edge case behaviors apply:%0A   *%0A   * %3Cul%3E%0A   *   %3Cli%3EIf the %7B@link DataSpec#position requested position%7D is within the resource, but the%0A   *       %7B@link DataSpec#length requested length%7D extends beyond the end of the resource, then%0A   *       %7B@link #open%7D will succeed and data from the requested position to the end of the%0A   *       resource will be made available through %7B@link #read%7D.%0A   *   %3Cli%3EIf the %7B@link DataSpec#position requested position%7D is equal to the length of the%0A   *       resource, then %7B@link #open%7D will succeed, and %7B@link #read%7D will immediately return%0A   *       %7B@link C#RESULT_END_OF_INPUT%7D.%0A   *   %3Cli%3EIf the %7B@link DataSpec#position requested position%7D is greater than the length of the%0A   *       resource, then %7B@link #open%7D will throw an %7B@link IOException%7D for which %7B@link%0A   *       DataSourceException#isCausedByPositionOutOfRange%7D will be %7B@code true%7D.%0A   * %3C/ul%3E
 %0A   
@@ -2894,16 +2894,17 @@
    (i.e.
+,
  request
@@ -2976,16 +2976,19 @@
 is value
+ is
 %0A   *   
@@ -2989,19 +2989,16 @@
   *     
-is 
 the reso
@@ -4070,28 +4070,8 @@
 rce.
-%0A   * %3Cp%3E%0A   * Note:
  Thi
@@ -4130,24 +4130,29 @@
 ll to %7B@link
+%0A   *
  #open(DataS
@@ -4156,21 +4156,16 @@
 taSpec)%7D
-%0A   *
  threw a
@@ -4190,54 +4190,8 @@
 on%7D.
- See %7B@link #open(DataSpec)%7D for more details.
 %0A   

Document DataSource edge-case behaviors