1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
/*
* Copyright (C) 2017 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.android.tv.tuner.exoplayer.tests;
import android.content.Context;
import android.content.res.AssetManager;
import com.google.android.exoplayer.C;
import com.google.android.exoplayer.upstream.DataSource;
import com.google.android.exoplayer.upstream.DataSpec;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
/** A local asset. */
// Copied from com.google.android.exoplayer.upstream.AssetDataSource for test.
final class AssetDataSource implements DataSource {
/** Thrown when an {@link IOException} is encountered reading a local asset. */
private static final class AssetDataSourceException extends IOException {
private AssetDataSourceException(IOException cause) {
super(cause);
}
}
private final AssetManager mAssetManager;
private InputStream mInputStream;
private long mBytesRemaining;
private boolean mOpened;
/** Constructs a new {@link DataSource} that retrieves data from a local asset. */
AssetDataSource(Context context) {
mAssetManager = context.getAssets();
}
@Override
public long open(DataSpec dataSpec) throws AssetDataSourceException {
try {
String path = dataSpec.uri.getPath();
if (path.startsWith("/android_asset/")) {
path = path.substring(15);
} else if (path.startsWith("/")) {
path = path.substring(1);
}
mInputStream = mAssetManager.open(path, AssetManager.ACCESS_RANDOM);
long skipped = mInputStream.skip(dataSpec.position);
if (skipped < dataSpec.position) {
// mAssetManager.open() returns an AssetInputStream, whose skip() implementation
// only skips fewer bytes than requested if the skip is beyond the end of the
// asset's data.
throw new EOFException();
}
if (dataSpec.length != C.LENGTH_UNBOUNDED) {
mBytesRemaining = dataSpec.length;
} else {
mBytesRemaining = mInputStream.available();
if (mBytesRemaining == Integer.MAX_VALUE) {
// mAssetManager.open() returns an AssetInputStream, whose available()
// implementation returns Integer.MAX_VALUE if the remaining length is greater
// than (or equal to) Integer.MAX_VALUE. We don't know the true length in this
// case, so treat as unbounded.
mBytesRemaining = C.LENGTH_UNBOUNDED;
}
}
} catch (IOException e) {
throw new AssetDataSourceException(e);
}
mOpened = true;
return mBytesRemaining;
}
@Override
public int read(byte[] buffer, int offset, int readLength) throws AssetDataSourceException {
if (mBytesRemaining == 0) {
return -1;
} else {
int bytesRead = 0;
try {
int bytesToRead =
mBytesRemaining == C.LENGTH_UNBOUNDED
? readLength
: (int) Math.min(mBytesRemaining, readLength);
bytesRead = mInputStream.read(buffer, offset, bytesToRead);
} catch (IOException e) {
throw new AssetDataSourceException(e);
}
if (bytesRead > 0 && mBytesRemaining != C.LENGTH_UNBOUNDED) {
mBytesRemaining -= bytesRead;
}
return bytesRead;
}
}
@Override
public void close() throws AssetDataSourceException {
if (mInputStream != null) {
try {
mInputStream.close();
} catch (IOException e) {
throw new AssetDataSourceException(e);
} finally {
mInputStream = null;
if (mOpened) {
mOpened = false;
}
}
}
}
}
|