Move DataSource utils into a DataSourceUtil class

PiperOrigin-RevId: 403910535
This commit is contained in:
olly 2021-10-18 11:31:06 +01:00 committed by Oliver Woodman
parent 8fd1381a84
commit 18cf01cda6
21 changed files with 159 additions and 120 deletions

View File

@ -50,6 +50,7 @@ import com.google.android.exoplayer2.RenderersFactory;
import com.google.android.exoplayer2.offline.DownloadService;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DataSourceInputStream;
import com.google.android.exoplayer2.upstream.DataSourceUtil;
import com.google.android.exoplayer2.upstream.DataSpec;
import com.google.android.exoplayer2.util.Log;
import com.google.android.exoplayer2.util.Util;
@ -284,7 +285,7 @@ public class SampleChooserActivity extends AppCompatActivity
Log.e(TAG, "Error loading sample list: " + uri, e);
sawError = true;
} finally {
Util.closeQuietly(dataSource);
DataSourceUtil.closeQuietly(dataSource);
}
}
return result;

View File

@ -38,6 +38,7 @@ import com.google.ads.interactivemedia.v3.api.player.VideoProgressUpdate;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.ui.AdOverlayInfo;
import com.google.android.exoplayer2.upstream.DataSchemeDataSource;
import com.google.android.exoplayer2.upstream.DataSourceUtil;
import com.google.android.exoplayer2.upstream.DataSpec;
import com.google.android.exoplayer2.util.Util;
import java.io.IOException;
@ -190,7 +191,7 @@ import java.util.Set;
DataSchemeDataSource dataSchemeDataSource = new DataSchemeDataSource();
try {
dataSchemeDataSource.open(adTagDataSpec);
request.setAdsResponse(Util.fromUtf8Bytes(Util.readToEnd(dataSchemeDataSource)));
request.setAdsResponse(Util.fromUtf8Bytes(DataSourceUtil.readToEnd(dataSchemeDataSource)));
} finally {
dataSchemeDataSource.close();
}

View File

@ -0,0 +1,90 @@
/*
* Copyright (C) 2021 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 androidx.annotation.Nullable;
import com.google.android.exoplayer2.C;
import java.io.IOException;
import java.util.Arrays;
/** Utility methods for {@link DataSource}. */
public final class DataSourceUtil {
private DataSourceUtil() {}
/**
* Reads data from the specified opened {@link DataSource} until it ends, and returns a byte array
* containing the read data.
*
* @param dataSource The source from which to read.
* @return The concatenation of all read data.
* @throws IOException If an error occurs reading from the source.
*/
public static byte[] readToEnd(DataSource dataSource) throws IOException {
byte[] data = new byte[1024];
int position = 0;
int bytesRead = 0;
while (bytesRead != C.RESULT_END_OF_INPUT) {
if (position == data.length) {
data = Arrays.copyOf(data, data.length * 2);
}
bytesRead = dataSource.read(data, position, data.length - position);
if (bytesRead != C.RESULT_END_OF_INPUT) {
position += bytesRead;
}
}
return Arrays.copyOf(data, position);
}
/**
* Reads {@code length} bytes from the specified opened {@link DataSource}, and returns a byte
* array containing the read data.
*
* @param dataSource The source from which to read.
* @return The read data.
* @throws IOException If an error occurs reading from the source.
* @throws IllegalStateException If the end of the source was reached before {@code length} bytes
* could be read.
*/
public static byte[] readExactly(DataSource dataSource, int length) throws IOException {
byte[] data = new byte[length];
int position = 0;
while (position < length) {
int bytesRead = dataSource.read(data, position, data.length - position);
if (bytesRead == C.RESULT_END_OF_INPUT) {
throw new IllegalStateException(
"Not enough data could be read: " + position + " < " + length);
}
position += bytesRead;
}
return data;
}
/**
* Closes a {@link DataSource}, suppressing any {@link IOException} that may occur.
*
* @param dataSource The {@link DataSource} to close.
*/
public static void closeQuietly(@Nullable DataSource dataSource) {
try {
if (dataSource != null) {
dataSource.close();
}
} catch (IOException e) {
// Ignore.
}
}
}

View File

