mirror of
https://github.com/samsonjs/media.git
synced 2026-04-27 15:07:40 +00:00
Move DataSource reading methods into Util
This will be used to read ads responses out of data: URLs in a subsequent change. PiperOrigin-RevId: 334778780
This commit is contained in:
parent
907e2e0515
commit
3e8dacc284
5 changed files with 59 additions and 60 deletions
|
|
@ -534,6 +534,54 @@ public final class Util {
|
||||||
return Executors.newSingleThreadExecutor(runnable -> new Thread(runnable, threadName));
|
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.
|
* Closes a {@link DataSource}, suppressing any {@link IOException} that may occur.
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,6 @@ import static org.junit.Assert.fail;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||||
import com.google.android.exoplayer2.C;
|
import com.google.android.exoplayer2.C;
|
||||||
import com.google.android.exoplayer2.testutil.TestUtil;
|
|
||||||
import com.google.android.exoplayer2.util.Util;
|
import com.google.android.exoplayer2.util.Util;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
|
|
@ -167,7 +166,7 @@ public final class DataSchemeDataSourceTest {
|
||||||
try {
|
try {
|
||||||
long length = dataSource.open(dataSpec);
|
long length = dataSource.open(dataSpec);
|
||||||
assertThat(length).isEqualTo(expectedData.length);
|
assertThat(length).isEqualTo(expectedData.length);
|
||||||
byte[] readData = TestUtil.readToEnd(dataSource);
|
byte[] readData = Util.readToEnd(dataSource);
|
||||||
assertThat(readData).isEqualTo(expectedData);
|
assertThat(readData).isEqualTo(expectedData);
|
||||||
} finally {
|
} finally {
|
||||||
dataSource.close();
|
dataSource.close();
|
||||||
|
|
|
||||||
|
|
@ -302,7 +302,7 @@ public final class CacheDataSourceTest {
|
||||||
CacheDataSource cacheDataSource = new CacheDataSource(cache, upstream, 0);
|
CacheDataSource cacheDataSource = new CacheDataSource(cache, upstream, 0);
|
||||||
|
|
||||||
cacheDataSource.open(unboundedDataSpec);
|
cacheDataSource.open(unboundedDataSpec);
|
||||||
TestUtil.readToEnd(cacheDataSource);
|
Util.readToEnd(cacheDataSource);
|
||||||
cacheDataSource.close();
|
cacheDataSource.close();
|
||||||
|
|
||||||
assertThat(upstream.getAndClearOpenedDataSpecs()).hasLength(1);
|
assertThat(upstream.getAndClearOpenedDataSpecs()).hasLength(1);
|
||||||
|
|
@ -319,7 +319,7 @@ public final class CacheDataSourceTest {
|
||||||
cache, upstream, CacheDataSource.FLAG_IGNORE_CACHE_FOR_UNSET_LENGTH_REQUESTS);
|
cache, upstream, CacheDataSource.FLAG_IGNORE_CACHE_FOR_UNSET_LENGTH_REQUESTS);
|
||||||
|
|
||||||
cacheDataSource.open(unboundedDataSpec);
|
cacheDataSource.open(unboundedDataSpec);
|
||||||
TestUtil.readToEnd(cacheDataSource);
|
Util.readToEnd(cacheDataSource);
|
||||||
cacheDataSource.close();
|
cacheDataSource.close();
|
||||||
|
|
||||||
assertThat(cache.getKeys()).isEmpty();
|
assertThat(cache.getKeys()).isEmpty();
|
||||||
|
|
@ -369,7 +369,7 @@ public final class CacheDataSourceTest {
|
||||||
cacheWriter.cache();
|
cacheWriter.cache();
|
||||||
|
|
||||||
// Read the rest of the data.
|
// Read the rest of the data.
|
||||||
TestUtil.readToEnd(cacheDataSource);
|
Util.readToEnd(cacheDataSource);
|
||||||
cacheDataSource.close();
|
cacheDataSource.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -419,7 +419,7 @@ public final class CacheDataSourceTest {
|
||||||
cacheWriter.cache();
|
cacheWriter.cache();
|
||||||
|
|
||||||
// Read the rest of the data.
|
// Read the rest of the data.
|
||||||
TestUtil.readToEnd(cacheDataSource);
|
Util.readToEnd(cacheDataSource);
|
||||||
cacheDataSource.close();
|
cacheDataSource.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -449,14 +449,14 @@ public final class CacheDataSourceTest {
|
||||||
|
|
||||||
// Open source and read some data from upstream as the data hasn't cached yet.
|
// Open source and read some data from upstream as the data hasn't cached yet.
|
||||||
cacheDataSource.open(unboundedDataSpec);
|
cacheDataSource.open(unboundedDataSpec);
|
||||||
TestUtil.readExactly(cacheDataSource, 100);
|
Util.readExactly(cacheDataSource, 100);
|
||||||
|
|
||||||
// Delete cached data.
|
// Delete cached data.
|
||||||
cache.removeResource(cacheDataSource.getCacheKeyFactory().buildCacheKey(unboundedDataSpec));
|
cache.removeResource(cacheDataSource.getCacheKeyFactory().buildCacheKey(unboundedDataSpec));
|
||||||
assertCacheEmpty(cache);
|
assertCacheEmpty(cache);
|
||||||
|
|
||||||
// Read the rest of the data.
|
// Read the rest of the data.
|
||||||
TestUtil.readToEnd(cacheDataSource);
|
Util.readToEnd(cacheDataSource);
|
||||||
cacheDataSource.close();
|
cacheDataSource.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -487,7 +487,7 @@ public final class CacheDataSourceTest {
|
||||||
cacheDataSource.open(unboundedDataSpec);
|
cacheDataSource.open(unboundedDataSpec);
|
||||||
|
|
||||||
// Read the first half from upstream as it hasn't cached yet.
|
// Read the first half from upstream as it hasn't cached yet.
|
||||||
TestUtil.readExactly(cacheDataSource, halfDataLength);
|
Util.readExactly(cacheDataSource, halfDataLength);
|
||||||
|
|
||||||
// Delete the cached latter half.
|
// Delete the cached latter half.
|
||||||
NavigableSet<CacheSpan> cachedSpans = cache.getCachedSpans(defaultCacheKey);
|
NavigableSet<CacheSpan> cachedSpans = cache.getCachedSpans(defaultCacheKey);
|
||||||
|
|
@ -498,7 +498,7 @@ public final class CacheDataSourceTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read the rest of the data.
|
// Read the rest of the data.
|
||||||
TestUtil.readToEnd(cacheDataSource);
|
Util.readToEnd(cacheDataSource);
|
||||||
cacheDataSource.close();
|
cacheDataSource.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -130,7 +130,7 @@ public final class CacheAsserts {
|
||||||
byte[] bytes;
|
byte[] bytes;
|
||||||
try {
|
try {
|
||||||
dataSource.open(dataSpec);
|
dataSource.open(dataSpec);
|
||||||
bytes = TestUtil.readToEnd(dataSource);
|
bytes = Util.readToEnd(dataSource);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new IOException("Opening/reading cache failed: " + dataSpec, e);
|
throw new IOException("Opening/reading cache failed: " + dataSpec, e);
|
||||||
} finally {
|
} finally {
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,6 @@
|
||||||
package com.google.android.exoplayer2.testutil;
|
package com.google.android.exoplayer2.testutil;
|
||||||
|
|
||||||
import static com.google.common.truth.Truth.assertThat;
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
import static org.junit.Assert.fail;
|
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.database.sqlite.SQLiteDatabase;
|
import android.database.sqlite.SQLiteDatabase;
|
||||||
|
|
@ -53,7 +52,6 @@ import java.io.InputStream;
|
||||||
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.util.concurrent.TimeoutException;
|
import java.util.concurrent.TimeoutException;
|
||||||
import org.checkerframework.checker.nullness.qual.EnsuresNonNull;
|
import org.checkerframework.checker.nullness.qual.EnsuresNonNull;
|
||||||
|
|
@ -77,52 +75,6 @@ public class TestUtil {
|
||||||
|
|
||||||
private TestUtil() {}
|
private TestUtil() {}
|
||||||
|
|
||||||
/**
|
|
||||||
* Given an open {@link DataSource}, repeatedly calls {@link DataSource#read(byte[], int, int)}
|
|
||||||
* until {@link C#RESULT_END_OF_INPUT} is returned.
|
|
||||||
*
|
|
||||||
* @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);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Given an open {@link DataSource}, repeatedly calls {@link DataSource#read(byte[], int, int)}
|
|
||||||
* until exactly {@code length} bytes have been read.
|
|
||||||
*
|
|
||||||
* @param dataSource The source from which to read.
|
|
||||||
* @return The read data.
|
|
||||||
* @throws IOException If an error occurs reading from the source.
|
|
||||||
*/
|
|
||||||
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) {
|
|
||||||
fail("Not enough data could be read: " + position + " < " + length);
|
|
||||||
} else {
|
|
||||||
position += bytesRead;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Equivalent to {@code buildTestData(length, length)}.
|
* Equivalent to {@code buildTestData(length, length)}.
|
||||||
*
|
*
|
||||||
|
|
@ -271,7 +223,7 @@ public class TestUtil {
|
||||||
try {
|
try {
|
||||||
long length = dataSource.open(dataSpec);
|
long length = dataSource.open(dataSpec);
|
||||||
assertThat(length).isEqualTo(expectKnownLength ? expectedData.length : C.LENGTH_UNSET);
|
assertThat(length).isEqualTo(expectKnownLength ? expectedData.length : C.LENGTH_UNSET);
|
||||||
byte[] readData = readToEnd(dataSource);
|
byte[] readData = Util.readToEnd(dataSource);
|
||||||
assertThat(readData).isEqualTo(expectedData);
|
assertThat(readData).isEqualTo(expectedData);
|
||||||
} finally {
|
} finally {
|
||||||
dataSource.close();
|
dataSource.close();
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue