Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
SDK
/
exoplayer
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
18cf01cd
authored
Oct 18, 2021
by
olly
Committed by
Oliver Woodman
Oct 18, 2021
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
Move DataSource utils into a DataSourceUtil class
PiperOrigin-RevId: 403910535
parent
8fd1381a
Hide whitespace changes
Inline
Side-by-side
Showing
21 changed files
with
159 additions
and
120 deletions
demos/main/src/main/java/com/google/android/exoplayer2/demo/SampleChooserActivity.java
extensions/ima/src/main/java/com/google/android/exoplayer2/ext/ima/ImaUtil.java
library/common/src/main/java/com/google/android/exoplayer2/upstream/DataSourceUtil.java
library/common/src/main/java/com/google/android/exoplayer2/util/Util.java
library/common/src/test/java/com/google/android/exoplayer2/upstream/DataSchemeDataSourceTest.java
library/core/src/main/java/com/google/android/exoplayer2/source/ProgressiveMediaPeriod.java
library/core/src/main/java/com/google/android/exoplayer2/source/SingleSampleMediaPeriod.java
library/core/src/main/java/com/google/android/exoplayer2/source/chunk/ContainerMediaChunk.java
library/core/src/main/java/com/google/android/exoplayer2/source/chunk/DataChunk.java
library/core/src/main/java/com/google/android/exoplayer2/source/chunk/InitializationChunk.java
library/core/src/main/java/com/google/android/exoplayer2/source/chunk/SingleSampleMediaChunk.java
library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheWriter.java
library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/CacheDataSourceTest.java
library/extractor/src/test/java/com/google/android/exoplayer2/extractor/ts/PsExtractorSeekTest.java
library/extractor/src/test/java/com/google/android/exoplayer2/extractor/ts/TsExtractorSeekTest.java
library/hls/src/main/java/com/google/android/exoplayer2/source/hls/HlsMediaChunk.java
library/rtsp/src/main/java/com/google/android/exoplayer2/source/rtsp/RtpDataLoadable.java
library/rtsp/src/main/java/com/google/android/exoplayer2/source/rtsp/UdpDataSourceRtpDataChannelFactory.java
testutils/src/main/java/com/google/android/exoplayer2/testutil/CacheAsserts.java
testutils/src/main/java/com/google/android/exoplayer2/testutil/DataSourceContractTest.java
testutils/src/main/java/com/google/android/exoplayer2/testutil/TestUtil.java
demos/main/src/main/java/com/google/android/exoplayer2/demo/SampleChooserActivity.java
View file @
18cf01cd
...
...
@@ -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
);
DataSource
Util
.
closeQuietly
(
dataSource
);
}
}
return
result
;
...
...
extensions/ima/src/main/java/com/google/android/exoplayer2/ext/ima/ImaUtil.java
View file @
18cf01cd
...
...
@@ -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
(
DataSource
Util
.
readToEnd
(
dataSchemeDataSource
)));
}
finally
{
dataSchemeDataSource
.
close
();
}
...
...
library/common/src/main/java/com/google/android/exoplayer2/upstream/DataSourceUtil.java
0 → 100644
View file @
18cf01cd
/*
* 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.
}
}
}
library/common/src/main/java/com/google/android/exoplayer2/util/Util.java
View file @
18cf01cd
...
...
@@ -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
;
...
...
@@ -540,69 +539,6 @@ public final class Util {
}
/**
* 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}.
*
...
...
library/common/src/test/java/com/google/android/exoplayer2/upstream/DataSchemeDataSourceTest.java
View file @
18cf01cd
...
...
@@ -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
(
DataSource
Util
.
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
=
DataSource
Util
.
readToEnd
(
dataSource
);
assertThat
(
readData
).
isEqualTo
(
expectedData
);
}
finally
{
dataSource
.
close
();
...
...
library/core/src/main/java/com/google/android/exoplayer2/source/ProgressiveMediaPeriod.java
View file @
18cf01cd
...
...
@@ -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
);
DataSource
Util
.
closeQuietly
(
dataSource
);
}
}
}
...
...
library/core/src/main/java/com/google/android/exoplayer2/source/SingleSampleMediaPeriod.java
View file @
18cf01cd
...
...
@@ -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
);
DataSource
Util
.
closeQuietly
(
dataSource
);
}
}
}
...
...
library/core/src/main/java/com/google/android/exoplayer2/source/chunk/ContainerMediaChunk.java
View file @
18cf01cd
...
...
@@ -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
);
DataSource
Util
.
closeQuietly
(
dataSource
);
}
loadCompleted
=
!
loadCanceled
;
}
...
...
library/core/src/main/java/com/google/android/exoplayer2/source/chunk/DataChunk.java
View file @
18cf01cd
...
...
@@ -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
);
DataSource
Util
.
closeQuietly
(
dataSource
);
}
}
...
...
library/core/src/main/java/com/google/android/exoplayer2/source/chunk/InitializationChunk.java
View file @
18cf01cd
...
...
@@ -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
);
DataSource
Util
.
closeQuietly
(
dataSource
);
}
}
}
library/core/src/main/java/com/google/android/exoplayer2/source/chunk/SingleSampleMediaChunk.java
View file @
18cf01cd
...
...
@@ -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
);
DataSource
Util
.
closeQuietly
(
dataSource
);
}
loadCompleted
=
true
;
}
...
...
library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheWriter.java
View file @
18cf01cd
...
...
@@ -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
);
DataSource
Util
.
closeQuietly
(
dataSource
);
}
}
...
...
@@ -171,7 +171,7 @@ public final class CacheWriter {
try
{
resolvedLength
=
dataSource
.
open
(
unboundedDataSpec
);
}
catch
(
IOException
e
)
{
Util
.
closeQuietly
(
dataSource
);
DataSource
Util
.
closeQuietly
(
dataSource
);
throw
e
;
}
}
...
...
@@ -194,7 +194,7 @@ public final class CacheWriter {
onRequestEndPosition
(
position
+
totalBytesRead
);
}
}
catch
(
IOException
e
)
{
Util
.
closeQuietly
(
dataSource
);
DataSource
Util
.
closeQuietly
(
dataSource
);
throw
e
;
}
...
...
library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/CacheDataSourceTest.java
View file @
18cf01cd
...
...
@@ -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
);
DataSource
Util
.
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
);
DataSource
Util
.
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
);
DataSource
Util
.
readToEnd
(
cacheDataSource
);
cacheDataSource
.
close
();
}
...
...
@@ -440,7 +441,7 @@ public final class CacheDataSourceTest {
cacheWriter
.
cache
();
// Read the rest of the data.
Util
.
readToEnd
(
cacheDataSource
);
DataSource
Util
.
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
);
DataSource
Util
.
readExactly
(
cacheDataSource
,
100
);
// Delete cached data.
cache
.
removeResource
(
cacheDataSource
.
getCacheKeyFactory
().
buildCacheKey
(
unboundedDataSpec
));
assertCacheEmpty
(
cache
);
// Read the rest of the data.
Util
.
readToEnd
(
cacheDataSource
);
DataSource
Util
.
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
);
DataSource
Util
.
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
);
DataSource
Util
.
readToEnd
(
cacheDataSource
);
cacheDataSource
.
close
();
}
...
...
library/extractor/src/test/java/com/google/android/exoplayer2/extractor/ts/PsExtractorSeekTest.java
View file @
18cf01cd
...
...
@@ -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
);
DataSource
Util
.
closeQuietly
(
dataSource
);
return
totalInputLength
;
}
...
...
@@ -229,7 +229,7 @@ public final class PsExtractorSeekTest {
extractorReadResult
=
psExtractor
.
read
(
extractorInput
,
positionHolder
);
}
}
finally
{
Util
.
closeQuietly
(
dataSource
);
DataSource
Util
.
closeQuietly
(
dataSource
);
}
if
(
extractorReadResult
==
Extractor
.
RESULT_SEEK
)
{
...
...
@@ -257,7 +257,7 @@ public final class PsExtractorSeekTest {
readResult
=
extractor
.
read
(
input
,
positionHolder
);
}
}
finally
{
Util
.
closeQuietly
(
dataSource
);
DataSource
Util
.
closeQuietly
(
dataSource
);
}
if
(
readResult
==
Extractor
.
RESULT_SEEK
)
{
...
...
@@ -283,7 +283,7 @@ public final class PsExtractorSeekTest {
readResult
=
extractor
.
read
(
input
,
positionHolder
);
}
}
finally
{
Util
.
closeQuietly
(
dataSource
);
DataSource
Util
.
closeQuietly
(
dataSource
);
}
if
(
readResult
==
Extractor
.
RESULT_SEEK
)
{
input
=
getExtractorInputFromPosition
(
positionHolder
.
position
);
...
...
library/extractor/src/test/java/com/google/android/exoplayer2/extractor/ts/TsExtractorSeekTest.java
View file @
18cf01cd
...
...
@@ -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
);
DataSource
Util
.
closeQuietly
(
dataSource
);
}
if
(
readResult
==
Extractor
.
RESULT_SEEK
)
{
input
=
...
...
library/hls/src/main/java/com/google/android/exoplayer2/source/hls/HlsMediaChunk.java
View file @
18cf01cd
...
...
@@ -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
);
DataSource
Util
.
closeQuietly
(
dataSource
);
}
}
...
...
library/rtsp/src/main/java/com/google/android/exoplayer2/source/rtsp/RtpDataLoadable.java
View file @
18cf01cd
...
...
@@ -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
);
DataSource
Util
.
closeQuietly
(
dataChannel
);
}
}
...
...
library/rtsp/src/main/java/com/google/android/exoplayer2/source/rtsp/UdpDataSourceRtpDataChannelFactory.java
View file @
18cf01cd
...
...
@@ -15,7 +15,7 @@
*/
package
com
.
google
.
android
.
exoplayer2
.
source
.
rtsp
;
import
com.google.android.exoplayer2.u
til.
Util
;
import
com.google.android.exoplayer2.u
pstream.DataSource
Util
;
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
);
DataSource
Util
.
closeQuietly
(
firstChannel
);
DataSource
Util
.
closeQuietly
(
secondChannel
);
throw
e
;
}
}
...
...
testutils/src/main/java/com/google/android/exoplayer2/testutil/CacheAsserts.java
View file @
18cf01cd
...
...
@@ -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
=
DataSource
Util
.
readToEnd
(
dataSource
);
}
catch
(
IOException
e
)
{
throw
new
IOException
(
"Opening/reading cache failed: "
+
dataSpec
,
e
);
}
finally
{
...
...
testutils/src/main/java/com/google/android/exoplayer2/testutil/DataSourceContractTest.java
View file @
18cf01cd
...
...
@@ -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
);
?
DataSource
Util
.
readExactly
(
dataSource
,
resource
.
getExpectedBytes
().
length
)
:
DataSource
Util
.
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
);
?
DataSource
Util
.
readExactly
(
dataSource
,
resource
.
getExpectedBytes
().
length
-
3
)
:
DataSource
Util
.
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
=
DataSource
Util
.
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
=
DataSource
Util
.
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
=
DataSource
Util
.
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
);
?
DataSource
Util
.
readExactly
(
dataSource
,
resource
.
getExpectedBytes
().
length
)
:
DataSource
Util
.
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
);
DataSource
Util
.
readExactly
(
dataSource
,
resource
.
getExpectedBytes
().
length
);
}
else
{
Util
.
readToEnd
(
dataSource
);
DataSource
Util
.
readToEnd
(
dataSource
);
}
// Verify sufficient onBytesTransferred() callbacks have been triggered before closing the
// DataSource.
...
...
testutils/src/main/java/com/google/android/exoplayer2/testutil/TestUtil.java
View file @
18cf01cd
...
...
@@ -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
=
DataSource
Util
.
readToEnd
(
dataSource
);
assertThat
(
readData
).
isEqualTo
(
expectedData
);
}
finally
{
dataSource
.
close
();
...
...
@@ -325,7 +326,7 @@ public class TestUtil {
}
}
}
finally
{
Util
.
closeQuietly
(
dataSource
);
DataSource
Util
.
closeQuietly
(
dataSource
);
}
if
(
readResult
==
Extractor
.
RESULT_SEEK
)
{
...
...
@@ -413,7 +414,7 @@ public class TestUtil {
extractorReadResult
=
extractor
.
read
(
extractorInput
,
positionHolder
);
}
}
finally
{
Util
.
closeQuietly
(
dataSource
);
DataSource
Util
.
closeQuietly
(
dataSource
);
}
if
(
extractorReadResult
==
Extractor
.
RESULT_SEEK
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment