package a0;

import java.io.EOFException;
import java.io.IOException;
import java.io.RandomAccessFile;

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

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

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private RandomAccessFile f1988b;

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

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

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

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

    public m(w wVar) {
        this.f1987a = wVar;
    }

    @Override // a0.f
    public long a(h hVar) throws a {
        try {
            this.f1989c = hVar.f1940a.toString();
            RandomAccessFile randomAccessFile = new RandomAccessFile(hVar.f1940a.getPath(), "r");
            this.f1988b = randomAccessFile;
            randomAccessFile.seek(hVar.f1943d);
            long length = hVar.f1944e;
            if (length == -1) {
                length = this.f1988b.length() - hVar.f1943d;
            }
            this.f1990d = length;
            if (length < 0) {
                throw new EOFException();
            }
            this.f1991e = true;
            w wVar = this.f1987a;
            if (wVar != null) {
                wVar.b();
            }
            return this.f1990d;
        } catch (IOException e2) {
            throw new a(e2);
        }
    }

    @Override // a0.f
    public void close() {
        this.f1989c = null;
        RandomAccessFile randomAccessFile = this.f1988b;
        if (randomAccessFile != null) {
            try {
                try {
                    randomAccessFile.close();
                } catch (IOException e2) {
                    throw new a(e2);
                }
            } finally {
                this.f1988b = null;
                if (this.f1991e) {
                    this.f1991e = false;
                    w wVar = this.f1987a;
                    if (wVar != null) {
                        wVar.a();
                    }
                }
            }
        }
    }

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

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