@ -63,7 +63,6 @@ import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.ParserException;
import com.google.android.exoplayer2.PlaybackException;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.common.base.Ascii;
import com.google.common.base.Charsets;
import java.io.ByteArrayOutputStream;
@ -539,69 +538,6 @@ public final class Util {
return Executors.newSingleThreadExecutor(runnable -> new Thread(runnable, threadName));
}
/**
* Reads data from the specified opened {@link DataSource} until it ends, and returns a byte array
* containing the read data.
*
* @param dataSource The source from which to read.
* @return The concatenation of all read data.
* @throws IOException If an error occurs reading from the source.
*/
public static byte[] readToEnd(DataSource dataSource) throws IOException {
byte[] data = new byte[1024];
int position = 0;
int bytesRead = 0;
while (bytesRead != C.RESULT_END_OF_INPUT) {
if (position == data.length) {
data = Arrays.copyOf(data, data.length * 2);
}
bytesRead = dataSource.read(data, position, data.length - position);
if (bytesRead != C.RESULT_END_OF_INPUT) {
position += bytesRead;
}
}
return Arrays.copyOf(data, position);
}
/**
* Reads {@code length} bytes from the specified opened {@link DataSource}, and returns a byte
* array containing the read data.
*
* @param dataSource The source from which to read.
* @return The read data.
* @throws IOException If an error occurs reading from the source.
* @throws IllegalStateException If the end of the source was reached before {@code length} bytes
* could be read.
*/
public static byte[] readExactly(DataSource dataSource, int length) throws IOException {
byte[] data = new byte[length];
int position = 0;
while (position < length) {
int bytesRead = dataSource.read(data, position, data.length - position);
if (bytesRead == C.RESULT_END_OF_INPUT) {
throw new IllegalStateException(
"Not enough data could be read: " + position + " < " + length);
}
position += bytesRead;
}
return data;
}
/**
* Closes a {@link DataSource}, suppressing any {@link IOException} that may occur.
*
* @param dataSource The {@link DataSource} to close.
*/
public static void closeQuietly(@Nullable DataSource dataSource) {
try {
if (dataSource != null) {
dataSource.close();
}
} catch (IOException e) {
// Ignore.
}
}
/**
* Closes a {@link Closeable}, suppressing any {@link IOException} that may occur. Both {@link
* java.io.OutputStream} and {@link InputStream} are {@code Closeable}.

View File

@ -139,7 +139,7 @@ public final class DataSchemeDataSourceTest {
String data = "Some Data!<>:\"/\\|?*%";
schemeDataDataSource.open(new DataSpec(Util.getDataUriForString("text/plain", data)));
assertThat(Util.fromUtf8Bytes(Util.readToEnd(schemeDataDataSource))).isEqualTo(data);
assertThat(Util.fromUtf8Bytes(DataSourceUtil.readToEnd(schemeDataDataSource))).isEqualTo(data);
}
private static DataSpec buildDataSpec(String uriString) {
@ -163,7 +163,7 @@ public final class DataSchemeDataSourceTest {
try {
long length = dataSource.open(dataSpec);
assertThat(length).isEqualTo(expectedData.length);
byte[] readData = Util.readToEnd(dataSource);
byte[] readData = DataSourceUtil.readToEnd(dataSource);
assertThat(readData).isEqualTo(expectedData);
} finally {
dataSource.close();

View File

@ -44,6 +44,7 @@ import com.google.android.exoplayer2.source.SampleStream.ReadFlags;
import com.google.android.exoplayer2.trackselection.ExoTrackSelection;
import com.google.android.exoplayer2.upstream.Allocator;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DataSourceUtil;
import com.google.android.exoplayer2.upstream.DataSpec;
import com.google.android.exoplayer2.upstream.LoadErrorHandlingPolicy;
import com.google.android.exoplayer2.upstream.LoadErrorHandlingPolicy.LoadErrorInfo;
@ -1056,7 +1057,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
} else if (progressiveMediaExtractor.getCurrentInputPosition() != C.POSITION_UNSET) {
positionHolder.position = progressiveMediaExtractor.getCurrentInputPosition();
}
Util.closeQuietly(dataSource);
DataSourceUtil.closeQuietly(dataSource);
}
}
}

View File

@ -24,6 +24,7 @@ import com.google.android.exoplayer2.decoder.DecoderInputBuffer;
import com.google.android.exoplayer2.source.MediaSourceEventListener.EventDispatcher;
import com.google.android.exoplayer2.trackselection.ExoTrackSelection;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DataSourceUtil;
import com.google.android.exoplayer2.upstream.DataSpec;
import com.google.android.exoplayer2.upstream.LoadErrorHandlingPolicy;
import com.google.android.exoplayer2.upstream.LoadErrorHandlingPolicy.LoadErrorInfo;
@ -35,7 +36,6 @@ import com.google.android.exoplayer2.upstream.TransferListener;
import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.Log;
import com.google.android.exoplayer2.util.MimeTypes;
import com.google.android.exoplayer2.util.Util;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
@ -445,7 +445,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
result = dataSource.read(sampleData, sampleSize, sampleData.length - sampleSize);
}
} finally {
Util.closeQuietly(dataSource);
DataSourceUtil.closeQuietly(dataSource);
}
}
}

View File

@ -23,8 +23,8 @@ import com.google.android.exoplayer2.extractor.Extractor;
import com.google.android.exoplayer2.extractor.ExtractorInput;
import com.google.android.exoplayer2.source.chunk.ChunkExtractor.TrackOutputProvider;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DataSourceUtil;
import com.google.android.exoplayer2.upstream.DataSpec;
import com.google.android.exoplayer2.util.Util;
import java.io.IOException;
/** A {@link BaseMediaChunk} that uses an {@link Extractor} to decode sample data. */
@ -129,7 +129,7 @@ public class ContainerMediaChunk extends BaseMediaChunk {
nextLoadPosition = input.getPosition() - dataSpec.position;
}
} finally {
Util.closeQuietly(dataSource);
DataSourceUtil.closeQuietly(dataSource);
}
loadCompleted = !loadCanceled;
}

