Commit ea08bfd3 by olly Committed by Oliver Woodman

Move isCausedByPositionOutOfRange to DataSourceException

PiperOrigin-RevId: 311002702
parent 6bf89bb4
......@@ -15,6 +15,7 @@
*/
package com.google.android.exoplayer2.upstream;
import androidx.annotation.Nullable;
import java.io.IOException;
/**
......@@ -22,6 +23,24 @@ import java.io.IOException;
*/
public final class DataSourceException extends IOException {
/**
* Returns whether the given {@link IOException} was caused by a {@link DataSourceException} whose
* {@link #reason} is {@link #POSITION_OUT_OF_RANGE} in its cause stack.
*/
public static boolean isCausedByPositionOutOfRange(IOException e) {
@Nullable Throwable cause = e;
while (cause != null) {
if (cause instanceof DataSourceException) {
int reason = ((DataSourceException) cause).reason;
if (reason == DataSourceException.POSITION_OUT_OF_RANGE) {
return true;
}
}
cause = cause.getCause();
}
return false;
}
public static final int POSITION_OUT_OF_RANGE = 0;
/**
......
/*
* Copyright 2020 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 static com.google.common.truth.Truth.assertThat;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import java.io.IOException;
import org.junit.Test;
import org.junit.runner.RunWith;
/** Unit tests for {@link DataSourceException}. */
@RunWith(AndroidJUnit4.class)
public class DataSourceExceptionTest {
private static final int REASON_OTHER = DataSourceException.POSITION_OUT_OF_RANGE - 1;
@Test
public void isCausedByPositionOutOfRange_reasonIsPositionOutOfRange_returnsTrue() {
DataSourceException e = new DataSourceException(DataSourceException.POSITION_OUT_OF_RANGE);
assertThat(DataSourceException.isCausedByPositionOutOfRange(e)).isTrue();
}
@Test
public void isCausedByPositionOutOfRange_reasonIsOther_returnsFalse() {
DataSourceException e = new DataSourceException(REASON_OTHER);
assertThat(DataSourceException.isCausedByPositionOutOfRange(e)).isFalse();
}
@Test
public void isCausedByPositionOutOfRange_indirectauseReasonIsPositionOutOfRange_returnsTrue() {
DataSourceException cause = new DataSourceException(DataSourceException.POSITION_OUT_OF_RANGE);
IOException e = new IOException(new IOException(cause));
assertThat(DataSourceException.isCausedByPositionOutOfRange(e)).isTrue();
}
@Test
public void isCausedByPositionOutOfRange_causeReasonIsOther_returnsFalse() {
DataSourceException cause = new DataSourceException(REASON_OTHER);
IOException e = new IOException(new IOException(cause));
assertThat(DataSourceException.isCausedByPositionOutOfRange(e)).isFalse();
}
}
......@@ -630,7 +630,7 @@ public final class CacheDataSource implements DataSource {
}
return bytesRead;
} catch (IOException e) {
if (currentDataSpecLengthUnset && CacheUtil.isCausedByPositionOutOfRange(e)) {
if (currentDataSpecLengthUnset && DataSourceException.isCausedByPositionOutOfRange(e)) {
setNoBytesRemainingAndMaybeStoreLength();
return C.RESULT_END_OF_INPUT;
}
......
......@@ -273,7 +273,7 @@ public final class CacheUtil {
dataSource.open(dataSpec.subrange(positionOffset, endOffset - positionOffset));
isDataSourceOpen = true;
} catch (IOException exception) {
if (!isLastBlock || !isCausedByPositionOutOfRange(exception)) {
if (!isLastBlock || !DataSourceException.isCausedByPositionOutOfRange(exception)) {
throw exception;
}
Util.closeQuietly(dataSource);
......@@ -349,20 +349,6 @@ public final class CacheUtil {
}
}
/* package */ static boolean isCausedByPositionOutOfRange(IOException e) {
@Nullable Throwable cause = e;
while (cause != null) {
if (cause instanceof DataSourceException) {
int reason = ((DataSourceException) cause).reason;
if (reason == DataSourceException.POSITION_OUT_OF_RANGE) {
return true;
}
}
cause = cause.getCause();
}
return false;
}
private static String buildCacheKey(
DataSpec dataSpec, @Nullable CacheKeyFactory cacheKeyFactory) {
return (cacheKeyFactory != null ? cacheKeyFactory : CacheKeyFactory.DEFAULT)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment