Rollback set content length and redirect URI

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=196765970
This commit is contained in:
pfxing 2018-05-15 18:54:40 -07:00 committed by Andrew Lewis
parent 75db04d51d
commit 5ffb4d8f55

View File

@ -18,6 +18,7 @@ package com.google.android.exoplayer2.upstream.cache;
import android.net.Uri; import android.net.Uri;
import android.support.annotation.IntDef; import android.support.annotation.IntDef;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.util.Log;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.upstream.DataSink; import com.google.android.exoplayer2.upstream.DataSink;
import com.google.android.exoplayer2.upstream.DataSource; import com.google.android.exoplayer2.upstream.DataSource;
@ -51,6 +52,8 @@ public final class CacheDataSource implements DataSource {
*/ */
public static final long DEFAULT_MAX_CACHE_FILE_SIZE = 2 * 1024 * 1024; public static final long DEFAULT_MAX_CACHE_FILE_SIZE = 2 * 1024 * 1024;
private static final String TAG = "CacheDataSource";
/** /**
* Flags controlling the cache's behavior. * Flags controlling the cache's behavior.
*/ */
@ -218,7 +221,7 @@ public final class CacheDataSource implements DataSource {
try { try {
key = CacheUtil.getKey(dataSpec); key = CacheUtil.getKey(dataSpec);
uri = dataSpec.uri; uri = dataSpec.uri;
actualUri = getRedirectedUriOrDefault(cache, key, /* defaultUri= */ uri); actualUri = loadRedirectedUriOrReturnGivenUri(cache, key, uri);
flags = dataSpec.flags; flags = dataSpec.flags;
readPosition = dataSpec.position; readPosition = dataSpec.position;
@ -269,7 +272,7 @@ public final class CacheDataSource implements DataSource {
bytesRemaining -= bytesRead; bytesRemaining -= bytesRead;
} }
} else if (currentDataSpecLengthUnset) { } else if (currentDataSpecLengthUnset) {
setNoBytesRemainingAndMaybeStoreLength(); setBytesRemainingAndMaybeStoreLength(0);
} else if (bytesRemaining > 0 || bytesRemaining == C.LENGTH_UNSET) { } else if (bytesRemaining > 0 || bytesRemaining == C.LENGTH_UNSET) {
closeCurrentSource(); closeCurrentSource();
openNextSource(false); openNextSource(false);
@ -278,7 +281,7 @@ public final class CacheDataSource implements DataSource {
return bytesRead; return bytesRead;
} catch (IOException e) { } catch (IOException e) {
if (currentDataSpecLengthUnset && isCausedByPositionOutOfRange(e)) { if (currentDataSpecLengthUnset && isCausedByPositionOutOfRange(e)) {
setNoBytesRemainingAndMaybeStoreLength(); setBytesRemainingAndMaybeStoreLength(0);
return C.RESULT_END_OF_INPUT; return C.RESULT_END_OF_INPUT;
} }
handleBeforeThrow(e); handleBeforeThrow(e);
@ -399,38 +402,46 @@ public final class CacheDataSource implements DataSource {
currentDataSource = nextDataSource; currentDataSource = nextDataSource;
currentDataSpecLengthUnset = nextDataSpec.length == C.LENGTH_UNSET; currentDataSpecLengthUnset = nextDataSpec.length == C.LENGTH_UNSET;
long resolvedLength = nextDataSource.open(nextDataSpec); long resolvedLength = nextDataSource.open(nextDataSpec);
// Update bytesRemaining, actualUri and (if writing to cache) the cache metadata.
ContentMetadataMutations mutations = new ContentMetadataMutations();
if (currentDataSpecLengthUnset && resolvedLength != C.LENGTH_UNSET) { if (currentDataSpecLengthUnset && resolvedLength != C.LENGTH_UNSET) {
bytesRemaining = resolvedLength; setBytesRemainingAndMaybeStoreLength(resolvedLength);
ContentMetadataInternal.setContentLength(mutations, readPosition + bytesRemaining);
} }
if (isReadingFromUpstream()) { // TODO find a way to store length and redirected uri in one metadata mutation.
actualUri = currentDataSource.getUri(); maybeUpdateActualUriFieldAndRedirectedUriMetadata();
boolean isRedirected = !uri.equals(actualUri); }
if (isRedirected) {
ContentMetadataInternal.setRedirectedUri(mutations, actualUri); private void maybeUpdateActualUriFieldAndRedirectedUriMetadata() {
} else { if (!isReadingFromUpstream()) {
ContentMetadataInternal.removeRedirectedUri(mutations); return;
}
} }
if (isWritingToCache()) { actualUri = currentDataSource.getUri();
maybeUpdateRedirectedUriMetadata();
}
private void maybeUpdateRedirectedUriMetadata() {
if (!isWritingToCache()) {
return;
}
ContentMetadataMutations mutations = new ContentMetadataMutations();
boolean isRedirected = !uri.equals(actualUri);
if (isRedirected) {
ContentMetadataInternal.setRedirectedUri(mutations, actualUri);
} else {
ContentMetadataInternal.removeRedirectedUri(mutations);
}
try {
cache.applyContentMetadataMutations(key, mutations); cache.applyContentMetadataMutations(key, mutations);
} catch (CacheException e) {
String message =
"Couldn't update redirected URI. "
+ "This might cause relative URIs get resolved incorrectly.";
Log.w(TAG, message, e);
} }
} }
private void setNoBytesRemainingAndMaybeStoreLength() throws IOException { private static Uri loadRedirectedUriOrReturnGivenUri(Cache cache, String key, Uri uri) {
bytesRemaining = 0;
if (isWritingToCache()) {
cache.setContentLength(key, readPosition);
}
}
private static Uri getRedirectedUriOrDefault(Cache cache, String key, Uri defaultUri) {
ContentMetadata contentMetadata = cache.getContentMetadata(key); ContentMetadata contentMetadata = cache.getContentMetadata(key);
Uri redirectedUri = ContentMetadataInternal.getRedirectedUri(contentMetadata); Uri redirectedUri = ContentMetadataInternal.getRedirectedUri(contentMetadata);
return redirectedUri == null ? defaultUri : redirectedUri; return redirectedUri == null ? uri : redirectedUri;
} }
private static boolean isCausedByPositionOutOfRange(IOException e) { private static boolean isCausedByPositionOutOfRange(IOException e) {
@ -447,6 +458,13 @@ public final class CacheDataSource implements DataSource {
return false; return false;
} }
private void setBytesRemainingAndMaybeStoreLength(long bytesRemaining) throws IOException {
this.bytesRemaining = bytesRemaining;
if (isWritingToCache()) {
cache.setContentLength(key, readPosition + bytesRemaining);
}
}
private boolean isReadingFromUpstream() { private boolean isReadingFromUpstream() {
return !isReadingFromCache(); return !isReadingFromCache();
} }