View File

@ -20,6 +20,7 @@ import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.C.DataType;
import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DataSourceUtil;
import com.google.android.exoplayer2.upstream.DataSpec;
import com.google.android.exoplayer2.util.Util;
import java.io.IOException;
@ -101,7 +102,7 @@ public abstract class DataChunk extends Chunk {
consume(data, limit);
}
} finally {
Util.closeQuietly(dataSource);
DataSourceUtil.closeQuietly(dataSource);
}
}

View File

@ -23,8 +23,8 @@ import com.google.android.exoplayer2.extractor.Extractor;
import com.google.android.exoplayer2.extractor.ExtractorInput;
import com.google.android.exoplayer2.source.chunk.ChunkExtractor.TrackOutputProvider;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DataSourceUtil;
import com.google.android.exoplayer2.upstream.DataSpec;
import com.google.android.exoplayer2.util.Util;
import java.io.IOException;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
@ -104,7 +104,7 @@ public final class InitializationChunk extends Chunk {
nextLoadPosition = input.getPosition() - dataSpec.position;
}
} finally {
Util.closeQuietly(dataSource);
DataSourceUtil.closeQuietly(dataSource);
}
}
}

View File

@ -22,8 +22,8 @@ import com.google.android.exoplayer2.extractor.DefaultExtractorInput;
import com.google.android.exoplayer2.extractor.ExtractorInput;
import com.google.android.exoplayer2.extractor.TrackOutput;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DataSourceUtil;
import com.google.android.exoplayer2.upstream.DataSpec;
import com.google.android.exoplayer2.util.Util;
import java.io.IOException;
/** A {@link BaseMediaChunk} for chunks consisting of a single raw sample. */
@ -110,7 +110,7 @@ public final class SingleSampleMediaChunk extends BaseMediaChunk {
int sampleSize = (int) nextLoadPosition;
trackOutput.sampleMetadata(startTimeUs, C.BUFFER_FLAG_KEY_FRAME, sampleSize, 0, null);
} finally {
Util.closeQuietly(dataSource);
DataSourceUtil.closeQuietly(dataSource);
}
loadCompleted = true;
}

View File

@ -18,10 +18,10 @@ package com.google.android.exoplayer2.upstream.cache;
import androidx.annotation.Nullable;
import androidx.annotation.WorkerThread;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.upstream.DataSourceUtil;
import com.google.android.exoplayer2.upstream.DataSpec;
import com.google.android.exoplayer2.util.PriorityTaskManager;
import com.google.android.exoplayer2.util.PriorityTaskManager.PriorityTooLowException;
import com.google.android.exoplayer2.util.Util;
import java.io.IOException;
import java.io.InterruptedIOException;
@ -158,7 +158,7 @@ public final class CacheWriter {
resolvedLength = dataSource.open(boundedDataSpec);
isDataSourceOpen = true;
} catch (IOException e) {
Util.closeQuietly(dataSource);
DataSourceUtil.closeQuietly(dataSource);
}
}
@ -171,7 +171,7 @@ public final class CacheWriter {
try {
resolvedLength = dataSource.open(unboundedDataSpec);
} catch (IOException e) {
Util.closeQuietly(dataSource);
DataSourceUtil.closeQuietly(dataSource);
throw e;
}
}
@ -194,7 +194,7 @@ public final class CacheWriter {
onRequestEndPosition(position + totalBytesRead);
}
} catch (IOException e) {
Util.closeQuietly(dataSource);
DataSourceUtil.closeQuietly(dataSource);
throw e;
}

View File

