Promote getResponseHeaders to DataSource

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=199607604
This commit is contained in:
aquilescanta 2018-06-07 03:07:27 -07:00 committed by Oliver Woodman
parent ccc41f1adf
commit bb684b528e
9 changed files with 63 additions and 22 deletions

View file

@ -32,6 +32,7 @@ import java.io.IOException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
@ -247,7 +248,7 @@ public class CronetDataSource extends UrlRequest.Callback implements HttpDataSou
@Override
public Map<String, List<String>> getResponseHeaders() {
return responseInfo == null ? null : responseInfo.getAllHeaders();
return responseInfo == null ? Collections.emptyMap() : responseInfo.getAllHeaders();
}
@Override

View file

@ -30,6 +30,7 @@ import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InterruptedIOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
@ -131,7 +132,7 @@ public class OkHttpDataSource implements HttpDataSource {
@Override
public Map<String, List<String>> getResponseHeaders() {
return response == null ? null : response.headers().toMultimap();
return response == null ? Collections.emptyMap() : response.headers().toMultimap();
}
@Override

View file

@ -19,6 +19,9 @@ import android.net.Uri;
import android.support.annotation.Nullable;
import com.google.android.exoplayer2.C;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
/**
* A component from which streams of data can be read.
@ -82,6 +85,14 @@ public interface DataSource {
*/
@Nullable Uri getUri();
/**
* When the source is open, returns the response headers associated with the last {@link #open}
* call. Otherwise, returns an empty map.
*/
default Map<String, List<String>> getResponseHeaders() {
return Collections.emptyMap();
}
/**
* Closes the source.
* <p>

View file

@ -21,6 +21,8 @@ import android.util.Log;
import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.Util;
import java.io.IOException;
import java.util.List;
import java.util.Map;
/**
* A {@link DataSource} that supports multiple URI schemes. The supported schemes are:
@ -157,6 +159,13 @@ public final class DefaultDataSource implements DataSource {
return dataSource == null ? null : dataSource.getUri();
}
@Override
public Map<String, List<String>> getResponseHeaders() {
return dataSource == null
? DataSource.super.getResponseHeaders()
: dataSource.getResponseHeaders();
}
@Override
public void close() throws IOException {
if (dataSource != null) {

View file

@ -32,6 +32,7 @@ import java.net.HttpURLConnection;
import java.net.NoRouteToHostException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
@ -162,7 +163,7 @@ public class DefaultHttpDataSource implements HttpDataSource {
@Override
public Map<String, List<String>> getResponseHeaders() {
return connection == null ? null : connection.getHeaderFields();
return connection == null ? Collections.emptyMap() : connection.getHeaderFields();
}
@Override

View file

@ -341,10 +341,6 @@ public interface HttpDataSource extends DataSource {
*/
void clearAllRequestProperties();
/**
* Returns the headers provided in the response, or {@code null} if response headers are
* unavailable.
*/
@Override
Map<String, List<String>> getResponseHeaders();
}

View file

@ -31,6 +31,8 @@ import java.io.IOException;
import java.io.InterruptedIOException;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.List;
import java.util.Map;
/**
* A {@link DataSource} that reads and writes a {@link Cache}. Requests are fulfilled from the cache
@ -332,6 +334,14 @@ public final class CacheDataSource implements DataSource {
return actualUri;
}
@Override
public Map<String, List<String>> getResponseHeaders() {
// TODO: Implement.
return isReadingFromUpstream()
? upstreamDataSource.getResponseHeaders()
: DataSource.super.getResponseHeaders();
}
@Override
public void close() throws IOException {
uri = null;

View file

@ -20,6 +20,8 @@ import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DataSpec;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import javax.crypto.Cipher;
/**
@ -59,15 +61,19 @@ public final class AesCipherDataSource implements DataSource {
return read;
}
@Override
public void close() throws IOException {
cipher = null;
upstream.close();
}
@Override
public Uri getUri() {
return upstream.getUri();
}
@Override
public Map<String, List<String>> getResponseHeaders() {
return upstream.getResponseHeaders();
}
@Override
public void close() throws IOException {
cipher = null;
upstream.close();
}
}

View file

@ -27,6 +27,8 @@ import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.spec.AlgorithmParameterSpec;
import java.util.List;
import java.util.Map;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.NoSuchPaddingException;
@ -85,14 +87,6 @@ import javax.crypto.spec.SecretKeySpec;
return C.LENGTH_UNSET;
}
@Override
public void close() throws IOException {
if (cipherInputStream != null) {
cipherInputStream = null;
upstream.close();
}
}
@Override
public int read(byte[] buffer, int offset, int readLength) throws IOException {
Assertions.checkState(cipherInputStream != null);
@ -108,4 +102,16 @@ import javax.crypto.spec.SecretKeySpec;
return upstream.getUri();
}
@Override
public Map<String, List<String>> getResponseHeaders() {
return upstream.getResponseHeaders();
}
@Override
public void close() throws IOException {
if (cipherInputStream != null) {
cipherInputStream = null;
upstream.close();
}
}
}