From 18cf01cda62cbb81a1a6f52c7f77afc1a5395b47 Mon Sep 17 00:00:00 2001 From: olly Date: Mon, 18 Oct 2021 11:31:06 +0100 Subject: [PATCH] Move DataSource utils into a DataSourceUtil class PiperOrigin-RevId: 403910535 --- .../demo/SampleChooserActivity.java | 3 +- .../android/exoplayer2/ext/ima/ImaUtil.java | 3 +- .../exoplayer2/upstream/DataSourceUtil.java | 90 +++++++++++++++++++ .../google/android/exoplayer2/util/Util.java | 64 ------------- .../upstream/DataSchemeDataSourceTest.java | 4 +- .../source/ProgressiveMediaPeriod.java | 3 +- .../source/SingleSampleMediaPeriod.java | 4 +- .../source/chunk/ContainerMediaChunk.java | 4 +- .../exoplayer2/source/chunk/DataChunk.java | 3 +- .../source/chunk/InitializationChunk.java | 4 +- .../source/chunk/SingleSampleMediaChunk.java | 4 +- .../upstream/cache/CacheWriter.java | 8 +- .../upstream/cache/CacheDataSourceTest.java | 17 ++-- .../extractor/ts/PsExtractorSeekTest.java | 10 +-- .../extractor/ts/TsExtractorSeekTest.java | 4 +- .../exoplayer2/source/hls/HlsMediaChunk.java | 4 +- .../source/rtsp/RtpDataLoadable.java | 3 +- .../UdpDataSourceRtpDataChannelFactory.java | 6 +- .../exoplayer2/testutil/CacheAsserts.java | 3 +- .../testutil/DataSourceContractTest.java | 31 ++++--- .../android/exoplayer2/testutil/TestUtil.java | 7 +- 21 files changed, 159 insertions(+), 120 deletions(-) create mode 100644 library/common/src/main/java/com/google/android/exoplayer2/upstream/DataSourceUtil.java diff --git a/demos/main/src/main/java/com/google/android/exoplayer2/demo/SampleChooserActivity.java b/demos/main/src/main/java/com/google/android/exoplayer2/demo/SampleChooserActivity.java index 1a00670a39..ec5d82ac1a 100644 --- a/demos/main/src/main/java/com/google/android/exoplayer2/demo/SampleChooserActivity.java +++ b/demos/main/src/main/java/com/google/android/exoplayer2/demo/SampleChooserActivity.java @@ -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; diff --git a/extensions/ima/src/main/java/com/google/android/exoplayer2/ext/ima/ImaUtil.java b/extensions/ima/src/main/java/com/google/android/exoplayer2/ext/ima/ImaUtil.java index 377a9c4db8..13952abd72 100644 --- a/extensions/ima/src/main/java/com/google/android/exoplayer2/ext/ima/ImaUtil.java +++ b/extensions/ima/src/main/java/com/google/android/exoplayer2/ext/ima/ImaUtil.java @@ -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(); } diff --git a/library/common/src/main/java/com/google/android/exoplayer2/upstream/DataSourceUtil.java b/library/common/src/main/java/com/google/android/exoplayer2/upstream/DataSourceUtil.java new file mode 100644 index 0000000000..b0a31d9b7b --- /dev/null +++ b/library/common/src/main/java/com/google/android/exoplayer2/upstream/DataSourceUtil.java @@ -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. + } + } +} diff --git a/library/common/src/main/java/com/google/android/exoplayer2/util/Util.java b/library/common/src/main/java/com/google/android/exoplayer2/util/Util.java index 27a0d93670..9cf935d56f 100644 --- a/library/common/src/main/java/com/google/android/exoplayer2/util/Util.java +++ b/library/common/src/main/java/com/google/android/exoplayer2/util/Util.java @@ -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}. diff --git a/library/common/src/test/java/com/google/android/exoplayer2/upstream/DataSchemeDataSourceTest.java b/library/common/src/test/java/com/google/android/exoplayer2/upstream/DataSchemeDataSourceTest.java index 297ae69e0b..dfdd64f0d1 100644 --- a/library/common/src/test/java/com/google/android/exoplayer2/upstream/DataSchemeDataSourceTest.java +++ b/library/common/src/test/java/com/google/android/exoplayer2/upstream/DataSchemeDataSourceTest.java @@ -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(); diff --git a/library/core/src/main/java/com/google/android/exoplayer2/source/ProgressiveMediaPeriod.java b/library/core/src/main/java/com/google/android/exoplayer2/source/ProgressiveMediaPeriod.java index 654b2212ec..c30476405d 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/source/ProgressiveMediaPeriod.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/source/ProgressiveMediaPeriod.java @@ -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); } } } diff --git a/library/core/src/main/java/com/google/android/exoplayer2/source/SingleSampleMediaPeriod.java b/library/core/src/main/java/com/google/android/exoplayer2/source/SingleSampleMediaPeriod.java index 314946f90a..a018f94de2 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/source/SingleSampleMediaPeriod.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/source/SingleSampleMediaPeriod.java @@ -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); } } } diff --git a/library/core/src/main/java/com/google/android/exoplayer2/source/chunk/ContainerMediaChunk.java b/library/core/src/main/java/com/google/android/exoplayer2/source/chunk/ContainerMediaChunk.java index 776b1803c8..154cccb786 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/source/chunk/ContainerMediaChunk.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/source/chunk/ContainerMediaChunk.java @@ -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; } diff --git a/library/core/src/main/java/com/google/android/exoplayer2/source/chunk/DataChunk.java b/library/core/src/main/java/com/google/android/exoplayer2/source/chunk/DataChunk.java index a9b989c858..7b106c0423 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/source/chunk/DataChunk.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/source/chunk/DataChunk.java @@ -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); } } diff --git a/library/core/src/main/java/com/google/android/exoplayer2/source/chunk/InitializationChunk.java b/library/core/src/main/java/com/google/android/exoplayer2/source/chunk/InitializationChunk.java index 2b4ba05e36..a1c3cfea78 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/source/chunk/InitializationChunk.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/source/chunk/InitializationChunk.java @@ -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); } } } diff --git a/library/core/src/main/java/com/google/android/exoplayer2/source/chunk/SingleSampleMediaChunk.java b/library/core/src/main/java/com/google/android/exoplayer2/source/chunk/SingleSampleMediaChunk.java index 8781577ab8..4fdce6377b 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/source/chunk/SingleSampleMediaChunk.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/source/chunk/SingleSampleMediaChunk.java @@ -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; } diff --git a/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheWriter.java b/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheWriter.java index 7e9b3794b9..5fa6638981 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheWriter.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheWriter.java @@ -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; } diff --git a/library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/CacheDataSourceTest.java b/library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/CacheDataSourceTest.java index eefc4eb8c4..481c50664e 100644 --- a/library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/CacheDataSourceTest.java +++ b/library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/CacheDataSourceTest.java @@ -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 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(); } diff --git a/library/extractor/src/test/java/com/google/android/exoplayer2/extractor/ts/PsExtractorSeekTest.java b/library/extractor/src/test/java/com/google/android/exoplayer2/extractor/ts/PsExtractorSeekTest.java index bd02ae9711..dea5d20ad9 100644 --- a/library/extractor/src/test/java/com/google/android/exoplayer2/extractor/ts/PsExtractorSeekTest.java +++ b/library/extractor/src/test/java/com/google/android/exoplayer2/extractor/ts/PsExtractorSeekTest.java @@ -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); diff --git a/library/extractor/src/test/java/com/google/android/exoplayer2/extractor/ts/TsExtractorSeekTest.java b/library/extractor/src/test/java/com/google/android/exoplayer2/extractor/ts/TsExtractorSeekTest.java index 32143c22ae..b014100577 100644 --- a/library/extractor/src/test/java/com/google/android/exoplayer2/extractor/ts/TsExtractorSeekTest.java +++ b/library/extractor/src/test/java/com/google/android/exoplayer2/extractor/ts/TsExtractorSeekTest.java @@ -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 = diff --git a/library/hls/src/main/java/com/google/android/exoplayer2/source/hls/HlsMediaChunk.java b/library/hls/src/main/java/com/google/android/exoplayer2/source/hls/HlsMediaChunk.java index d074d269b6..31a0a5096e 100644 --- a/library/hls/src/main/java/com/google/android/exoplayer2/source/hls/HlsMediaChunk.java +++ b/library/hls/src/main/java/com/google/android/exoplayer2/source/hls/HlsMediaChunk.java @@ -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); } } diff --git a/library/rtsp/src/main/java/com/google/android/exoplayer2/source/rtsp/RtpDataLoadable.java b/library/rtsp/src/main/java/com/google/android/exoplayer2/source/rtsp/RtpDataLoadable.java index 8f171ef44e..b42a938205 100644 --- a/library/rtsp/src/main/java/com/google/android/exoplayer2/source/rtsp/RtpDataLoadable.java +++ b/library/rtsp/src/main/java/com/google/android/exoplayer2/source/rtsp/RtpDataLoadable.java @@ -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); } } diff --git a/library/rtsp/src/main/java/com/google/android/exoplayer2/source/rtsp/UdpDataSourceRtpDataChannelFactory.java b/library/rtsp/src/main/java/com/google/android/exoplayer2/source/rtsp/UdpDataSourceRtpDataChannelFactory.java index b59753ea12..b4c0b4c5ed 100644 --- a/library/rtsp/src/main/java/com/google/android/exoplayer2/source/rtsp/UdpDataSourceRtpDataChannelFactory.java +++ b/library/rtsp/src/main/java/com/google/android/exoplayer2/source/rtsp/UdpDataSourceRtpDataChannelFactory.java @@ -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; } } diff --git a/testutils/src/main/java/com/google/android/exoplayer2/testutil/CacheAsserts.java b/testutils/src/main/java/com/google/android/exoplayer2/testutil/CacheAsserts.java index 65cc7dc8e7..f0df5f1c0c 100644 --- a/testutils/src/main/java/com/google/android/exoplayer2/testutil/CacheAsserts.java +++ b/testutils/src/main/java/com/google/android/exoplayer2/testutil/CacheAsserts.java @@ -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 { diff --git a/testutils/src/main/java/com/google/android/exoplayer2/testutil/DataSourceContractTest.java b/testutils/src/main/java/com/google/android/exoplayer2/testutil/DataSourceContractTest.java index 12117e121b..b119512840 100644 --- a/testutils/src/main/java/com/google/android/exoplayer2/testutil/DataSourceContractTest.java +++ b/testutils/src/main/java/com/google/android/exoplayer2/testutil/DataSourceContractTest.java @@ -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. diff --git a/testutils/src/main/java/com/google/android/exoplayer2/testutil/TestUtil.java b/testutils/src/main/java/com/google/android/exoplayer2/testutil/TestUtil.java index 462ccc5e6d..6db68eb6c3 100644 --- a/testutils/src/main/java/com/google/android/exoplayer2/testutil/TestUtil.java +++ b/testutils/src/main/java/com/google/android/exoplayer2/testutil/TestUtil.java @@ -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) {