@ -29,6 +29,7 @@ import com.google.android.exoplayer2.testutil.CacheAsserts;
import com.google.android.exoplayer2.testutil.FakeDataSet.FakeData;
import com.google.android.exoplayer2.testutil.FakeDataSource;
import com.google.android.exoplayer2.testutil.TestUtil;
import com.google.android.exoplayer2.upstream.DataSourceUtil;
import com.google.android.exoplayer2.upstream.DataSpec;
import com.google.android.exoplayer2.upstream.FileDataSource;
import com.google.android.exoplayer2.util.Util;
@ -325,7 +326,7 @@ public final class CacheDataSourceTest {
CacheDataSource cacheDataSource = new CacheDataSource(cache, upstream, 0);
cacheDataSource.open(unboundedDataSpec);
Util.readToEnd(cacheDataSource);
DataSourceUtil.readToEnd(cacheDataSource);
cacheDataSource.close();
assertThat(upstream.getAndClearOpenedDataSpecs()).hasLength(1);
@ -342,7 +343,7 @@ public final class CacheDataSourceTest {
cache, upstream, CacheDataSource.FLAG_IGNORE_CACHE_FOR_UNSET_LENGTH_REQUESTS);
cacheDataSource.open(unboundedDataSpec);
Util.readToEnd(cacheDataSource);
DataSourceUtil.readToEnd(cacheDataSource);
cacheDataSource.close();
assertThat(cache.getKeys()).isEmpty();
@ -391,7 +392,7 @@ public final class CacheDataSourceTest {
cacheWriter.cache();
// Read the rest of the data.
Util.readToEnd(cacheDataSource);
DataSourceUtil.readToEnd(cacheDataSource);
cacheDataSource.close();
}
@ -440,7 +441,7 @@ public final class CacheDataSourceTest {
cacheWriter.cache();
// Read the rest of the data.
Util.readToEnd(cacheDataSource);
DataSourceUtil.readToEnd(cacheDataSource);
cacheDataSource.close();
}
@ -469,14 +470,14 @@ public final class CacheDataSourceTest {
// Open source and read some data from upstream as the data hasn't cached yet.
cacheDataSource.open(unboundedDataSpec);
Util.readExactly(cacheDataSource, 100);
DataSourceUtil.readExactly(cacheDataSource, 100);
// Delete cached data.
cache.removeResource(cacheDataSource.getCacheKeyFactory().buildCacheKey(unboundedDataSpec));
assertCacheEmpty(cache);
// Read the rest of the data.
Util.readToEnd(cacheDataSource);
DataSourceUtil.readToEnd(cacheDataSource);
cacheDataSource.close();
}
@ -506,7 +507,7 @@ public final class CacheDataSourceTest {
cacheDataSource.open(unboundedDataSpec);
// Read the first half from upstream as it hasn't cached yet.
Util.readExactly(cacheDataSource, halfDataLength);
DataSourceUtil.readExactly(cacheDataSource, halfDataLength);
// Delete the cached latter half.
NavigableSet<CacheSpan> cachedSpans = cache.getCachedSpans(defaultCacheKey);
@ -517,7 +518,7 @@ public final class CacheDataSourceTest {
}
// Read the rest of the data.
Util.readToEnd(cacheDataSource);
DataSourceUtil.readToEnd(cacheDataSource);
cacheDataSource.close();
}

View File

@ -31,9 +31,9 @@ import com.google.android.exoplayer2.testutil.FakeExtractorInput;
import com.google.android.exoplayer2.testutil.FakeExtractorOutput;
import com.google.android.exoplayer2.testutil.FakeTrackOutput;
import com.google.android.exoplayer2.testutil.TestUtil;
import com.google.android.exoplayer2.upstream.DataSourceUtil;
import com.google.android.exoplayer2.upstream.DataSpec;
import com.google.android.exoplayer2.upstream.DefaultDataSource;
import com.google.android.exoplayer2.util.Util;
import java.io.IOException;
import java.util.Arrays;
import java.util.Random;
@ -198,7 +198,7 @@ public final class PsExtractorSeekTest {
private long readInputLength() throws IOException {
DataSpec dataSpec = new DataSpec(Uri.parse("asset:///" + PS_FILE_PATH));
long totalInputLength = dataSource.open(dataSpec);
Util.closeQuietly(dataSource);
DataSourceUtil.closeQuietly(dataSource);
return totalInputLength;
}
@ -229,7 +229,7 @@ public final class PsExtractorSeekTest {
extractorReadResult = psExtractor.read(extractorInput, positionHolder);
}
} finally {
Util.closeQuietly(dataSource);
DataSourceUtil.closeQuietly(dataSource);
}
if (extractorReadResult == Extractor.RESULT_SEEK) {
@ -257,7 +257,7 @@ public final class PsExtractorSeekTest {
readResult = extractor.read(input, positionHolder);
}
} finally {
Util.closeQuietly(dataSource);
DataSourceUtil.closeQuietly(dataSource);
}
if (readResult == Extractor.RESULT_SEEK) {
@ -283,7 +283,7 @@ public final class PsExtractorSeekTest {
readResult = extractor.read(input, positionHolder);
}
} finally {
Util.closeQuietly(dataSource);
DataSourceUtil.closeQuietly(dataSource);
}
if (readResult == Extractor.RESULT_SEEK) {
input = getExtractorInputFromPosition(positionHolder.position);

View File

@ -27,8 +27,8 @@ import com.google.android.exoplayer2.extractor.SeekMap;
import com.google.android.exoplayer2.testutil.FakeExtractorOutput;
import com.google.android.exoplayer2.testutil.FakeTrackOutput;
import com.google.android.exoplayer2.testutil.TestUtil;
import com.google.android.exoplayer2.upstream.DataSourceUtil;
import com.google.android.exoplayer2.upstream.DefaultDataSource;
import com.google.android.exoplayer2.util.Util;
import java.io.IOException;
import java.util.Arrays;
import java.util.Random;
@ -221,7 +221,7 @@ public final class TsExtractorSeekTest {
readResult = extractor.read(input, positionHolder);
}
} finally {
Util.closeQuietly(dataSource);
DataSourceUtil.closeQuietly(dataSource);
}
if (readResult == Extractor.RESULT_SEEK) {
input =

View File

@ -30,12 +30,12 @@ import com.google.android.exoplayer2.metadata.id3.PrivFrame;
import com.google.android.exoplayer2.source.chunk.MediaChunk;
import com.google.android.exoplayer2.source.hls.playlist.HlsMediaPlaylist;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DataSourceUtil;
import com.google.android.exoplayer2.upstream.DataSpec;
import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.ParsableByteArray;
import com.google.android.exoplayer2.util.TimestampAdjuster;
import com.google.android.exoplayer2.util.UriUtil;
import com.google.android.exoplayer2.util.Util;
import com.google.common.base.Ascii;
import com.google.common.collect.ImmutableList;
import java.io.EOFException;
@ -472,7 +472,7 @@ import org.checkerframework.checker.nullness.qual.RequiresNonNull;
nextLoadPosition = (int) (input.getPosition() - dataSpec.position);
}
} finally {
Util.closeQuietly(dataSource);
DataSourceUtil.closeQuietly(dataSource);
}
}

View File

