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
1e3337e4
authored
Feb 26, 2021
by
olly
Committed by
Oliver Woodman
Mar 02, 2021
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
Add RawResourceDataSource contract test
PiperOrigin-RevId: 359743165
parent
759b0431
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
109 additions
and
5 deletions
library/core/src/androidTest/java/com/google/android/exoplayer2/upstream/RawResourceDataSourceContractTest.java
library/core/src/androidTest/res/raw/resource1
library/core/src/androidTest/res/raw/resource2
library/core/src/main/java/com/google/android/exoplayer2/upstream/RawResourceDataSource.java
library/core/src/androidTest/java/com/google/android/exoplayer2/upstream/RawResourceDataSourceContractTest.java
0 → 100644
View file @
1e3337e4
/*
* Copyright (C) 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
com
.
google
.
android
.
exoplayer2
.
upstream
;
import
android.content.res.Resources
;
import
android.net.Uri
;
import
androidx.test.core.app.ApplicationProvider
;
import
androidx.test.ext.junit.runners.AndroidJUnit4
;
import
com.google.android.exoplayer2.core.test.R
;
import
com.google.android.exoplayer2.testutil.DataSourceContractTest
;
import
com.google.android.exoplayer2.util.Util
;
import
com.google.common.collect.ImmutableList
;
import
org.junit.runner.RunWith
;
/** {@link DataSource} contract tests for {@link RawResourceDataSource}. */
@RunWith
(
AndroidJUnit4
.
class
)
public
final
class
RawResourceDataSourceContractTest
extends
DataSourceContractTest
{
private
static
final
byte
[]
RESOURCE_1_DATA
=
Util
.
getUtf8Bytes
(
"resource1 abc\n"
);
private
static
final
byte
[]
RESOURCE_2_DATA
=
Util
.
getUtf8Bytes
(
"resource2 abcdef\n"
);
@Override
protected
DataSource
createDataSource
()
{
return
new
RawResourceDataSource
(
ApplicationProvider
.
getApplicationContext
());
}
@Override
protected
ImmutableList
<
TestResource
>
getTestResources
()
{
// Android packages raw resources into a single file. When reading a resource other than the
// last one, Android does not prevent accidentally reading beyond the end of the resource and
// into the next one. We use two resources in this test to ensure that when packaged, at least
// one of them has a subsequent resource. This allows the contract test to enforce that the
// RawResourceDataSource implementation doesn't erroneously read into the second resource when
// opened to read the first.
return
ImmutableList
.
of
(
new
TestResource
.
Builder
()
.
setName
(
"resource 1"
)
.
setUri
(
RawResourceDataSource
.
buildRawResourceUri
(
R
.
raw
.
resource1
))
.
setExpectedBytes
(
RESOURCE_1_DATA
)
.
build
(),
new
TestResource
.
Builder
()
.
setName
(
"resource 2"
)
.
setUri
(
RawResourceDataSource
.
buildRawResourceUri
(
R
.
raw
.
resource2
))
.
setExpectedBytes
(
RESOURCE_2_DATA
)
.
build
(),
// Additional resources using different URI schemes.
new
TestResource
.
Builder
()
.
setName
(
"android.resource:// with path"
)
.
setUri
(
Uri
.
parse
(
"android.resource://"
+
ApplicationProvider
.
getApplicationContext
().
getPackageName
()
+
"/raw/resource1"
))
.
setExpectedBytes
(
RESOURCE_1_DATA
)
.
build
(),
new
TestResource
.
Builder
()
.
setName
(
"android.resource:// with ID"
)
.
setUri
(
Uri
.
parse
(
"android.resource://"
+
ApplicationProvider
.
getApplicationContext
().
getPackageName
()
+
"/"
+
R
.
raw
.
resource1
))
.
setExpectedBytes
(
RESOURCE_1_DATA
)
.
build
());
}
@Override
protected
Uri
getNotFoundUri
()
{
return
RawResourceDataSource
.
buildRawResourceUri
(
Resources
.
ID_NULL
);
}
}
library/core/src/androidTest/res/raw/resource1
0 → 100644
View file @
1e3337e4
resource1 abc
library/core/src/androidTest/res/raw/resource2
0 → 100644
View file @
1e3337e4
resource2 abcdef
library/core/src/main/java/com/google/android/exoplayer2/upstream/RawResourceDataSource.java
View file @
1e3337e4
...
...
@@ -60,7 +60,7 @@ public final class RawResourceDataSource extends BaseDataSource {
super
(
message
);
}
public
RawResourceDataSourceException
(
IOException
e
)
{
public
RawResourceDataSourceException
(
Throwable
e
)
{
super
(
e
);
}
}
...
...
@@ -133,21 +133,39 @@ public final class RawResourceDataSource extends BaseDataSource {
}
transferInitializing
(
dataSpec
);
AssetFileDescriptor
assetFileDescriptor
=
resources
.
openRawResourceFd
(
resourceId
);
AssetFileDescriptor
assetFileDescriptor
;
try
{
assetFileDescriptor
=
resources
.
openRawResourceFd
(
resourceId
);
}
catch
(
Resources
.
NotFoundException
e
)
{
throw
new
RawResourceDataSourceException
(
e
);
}
this
.
assetFileDescriptor
=
assetFileDescriptor
;
if
(
assetFileDescriptor
==
null
)
{
throw
new
RawResourceDataSourceException
(
"Resource is compressed: "
+
uri
);
}
long
assetFileDescriptorLength
=
assetFileDescriptor
.
getLength
();
FileInputStream
inputStream
=
new
FileInputStream
(
assetFileDescriptor
.
getFileDescriptor
());
this
.
inputStream
=
inputStream
;
try
{
// We can't rely only on the "skipped < dataSpec.position" check below to detect whether the
// position is beyond the end of the resource being read. This is because the file will
// typically contain multiple resources, and there's nothing to prevent InputStream.skip()
// from succeeding by skipping into the data of the next resource. Hence we also need to check
// against the resource length explicitly, which is guaranteed to be set unless the resource
// extends to the end of the file.
if
(
assetFileDescriptorLength
!=
AssetFileDescriptor
.
UNKNOWN_LENGTH
&&
dataSpec
.
position
>
assetFileDescriptorLength
)
{
throw
new
DataSourceException
(
DataSourceException
.
POSITION_OUT_OF_RANGE
);
}
inputStream
.
skip
(
assetFileDescriptor
.
getStartOffset
());
long
skipped
=
inputStream
.
skip
(
dataSpec
.
position
);
if
(
skipped
<
dataSpec
.
position
)
{
// We expect the skip to be satisfied in full. If it isn't then we're probably trying to
//
skip beyond the end of the data
.
throw
new
EOFException
(
);
//
read beyond the end of the last resource in the file
.
throw
new
DataSourceException
(
DataSourceException
.
POSITION_OUT_OF_RANGE
);
}
}
catch
(
IOException
e
)
{
throw
new
RawResourceDataSourceException
(
e
);
...
...
@@ -156,7 +174,6 @@ public final class RawResourceDataSource extends BaseDataSource {
if
(
dataSpec
.
length
!=
C
.
LENGTH_UNSET
)
{
bytesRemaining
=
dataSpec
.
length
;
}
else
{
long
assetFileDescriptorLength
=
assetFileDescriptor
.
getLength
();
// If the length is UNKNOWN_LENGTH then the asset extends to the end of the file.
bytesRemaining
=
assetFileDescriptorLength
==
AssetFileDescriptor
.
UNKNOWN_LENGTH
...
...
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