Skip to content

Commit

Permalink
Add specific error message if file-not-found for path with fragment o…
Browse files Browse the repository at this point in the history
…r query

This doesn't change the current behaviour, just adds a clear error message
to the developer with instructions on how to avoid it.

Issue:#6470
PiperOrigin-RevId: 272405556
  • Loading branch information
icbaker authored and ojw28 committed Oct 2, 2019
1 parent 6780b80 commit f7b8d07
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
2 changes: 2 additions & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@
[#5973](https://github.com/google/ExoPlayer/issues/5973)).
* Expose the raw ICY metadata through `IcyInfo`
([#6476](https://github.com/google/ExoPlayer/issues/6476)).
* Fail more explicitly when local-file Uris contain invalid parts (e.g.
fragment) ([#6470](https://github.com/google/ExoPlayer/issues/6470)).

### 2.10.5 (2019-09-20) ###

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
import static com.google.android.exoplayer2.util.Util.castNonNull;

import android.net.Uri;
import android.text.TextUtils;
import androidx.annotation.Nullable;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.util.Assertions;
import java.io.EOFException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

Expand All @@ -37,6 +39,9 @@ public FileDataSourceException(IOException cause) {
super(cause);
}

public FileDataSourceException(String message, IOException cause) {
super(message, cause);
}
}

@Nullable private RandomAccessFile file;
Expand All @@ -55,8 +60,8 @@ public long open(DataSpec dataSpec) throws FileDataSourceException {
this.uri = uri;

transferInitializing(dataSpec);
RandomAccessFile file = new RandomAccessFile(Assertions.checkNotNull(uri.getPath()), "r");
this.file = file;

this.file = openLocalFile(uri);

file.seek(dataSpec.position);
bytesRemaining = dataSpec.length == C.LENGTH_UNSET ? file.length() - dataSpec.position
Expand All @@ -74,6 +79,23 @@ public long open(DataSpec dataSpec) throws FileDataSourceException {
return bytesRemaining;
}

private static RandomAccessFile openLocalFile(Uri uri) throws FileDataSourceException {
try {
return new RandomAccessFile(Assertions.checkNotNull(uri.getPath()), "r");
} catch (FileNotFoundException e) {
if (!TextUtils.isEmpty(uri.getQuery()) || !TextUtils.isEmpty(uri.getFragment())) {
throw new FileDataSourceException(
String.format(
"uri has query and/or fragment, which are not supported. Did you call Uri.parse()"
+ " on a string containing '?' or '#'? Use Uri.fromFile(new File(path)) to"
+ " avoid this. path=%s,query=%s,fragment=%s",
uri.getPath(), uri.getQuery(), uri.getFragment()),
e);
}
throw new FileDataSourceException(e);
}
}

@Override
public int read(byte[] buffer, int offset, int readLength) throws FileDataSourceException {
if (readLength == 0) {
Expand Down

0 comments on commit f7b8d07

Please sign in to comment.