@ -26,6 +26,7 @@ import com.google.android.exoplayer2.extractor.Extractor;
import com.google.android.exoplayer2.extractor.ExtractorInput;
import com.google.android.exoplayer2.extractor.ExtractorOutput;
import com.google.android.exoplayer2.extractor.PositionHolder;
import com.google.android.exoplayer2.upstream.DataSourceUtil;
import com.google.android.exoplayer2.upstream.Loader;
import com.google.android.exoplayer2.util.Util;
import java.io.IOException;
@ -162,7 +163,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
}
}
} finally {
Util.closeQuietly(dataChannel);
DataSourceUtil.closeQuietly(dataChannel);
}
}

View File

@ -15,7 +15,7 @@
*/
package com.google.android.exoplayer2.source.rtsp;
import com.google.android.exoplayer2.util.Util;
import com.google.android.exoplayer2.upstream.DataSourceUtil;
import java.io.IOException;
/** Factory for {@link UdpDataSourceRtpDataChannel}. */
@ -60,8 +60,8 @@ import java.io.IOException;
return secondChannel;
}
} catch (IOException e) {
Util.closeQuietly(firstChannel);
Util.closeQuietly(secondChannel);
DataSourceUtil.closeQuietly(firstChannel);
DataSourceUtil.closeQuietly(secondChannel);
throw e;
}
}

View File

