package M;

import androidx.media3.session.legacy.PlaybackStateCompat;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.channels.FileChannel;

/* JADX INFO: loaded from: classes.dex */
public class c {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    private byte[] f1373a;

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

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

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

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

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

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

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    private BufferedInputStream f1380h;

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

    public c(InputStream inputStream) throws Exception {
        this.f1374b = null;
        this.f1381i = false;
        this.f1379g = PlaybackStateCompat.ACTION_SET_CAPTIONING_ENABLED;
        this.f1373a = new byte[1048576];
        try {
            this.f1380h = new BufferedInputStream(inputStream);
            this.f1377e = 0L;
            f(0L);
        } catch (Exception e2) {
            this.f1380h.close();
            e2.printStackTrace();
            throw new Exception("BufferedInputStream exception ~");
        }
    }

    private void f(long j2) throws Exception {
        String str;
        String str2;
        String str3 = "IO error at offset +";
        if (this.f1374b != null) {
            try {
                this.f1377e = j2;
                this.f1376d.position(j2);
                long j3 = this.f1379g - j2;
                if (j3 >= 0) {
                    byte[] bArr = this.f1373a;
                    int i2 = j3 < ((long) bArr.length) ? this.f1375c.read(bArr, 0, (int) j3) : this.f1375c.read(bArr, 0, bArr.length);
                    this.f1378f = (((long) i2) + j2) - 1;
                    b0.o.c("FileBuffer", 16, "numRead = ", Integer.valueOf(i2), "offset = ", Long.valueOf(j2));
                    return;
                }
                throw new Exception("Offset " + j2 + " out of bounds for file " + this.f1374b);
            } catch (IOException unused) {
                a();
                throw new Exception("IO error at offset +" + j2 + " of file '" + this.f1374b + "'");
            } catch (IllegalArgumentException unused2) {
                a();
                throw new Exception("IO error at offset +" + j2 + " of file '" + this.f1374b + "'");
            }
        }
        BufferedInputStream bufferedInputStream = this.f1380h;
        if (bufferedInputStream == null) {
            if (this.f1381i) {
                try {
                    this.f1377e = j2;
                    long j4 = this.f1379g;
                    if (j4 - j2 >= 0) {
                        this.f1378f = j4 - 1;
                        b0.o.c("FileBuffer", 16, "offset = ", Long.valueOf(j2), " length = ", Long.valueOf(this.f1379g));
                        return;
                    }
                    throw new Exception("Offset " + j2 + " out of bounds for file " + this.f1374b);
                } catch (IOException unused3) {
                    a();
                    throw new Exception("IO error at offset +" + j2 + " of file '" + this.f1374b + "'");
                } catch (IllegalArgumentException unused4) {
                    a();
                    throw new Exception("IO error at offset +" + j2 + " of file '" + this.f1374b + "'");
                }
            }
            return;
        }
        try {
            try {
                this.f1377e = j2;
                try {
                    bufferedInputStream.mark(((int) j2) + 1048576);
                } catch (Exception e2) {
                    try {
                        e2.printStackTrace();
                    } catch (IOException e3) {
                        e = e3;
                        str = str3;
                        str2 = str;
                        a();
                        e.printStackTrace();
                        throw new Exception(str2 + j2 + " of file '" + this.f1374b + "'");
                    }
                }
                long j5 = 0;
                while (j5 < j2) {
                    str = str3;
                    try {
                        long jSkip = this.f1380h.skip(j2 - j5);
                        if (jSkip <= 0) {
                            break;
                        }
                        j5 += jSkip;
                        str3 = str;
                    } catch (IOException e4) {
                        e = e4;
                        str2 = str;
                        a();
                        e.printStackTrace();
                        throw new Exception(str2 + j2 + " of file '" + this.f1374b + "'");
                    } catch (IllegalArgumentException e5) {
                        e = e5;
                        a();
                        e.printStackTrace();
                        throw new Exception(str + j2 + " of file '" + this.f1374b + "'");
                    }
                }
                b0.o.c("FileBuffer", 16, "offset = ", Long.valueOf(j2), " totalSkipped = ", Long.valueOf(j5));
                if (j5 != j2) {
                    throw new Exception("Skip " + j2 + " skip fail, actual skip " + j5);
                }
                int i3 = this.f1380h.read(this.f1373a, 0, 1048576);
                if (i3 < 1048576) {
                    this.f1379g = ((long) i3) + j2;
                } else {
                    this.f1379g = ((long) (i3 * 2)) + j2;
                }
                try {
                    this.f1380h.reset();
                } catch (Exception e6) {
                    e6.printStackTrace();
                }
                this.f1378f = (((long) i3) + j2) - 1;
                b0.o.c("FileBuffer", 16, "numRead = ", Integer.valueOf(i3), " offset = ", Long.valueOf(j2), " offsetEnd = ", Long.valueOf(this.f1378f), "length = ", Long.valueOf(this.f1379g));
            } catch (IOException e7) {
                e = e7;
                str2 = "IO error at offset +";
            }
        } catch (IllegalArgumentException e8) {
            e = e8;
            str = str3;
        }
    }

    public void a() {
        try {
            BufferedInputStream bufferedInputStream = this.f1380h;
            if (bufferedInputStream != null) {
                bufferedInputStream.close();
            }
            FileChannel fileChannel = this.f1376d;
            if (fileChannel != null) {
                fileChannel.close();
            }
            FileInputStream fileInputStream = this.f1375c;
            if (fileInputStream != null) {
                fileInputStream.close();
            }
        } catch (IOException e2) {
            e2.printStackTrace();
        }
    }

    public int b(long j2) throws Exception {
        if (j2 < this.f1377e || j2 > this.f1378f) {
            f(j2);
        }
        return this.f1373a[(int) (j2 - this.f1377e)] & 255;
    }

    public int c(long j2) throws Exception {
        if (j2 < this.f1377e || 3 + j2 > this.f1378f) {
            f(j2);
        }
        int i2 = (int) (j2 - this.f1377e);
        byte[] bArr = this.f1373a;
        return ((bArr[i2] & 255) << 24) | (bArr[i2 + 3] & 255) | ((bArr[i2 + 2] & 255) << 8) | ((bArr[i2 + 1] & 255) << 16);
    }

    public long d() {
        return this.f1379g;
    }

    public int e(long j2) throws Exception {
        if (j2 < this.f1377e || 1 + j2 > this.f1378f) {
            f(j2);
        }
        int i2 = (int) (j2 - this.f1377e);
        byte[] bArr = this.f1373a;
        return ((bArr[i2] & 255) << 8) | (bArr[i2 + 1] & 255);
    }

    public final void finalize() throws Throwable {
        BufferedInputStream bufferedInputStream = this.f1380h;
        if (bufferedInputStream != null) {
            bufferedInputStream.close();
        }
        FileChannel fileChannel = this.f1376d;
        if (fileChannel != null) {
            fileChannel.close();
        }
        FileInputStream fileInputStream = this.f1375c;
        if (fileInputStream != null) {
            fileInputStream.close();
        }
        super.finalize();
    }

    public c(byte[] bArr) throws Exception {
        this.f1374b = null;
        this.f1379g = bArr.length;
        this.f1381i = true;
        this.f1373a = bArr;
        this.f1377e = 0L;
        f(0L);
    }
}
