package a0;

import android.content.ContentResolver;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/* JADX INFO: loaded from: classes.dex */
public final class e implements x {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    private final ContentResolver f1928a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private final w f1929b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private AssetFileDescriptor f1930c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private InputStream f1931d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private String f1932e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private long f1933f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    private boolean f1934g;

    public static class a extends IOException {
        public a(IOException iOException) {
            super(iOException);
        }
    }

    public e(Context context, w wVar) {
        this.f1928a = context.getContentResolver();
        this.f1929b = wVar;
    }

    @Override // a0.f
    public long a(h hVar) throws a {
        try {
            this.f1932e = hVar.f1940a.toString();
            this.f1930c = this.f1928a.openAssetFileDescriptor(hVar.f1940a, "r");
            FileInputStream fileInputStream = new FileInputStream(this.f1930c.getFileDescriptor());
            this.f1931d = fileInputStream;
            if (fileInputStream.skip(hVar.f1943d) < hVar.f1943d) {
                throw new EOFException();
            }
            long j2 = hVar.f1944e;
            if (j2 != -1) {
                this.f1933f = j2;
            } else {
                long jAvailable = this.f1931d.available();
                this.f1933f = jAvailable;
                if (jAvailable == 0) {
                    this.f1933f = -1L;
                }
            }
            this.f1934g = true;
            w wVar = this.f1929b;
            if (wVar != null) {
                wVar.b();
            }
            return this.f1933f;
        } catch (IOException e2) {
            throw new a(e2);
        }
    }

    @Override // a0.f
    public void close() {
        this.f1932e = null;
        try {
            try {
                InputStream inputStream = this.f1931d;
                if (inputStream != null) {
                    inputStream.close();
                }
                this.f1931d = null;
                try {
                    try {
                        AssetFileDescriptor assetFileDescriptor = this.f1930c;
                        if (assetFileDescriptor != null) {
                            assetFileDescriptor.close();
                        }
                    } catch (IOException e2) {
                        throw new a(e2);
                    }
                } finally {
                    this.f1930c = null;
                    if (this.f1934g) {
                        this.f1934g = false;
                        w wVar = this.f1929b;
                        if (wVar != null) {
                            wVar.a();
                        }
                    }
                }
            } catch (IOException e3) {
                throw new a(e3);
            }
        } catch (Throwable th) {
            this.f1931d = null;
            try {
                try {
                    AssetFileDescriptor assetFileDescriptor2 = this.f1930c;
                    if (assetFileDescriptor2 != null) {
                        assetFileDescriptor2.close();
                    }
                    this.f1930c = null;
                    if (this.f1934g) {
                        this.f1934g = false;
                        w wVar2 = this.f1929b;
                        if (wVar2 != null) {
                            wVar2.a();
                        }
                    }
                    throw th;
                } catch (IOException e4) {
                    throw new a(e4);
                }
            } finally {
                this.f1930c = null;
                if (this.f1934g) {
                    this.f1934g = false;
                    w wVar3 = this.f1929b;
                    if (wVar3 != null) {
                        wVar3.a();
                    }
                }
            }
        }
    }

    @Override // a0.x
    public String getUri() {
        return this.f1932e;
    }

    @Override // a0.f
    public int read(byte[] bArr, int i2, int i3) throws a {
        long j2 = this.f1933f;
        if (j2 == 0) {
            return -1;
        }
        if (j2 != -1) {
            try {
                i3 = (int) Math.min(j2, i3);
            } catch (IOException e2) {
                throw new a(e2);
            }
        }
        int i4 = this.f1931d.read(bArr, i2, i3);
        if (i4 > 0) {
            long j3 = this.f1933f;
            if (j3 != -1) {
                this.f1933f = j3 - ((long) i4);
            }
            w wVar = this.f1929b;
            if (wVar != null) {
                wVar.c(i4);
            }
        }
        return i4;
    }
}