@ -22,6 +22,7 @@ import android.net.Uri;
import com.google.android.exoplayer2.testutil.FakeDataSet.FakeData;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DataSourceInputStream;
import com.google.android.exoplayer2.upstream.DataSourceUtil;
import com.google.android.exoplayer2.upstream.DataSpec;
import com.google.android.exoplayer2.upstream.DummyDataSource;
import com.google.android.exoplayer2.upstream.cache.Cache;
@ -130,7 +131,7 @@ public final class CacheAsserts {
byte[] bytes;
try {
dataSource.open(dataSpec);
bytes = Util.readToEnd(dataSource);
bytes = DataSourceUtil.readToEnd(dataSource);
} catch (IOException e) {
throw new IOException("Opening/reading cache failed: " + dataSpec, e);
} finally {

View File

@ -34,6 +34,7 @@ import androidx.annotation.RequiresApi;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DataSourceException;
import com.google.android.exoplayer2.upstream.DataSourceUtil;
import com.google.android.exoplayer2.upstream.DataSpec;
import com.google.android.exoplayer2.upstream.TransferListener;
import com.google.android.exoplayer2.util.Assertions;
@ -119,8 +120,8 @@ public abstract class DataSourceContractTest {
long length = dataSource.open(new DataSpec(resource.getUri()));
byte[] data =
unboundedReadsAreIndefinite()
? Util.readExactly(dataSource, resource.getExpectedBytes().length)
: Util.readToEnd(dataSource);
? DataSourceUtil.readExactly(dataSource, resource.getExpectedBytes().length)
: DataSourceUtil.readToEnd(dataSource);
if (length != C.LENGTH_UNSET) {
assertThat(length).isEqualTo(resource.getExpectedBytes().length);
@ -148,8 +149,8 @@ public abstract class DataSourceContractTest {
new DataSpec.Builder().setUri(resource.getUri()).setPosition(3).build());
byte[] data =
unboundedReadsAreIndefinite()
? Util.readExactly(dataSource, resource.getExpectedBytes().length - 3)
: Util.readToEnd(dataSource);
? DataSourceUtil.readExactly(dataSource, resource.getExpectedBytes().length - 3)
: DataSourceUtil.readToEnd(dataSource);
if (length != C.LENGTH_UNSET) {
assertThat(length).isEqualTo(resource.getExpectedBytes().length - 3);
@ -176,7 +177,7 @@ public abstract class DataSourceContractTest {
try {
long length =
dataSource.open(new DataSpec.Builder().setUri(resource.getUri()).setLength(4).build());
byte[] data = Util.readToEnd(dataSource);
byte[] data = DataSourceUtil.readToEnd(dataSource);
assertThat(length).isEqualTo(4);
byte[] expectedData = Arrays.copyOf(resource.getExpectedBytes(), 4);
@ -205,7 +206,7 @@ public abstract class DataSourceContractTest {
.setPosition(2)
.setLength(2)
.build());
byte[] data = Util.readToEnd(dataSource);
byte[] data = DataSourceUtil.readToEnd(dataSource);
assertThat(length).isEqualTo(2);
byte[] expectedData = Arrays.copyOfRange(resource.getExpectedBytes(), 2, 4);
@ -232,7 +233,9 @@ public abstract class DataSourceContractTest {
try {
long length = dataSource.open(dataSpec);
byte[] data =
unboundedReadsAreIndefinite() ? Util.EMPTY_BYTE_ARRAY : Util.readToEnd(dataSource);
unboundedReadsAreIndefinite()
? Util.EMPTY_BYTE_ARRAY
: DataSourceUtil.readToEnd(dataSource);
// The DataSource.open() contract requires the returned length to equal the length in the
// DataSpec if set. This is true even though the DataSource implementation may know that
@ -267,7 +270,9 @@ public abstract class DataSourceContractTest {
try {
long length = dataSource.open(dataSpec);
byte[] data =
unboundedReadsAreIndefinite() ? Util.EMPTY_BYTE_ARRAY : Util.readToEnd(dataSource);
unboundedReadsAreIndefinite()
? Util.EMPTY_BYTE_ARRAY
: DataSourceUtil.readToEnd(dataSource);
// The DataSource.open() contract requires the returned length to equal the length in the
// DataSpec if set. This is true even though the DataSource implementation may know that
@ -321,7 +326,7 @@ public abstract class DataSourceContractTest {
.build();
try {
long length = dataSource.open(dataSpec);
byte[] data = Util.readExactly(dataSource, /* length= */ 1);
byte[] data = DataSourceUtil.readExactly(dataSource, /* length= */ 1);
// TODO: Decide what the allowed behavior should be for the next read, and assert it.
// The DataSource.open() contract requires the returned length to equal the length in the
@ -361,8 +366,8 @@ public abstract class DataSourceContractTest {
.build());
byte[] data =
unboundedReadsAreIndefinite()
? Util.readExactly(dataSource, resource.getExpectedBytes().length)
: Util.readToEnd(dataSource);
? DataSourceUtil.readExactly(dataSource, resource.getExpectedBytes().length)
: DataSourceUtil.readToEnd(dataSource);
if (length != C.LENGTH_UNSET) {
assertThat(length).isEqualTo(resource.getExpectedBytes().length);
@ -423,9 +428,9 @@ public abstract class DataSourceContractTest {
inOrder.verifyNoMoreInteractions();
if (unboundedReadsAreIndefinite()) {
Util.readExactly(dataSource, resource.getExpectedBytes().length);
DataSourceUtil.readExactly(dataSource, resource.getExpectedBytes().length);
} else {
Util.readToEnd(dataSource);
DataSourceUtil.readToEnd(dataSource);
}
// Verify sufficient onBytesTransferred() callbacks have been triggered before closing the
// DataSource.

View File

@ -36,6 +36,7 @@ import com.google.android.exoplayer2.extractor.PositionHolder;
import com.google.android.exoplayer2.extractor.SeekMap;
import com.google.android.exoplayer2.metadata.MetadataInputBuffer;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DataSourceUtil;
import com.google.android.exoplayer2.upstream.DataSpec;
import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.Util;
@ -218,7 +219,7 @@ public class TestUtil {
try {
long length = dataSource.open(dataSpec);
assertThat(length).isEqualTo(expectKnownLength ? expectedData.length : C.LENGTH_UNSET);
byte[] readData = Util.readToEnd(dataSource);
byte[] readData = DataSourceUtil.readToEnd(dataSource);
assertThat(readData).isEqualTo(expectedData);
} finally {
dataSource.close();
@ -325,7 +326,7 @@ public class TestUtil {
}
}
} finally {
Util.closeQuietly(dataSource);
DataSourceUtil.closeQuietly(dataSource);
}
if (readResult == Extractor.RESULT_SEEK) {
@ -413,7 +414,7 @@ public class TestUtil {
extractorReadResult = extractor.read(extractorInput, positionHolder);
}
} finally {
Util.closeQuietly(dataSource);
DataSourceUtil.closeQuietly(dataSource);
}
if (extractorReadResult == Extractor.RESULT_SEEK) {