package o;

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;

/* compiled from: r8-map-id-3b7cc9102578c509342fa2f0fabed2bfc72239cd258d579fc679bfcbc198b048 */
/* renamed from: o.xo, reason: case insensitive filesystem */
/* loaded from: classes.dex */
public final class C2939xo {
    public static final Charset e = Charset.forName("UTF-8");
    public static final int f = 15;
    public static final C2844wo g = new C2844wo();
    public static final C0347Nk h = new C0347Nk(2);
    public static final C0195Hn i = new C0195Hn(2);
    public final AtomicInteger a = new AtomicInteger(0);
    public final C1533iy b;
    public final C0598Xb c;
    public final C0221In d;

    public C2939xo(C1533iy c1533iy, C0598Xb c0598Xb, C0221In c0221In) {
        this.b = c1533iy;
        this.c = c0598Xb;
        this.d = c0221In;
    }

    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 i2 = fileInputStream.read(bArr);
                if (i2 <= 0) {
                    String str = new String(byteArrayOutputStream.toByteArray(), e);
                    fileInputStream.close();
                    return str;
                }
                byteArrayOutputStream.write(bArr, 0, i2);
            } 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), e);
        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();
        C1533iy c1533iy = this.b;
        arrayList.addAll(C1533iy.e(((File) c1533iy.q).listFiles()));
        arrayList.addAll(C1533iy.e(((File) c1533iy.r).listFiles()));
        C0347Nk c0347Nk = h;
        Collections.sort(arrayList, c0347Nk);
        List listE = C1533iy.e(((File) c1533iy.p).listFiles());
        Collections.sort(listE, c0347Nk);
        arrayList.addAll(listE);
        return arrayList;
    }

    public final NavigableSet c() {
        return new TreeSet(C1533iy.e(((File) this.b.f590o).list())).descendingSet();
    }

    public final void d(AbstractC2275qo abstractC2275qo, String str, boolean z) {
        C1533iy c1533iy = this.b;
        int i2 = this.c.b().a.l;
        g.getClass();
        try {
            f(c1533iy.b(str, AbstractC2462sn.j("event", String.format(Locale.US, "%010d", Integer.valueOf(this.a.getAndIncrement())), z ? "_" : "")), C2844wo.a.c(abstractC2275qo));
        } catch (IOException unused) {
        }
        C0195Hn c0195Hn = new C0195Hn(3);
        c1533iy.getClass();
        File file = new File((File) c1533iy.f590o, str);
        file.mkdirs();
        List<File> listE = C1533iy.e(file.listFiles(c0195Hn));
        Collections.sort(listE, new C0347Nk(3));
        int size = listE.size();
        for (File file2 : listE) {
            if (size <= i2) {
                return;
            }
            C1533iy.d(file2);
            size--;
        }
    }
}
