mirror of
https://github.com/samsonjs/media.git
synced 2026-04-27 15:07:40 +00:00
Throw correct exception.
This commit is contained in:
parent
042bd2ff82
commit
4fbe4fc7d5
1 changed files with 37 additions and 37 deletions
|
|
@ -27,6 +27,7 @@ import android.net.Uri;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InterruptedIOException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A {@link DataSource} that reads and writes a {@link Cache}. Requests are fulfilled from the cache
|
* A {@link DataSource} that reads and writes a {@link Cache}. Requests are fulfilled from the cache
|
||||||
|
|
@ -182,46 +183,45 @@ public final class CacheDataSource implements DataSource {
|
||||||
* opened to read from the upstream source and write into the cache.
|
* opened to read from the upstream source and write into the cache.
|
||||||
*/
|
*/
|
||||||
private void openNextSource() throws IOException {
|
private void openNextSource() throws IOException {
|
||||||
try {
|
DataSpec dataSpec;
|
||||||
DataSpec dataSpec;
|
CacheSpan span;
|
||||||
CacheSpan span;
|
if (ignoreCache) {
|
||||||
if (ignoreCache) {
|
span = null;
|
||||||
span = null;
|
} else if (bytesRemaining == C.LENGTH_UNBOUNDED) {
|
||||||
} else if (bytesRemaining == C.LENGTH_UNBOUNDED) {
|
// TODO: Support caching for unbounded requests. This requires storing the source length
|
||||||
// TODO: Support caching for unbounded requests. This requires storing the source length
|
// into the cache (the simplest approach is to incorporate it into each cache file's name).
|
||||||
// into the cache (the simplest approach is to incorporate it into each cache file's name).
|
Log.w(TAG, "Cache bypassed due to unbounded length.");
|
||||||
Log.w(TAG, "Cache bypassed due to unbounded length.");
|
span = null;
|
||||||
span = null;
|
} else if (blockOnCache) {
|
||||||
} else if (blockOnCache) {
|
try {
|
||||||
span = cache.startReadWrite(key, readPosition);
|
span = cache.startReadWrite(key, readPosition);
|
||||||
} else {
|
} catch (InterruptedException e) {
|
||||||
span = cache.startReadWriteNonBlocking(key, readPosition);
|
throw new InterruptedIOException();
|
||||||
}
|
}
|
||||||
if (span == null) {
|
} else {
|
||||||
// The data is locked in the cache, or we're ignoring the cache. Bypass the cache and read
|
span = cache.startReadWriteNonBlocking(key, readPosition);
|
||||||
// from upstream.
|
|
||||||
currentDataSource = upstreamDataSource;
|
|
||||||
dataSpec = new DataSpec(uri, readPosition, bytesRemaining, key, flags);
|
|
||||||
} else if (span.isCached) {
|
|
||||||
// Data is cached, read from cache.
|
|
||||||
Uri fileUri = Uri.fromFile(span.file);
|
|
||||||
long filePosition = readPosition - span.position;
|
|
||||||
long length = Math.min(span.length - filePosition, bytesRemaining);
|
|
||||||
dataSpec = new DataSpec(fileUri, readPosition, filePosition, length, key, flags);
|
|
||||||
currentDataSource = cacheReadDataSource;
|
|
||||||
} else {
|
|
||||||
// Data is not cached, and data is not locked, read from upstream with cache backing.
|
|
||||||
lockedSpan = span;
|
|
||||||
long length = span.isOpenEnded() ? bytesRemaining : Math.min(span.length, bytesRemaining);
|
|
||||||
dataSpec = new DataSpec(uri, readPosition, length, key, flags);
|
|
||||||
currentDataSource = cacheWriteDataSource != null ? cacheWriteDataSource
|
|
||||||
: upstreamDataSource;
|
|
||||||
}
|
|
||||||
currentDataSource.open(dataSpec);
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
// Should never happen.
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
}
|
||||||
|
if (span == null) {
|
||||||
|
// The data is locked in the cache, or we're ignoring the cache. Bypass the cache and read
|
||||||
|
// from upstream.
|
||||||
|
currentDataSource = upstreamDataSource;
|
||||||
|
dataSpec = new DataSpec(uri, readPosition, bytesRemaining, key, flags);
|
||||||
|
} else if (span.isCached) {
|
||||||
|
// Data is cached, read from cache.
|
||||||
|
Uri fileUri = Uri.fromFile(span.file);
|
||||||
|
long filePosition = readPosition - span.position;
|
||||||
|
long length = Math.min(span.length - filePosition, bytesRemaining);
|
||||||
|
dataSpec = new DataSpec(fileUri, readPosition, filePosition, length, key, flags);
|
||||||
|
currentDataSource = cacheReadDataSource;
|
||||||
|
} else {
|
||||||
|
// Data is not cached, and data is not locked, read from upstream with cache backing.
|
||||||
|
lockedSpan = span;
|
||||||
|
long length = span.isOpenEnded() ? bytesRemaining : Math.min(span.length, bytesRemaining);
|
||||||
|
dataSpec = new DataSpec(uri, readPosition, length, key, flags);
|
||||||
|
currentDataSource = cacheWriteDataSource != null ? cacheWriteDataSource
|
||||||
|
: upstreamDataSource;
|
||||||
|
}
|
||||||
|
currentDataSource.open(dataSpec);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void closeCurrentSource() throws IOException {
|
private void closeCurrentSource() throws IOException {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue