package Q;

import I.d;
import I.g;
import I.i;
import android.util.Log;
import b0.o;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import n.z;
import okhttp3.HttpUrl;

/* JADX INFO: loaded from: classes.dex */
public class a implements i {

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

    public a() {
        this(true);
    }

    public static int d(byte[] bArr, int i2, int i3) {
        int i4 = i3 - 1;
        int i5 = 0;
        for (int i6 = i2; i6 < i2 + i3; i6++) {
            i5 += (bArr[i6] & 255) << (i4 * 8);
            i4--;
        }
        return i5;
    }

    @Override // I.i
    public boolean a(String str) {
        boolean zEquals = "text/srt".equals(str);
        o.d("InternalSrtParser", "canParse  type = " + str + " can = " + zEquals, 16);
        return zEquals;
    }

    @Override // I.i
    public g b(byte[] bArr, int i2, int i3) {
        long jD;
        BufferedReader bufferedReader;
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bArr, i2, i3);
        byte[] bArr2 = new byte[8];
        byteArrayInputStream.mark(8);
        if (-1 == byteArrayInputStream.read(bArr2, 0, 8)) {
            Log.d("InternalSrtParser", "inputStream read is -1");
        }
        if (new String(bArr2).startsWith("dftT")) {
            jD = d(bArr2, 4, 4);
        } else {
            byteArrayInputStream.reset();
            jD = 1000000;
        }
        long j2 = jD;
        try {
            bufferedReader = new BufferedReader(new InputStreamReader(byteArrayInputStream, "UTF-8"));
        } catch (UnsupportedEncodingException unused) {
            bufferedReader = new BufferedReader(new InputStreamReader(byteArrayInputStream));
        }
        String str = HttpUrl.FRAGMENT_ENCODE_SET;
        while (true) {
            try {
                try {
                    String line = bufferedReader.readLine();
                    if (line != null) {
                        o.d("InternalSrtParser", "parse line = " + line, 16);
                        if (!line.isEmpty()) {
                            if (!str.isEmpty()) {
                                str = str + "\n";
                            }
                            str = str + e(line.trim());
                        }
                    } else {
                        try {
                            break;
                        } catch (IOException unused2) {
                        }
                    }
                } catch (IOException e2) {
                    throw new z(e2);
                }
            } catch (Throwable th) {
                try {
                    bufferedReader.close();
                } catch (IOException unused3) {
                }
                throw th;
            }
        }
        bufferedReader.close();
        return !str.isEmpty() ? new d(str, 0L, j2) : new d();
    }

    protected String e(String str) {
        return this.f1573a ? str.replaceAll("</?[i|I|u|U|B|b]>", HttpUrl.FRAGMENT_ENCODE_SET).replaceAll("\\<.*?\\>", HttpUrl.FRAGMENT_ENCODE_SET).replaceAll("&lt;", "<").replaceAll("&gt;", ">").replaceAll("&nbsp;", " ").replaceAll("&amp;", "&") : str;
    }

    public a(boolean z2) {
        this.f1573a = z2;
    }
}
