package q4;

import D1.F;
import android.util.Log;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.NavigableSet;
import java.util.TreeSet;
import java.util.concurrent.atomic.AtomicInteger;
import k4.h;
import k4.j;
import n4.K0;
import o4.C1350a;

/* JADX INFO: renamed from: q4.a, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public final class C1461a {

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

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public static final int f16254f = 15;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public static final C1350a f16255g = new C1350a();

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public static final F f16256h = new F(15);
    public static final h i = new h(2);

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final AtomicInteger f16257a = new AtomicInteger(0);

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final C1463c f16258b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final s4.b f16259c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final j f16260d;

    public C1461a(C1463c c1463c, s4.b bVar, j jVar) {
        this.f16258b = c1463c;
        this.f16259c = bVar;
        this.f16260d = jVar;
    }

    public static void a(List list) {
        Iterator it = list.iterator();
        while (it.hasNext()) {
            ((File) it.next()).delete();
        }
    }

    public static String e(File file) {
        byte[] bArr = new byte[8192];
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        FileInputStream fileInputStream = new FileInputStream(file);
        while (true) {
            try {
                int i5 = fileInputStream.read(bArr);
                if (i5 <= 0) {
                    String str = new String(byteArrayOutputStream.toByteArray(), f16253e);
                    fileInputStream.close();
                    return str;
                }
                byteArrayOutputStream.write(bArr, 0, i5);
            } catch (Throwable th) {
                try {
                    fileInputStream.close();
                } catch (Throwable th2) {
                    th.addSuppressed(th2);
                }
                throw th;
            }
        }
    }

    public static void f(File file, String str) {
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(file), f16253e);
        try {
            outputStreamWriter.write(str);
            outputStreamWriter.close();
        } catch (Throwable th) {
            try {
                outputStreamWriter.close();
            } catch (Throwable th2) {
                th.addSuppressed(th2);
            }
            throw th;
        }
    }

    public final ArrayList b() {
        ArrayList arrayList = new ArrayList();
        C1463c c1463c = this.f16258b;
        arrayList.addAll(C1463c.k(((File) c1463c.f16267f).listFiles()));
        arrayList.addAll(C1463c.k(((File) c1463c.f16268g).listFiles()));
        F f3 = f16256h;
        Collections.sort(arrayList, f3);
        List listK = C1463c.k(((File) c1463c.f16266e).listFiles());
        Collections.sort(listK, f3);
        arrayList.addAll(listK);
        return arrayList;
    }

    public final NavigableSet c() {
        return new TreeSet(C1463c.k(((File) this.f16258b.f16265d).list())).descendingSet();
    }

    public final void d(K0 k02, String str, boolean z8) {
        C1463c c1463c = this.f16258b;
        W2.j jVar = this.f16259c.b().f16858a;
        f16255g.getClass();
        try {
            f(c1463c.f(str, I1.a.p("event", String.format(Locale.US, "%010d", Integer.valueOf(this.f16257a.getAndIncrement())), z8 ? "_" : "")), C1350a.f15716a.o(k02));
        } catch (IOException e8) {
            Log.w("FirebaseCrashlytics", "Could not persist event for session " + str, e8);
        }
        h hVar = new h(3);
        c1463c.getClass();
        File file = new File((File) c1463c.f16265d, str);
        file.mkdirs();
        List<File> listK = C1463c.k(file.listFiles(hVar));
        Collections.sort(listK, new F(16));
        int size = listK.size();
        for (File file2 : listK) {
            if (size <= jVar.f7097q) {
                return;
            }
            C1463c.j(file2);
            size--;
        }
    }
}
