package m4;

import android.util.Log;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Locale;

/* JADX INFO: renamed from: m4.m, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public final class C1177m implements InterfaceC1168d {

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    public static final Charset f14637s = Charset.forName("UTF-8");

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public final File f14638q;

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    public C1176l f14639r;

    public C1177m(File file) {
        this.f14638q = file;
    }

    @Override // m4.InterfaceC1168d
    public final void a() {
        k4.g.b(this.f14639r, "There was a problem closing the Crashlytics log file.");
        this.f14639r = null;
    }

    /* JADX WARN: Removed duplicated region for block: B:4:0x000a  */
    @Override // m4.InterfaceC1168d
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final String b() {
        B7.a aVar;
        byte[] bArr;
        if (this.f14638q.exists()) {
            c();
            C1176l c1176l = this.f14639r;
            if (c1176l != null) {
                int[] iArr = {0};
                byte[] bArr2 = new byte[c1176l.K()];
                try {
                    this.f14639r.j(new C1170f(bArr2, iArr));
                } catch (IOException e8) {
                    Log.e("FirebaseCrashlytics", "A problem occurred while reading the Crashlytics log file.", e8);
                }
                aVar = new B7.a(iArr[0], 8, bArr2);
            }
        } else {
            aVar = null;
        }
        if (aVar == null) {
            bArr = null;
        } else {
            int i = aVar.f1039r;
            bArr = new byte[i];
            System.arraycopy((byte[]) aVar.f1040s, 0, bArr, 0, i);
        }
        if (bArr != null) {
            return new String(bArr, f14637s);
        }
        return null;
    }

    public final void c() {
        File file = this.f14638q;
        if (this.f14639r == null) {
            try {
                this.f14639r = new C1176l(file);
            } catch (IOException e8) {
                Log.e("FirebaseCrashlytics", "Could not open log file: " + file, e8);
            }
        }
    }

    @Override // m4.InterfaceC1168d
    public final void d(long j8, String str) {
        c();
        if (this.f14639r == null) {
            return;
        }
        if (str == null) {
            str = "null";
        }
        try {
            if (str.length() > 16384) {
                str = "..." + str.substring(str.length() - 16384);
            }
            this.f14639r.a(String.format(Locale.US, "%d %s%n", Long.valueOf(j8), str.replaceAll("\r", " ").replaceAll("\n", " ")).getBytes(f14637s));
            while (!this.f14639r.m() && this.f14639r.K() > 65536) {
                this.f14639r.u();
            }
        } catch (IOException e8) {
            Log.e("FirebaseCrashlytics", "There was a problem writing to the Crashlytics log.", e8);
        }
    }
}
