From ae6e082d2f4ed4304211a9c818afd81e5b8a7388 Mon Sep 17 00:00:00 2001 From: Oliver Woodman Date: Mon, 27 Oct 2014 10:54:51 +0000 Subject: [PATCH] Add a UriDataSource for reading from file or network. --- .../exoplayer/upstream/UriDataSource.java | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 library/src/main/java/com/google/android/exoplayer/upstream/UriDataSource.java diff --git a/library/src/main/java/com/google/android/exoplayer/upstream/UriDataSource.java b/library/src/main/java/com/google/android/exoplayer/upstream/UriDataSource.java new file mode 100644 index 0000000000..1243576b17 --- /dev/null +++ b/library/src/main/java/com/google/android/exoplayer/upstream/UriDataSource.java @@ -0,0 +1,85 @@ +/* + * Copyright (C) 2014 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.exoplayer.upstream; + +import com.google.android.exoplayer.util.Assertions; + +import java.io.IOException; + +/** + * A data source that fetches data from a local or remote {@link DataSpec}. + */ +public final class UriDataSource implements DataSource { + + private static final String FILE_URI_SCHEME = "file"; + + private final DataSource fileDataSource; + private final DataSource httpDataSource; + + /** + * {@code null} if no data source is open. Otherwise, equal to {@link #fileDataSource} if the open + * data source is a file, or {@link #httpDataSource} otherwise. + */ + private DataSource dataSource; + + /** + * Constructs a new data source that delegates to a {@link FileDataSource} for file URIs and an + * {@link HttpDataSource} for other URIs. + * + * @param userAgent The User-Agent string that should be used when requesting remote data. + * @param transferListener An optional listener. + */ + public UriDataSource(String userAgent, TransferListener transferListener) { + this(new FileDataSource(transferListener), + new HttpDataSource(userAgent, null, transferListener)); + } + + /** + * Constructs a new data source using {@code fileDataSource} for file URIs, and + * {@code httpDataSource} for non-file URIs. + * + * @param fileDataSource {@link DataSource} to use for file URIs. + * @param httpDataSource {@link DataSource} to use for non-file URIs. + */ + public UriDataSource(DataSource fileDataSource, DataSource httpDataSource) { + this.fileDataSource = Assertions.checkNotNull(fileDataSource); + this.httpDataSource = Assertions.checkNotNull(httpDataSource); + } + + @Override + public long open(DataSpec dataSpec) throws IOException { + Assertions.checkState(dataSource == null); + + dataSource = dataSpec.uri.getScheme().equals(FILE_URI_SCHEME) ? fileDataSource : httpDataSource; + return dataSource.open(dataSpec); + } + + @Override + public void close() throws IOException { + Assertions.checkNotNull(dataSource); + + dataSource.close(); + dataSource = null; + } + + @Override + public int read(byte[] buffer, int offset, int readLength) throws IOException { + Assertions.checkNotNull(dataSource); + + return dataSource.read(buffer, offset, readLength); + } + +}