Rabbit-R1/original r1/java/sources/androidx/media3/datasource/FileDataSource.java

161 lines
5.6 KiB
Java
Raw Normal View History

2024-05-21 16:08:36 -05:00
package androidx.media3.datasource;
import android.net.Uri;
import android.system.ErrnoException;
import android.system.OsConstants;
import android.text.TextUtils;
import androidx.media3.common.util.Assertions;
import androidx.media3.common.util.Util;
import androidx.media3.datasource.DataSource;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
/* loaded from: classes2.dex */
public final class FileDataSource extends BaseDataSource {
private long bytesRemaining;
private RandomAccessFile file;
private boolean opened;
private Uri uri;
@Override // androidx.media3.datasource.DataSource
public Uri getUri() {
return this.uri;
}
/* loaded from: classes2.dex */
public static class FileDataSourceException extends DataSourceException {
@Deprecated
public FileDataSourceException(Exception exc) {
super(exc, 2000);
}
@Deprecated
public FileDataSourceException(String str, IOException iOException) {
super(str, iOException, 2000);
}
public FileDataSourceException(Throwable th, int i) {
super(th, i);
}
public FileDataSourceException(String str, Throwable th, int i) {
super(str, th, i);
}
}
/* loaded from: classes2.dex */
public static final class Factory implements DataSource.Factory {
private TransferListener listener;
public Factory setListener(TransferListener transferListener) {
this.listener = transferListener;
return this;
}
@Override // androidx.media3.datasource.DataSource.Factory
public FileDataSource createDataSource() {
FileDataSource fileDataSource = new FileDataSource();
TransferListener transferListener = this.listener;
if (transferListener != null) {
fileDataSource.addTransferListener(transferListener);
}
return fileDataSource;
}
}
public FileDataSource() {
super(false);
}
@Override // androidx.media3.datasource.DataSource
public long open(DataSpec dataSpec) throws FileDataSourceException {
Uri uri = dataSpec.uri;
this.uri = uri;
transferInitializing(dataSpec);
RandomAccessFile openLocalFile = openLocalFile(uri);
this.file = openLocalFile;
try {
openLocalFile.seek(dataSpec.position);
long length = dataSpec.length == -1 ? this.file.length() - dataSpec.position : dataSpec.length;
this.bytesRemaining = length;
if (length < 0) {
throw new FileDataSourceException(null, null, 2008);
}
this.opened = true;
transferStarted(dataSpec);
return this.bytesRemaining;
} catch (IOException e) {
throw new FileDataSourceException(e, 2000);
}
}
@Override // androidx.media3.common.DataReader
public int read(byte[] bArr, int i, int i2) throws FileDataSourceException {
if (i2 == 0) {
return 0;
}
if (this.bytesRemaining == 0) {
return -1;
}
try {
int read = ((RandomAccessFile) Util.castNonNull(this.file)).read(bArr, i, (int) Math.min(this.bytesRemaining, i2));
if (read > 0) {
this.bytesRemaining -= read;
bytesTransferred(read);
}
return read;
} catch (IOException e) {
throw new FileDataSourceException(e, 2000);
}
}
@Override // androidx.media3.datasource.DataSource
public void close() throws FileDataSourceException {
this.uri = null;
try {
try {
RandomAccessFile randomAccessFile = this.file;
if (randomAccessFile != null) {
randomAccessFile.close();
}
} catch (IOException e) {
throw new FileDataSourceException(e, 2000);
}
} finally {
this.file = null;
if (this.opened) {
this.opened = false;
transferEnded();
}
}
}
private static RandomAccessFile openLocalFile(Uri uri) throws FileDataSourceException {
try {
return new RandomAccessFile((String) 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, 1004);
}
throw new FileDataSourceException(e, (Util.SDK_INT < 21 || !Api21.isPermissionError(e.getCause())) ? 2005 : 2006);
} catch (SecurityException e2) {
throw new FileDataSourceException(e2, 2006);
} catch (RuntimeException e3) {
throw new FileDataSourceException(e3, 2000);
}
}
/* JADX INFO: Access modifiers changed from: private */
/* loaded from: classes2.dex */
public static final class Api21 {
private Api21() {
}
/* JADX INFO: Access modifiers changed from: private */
public static boolean isPermissionError(Throwable th) {
return (th instanceof ErrnoException) && ((ErrnoException) th).errno == OsConstants.EACCES;
}
}
}