package m4;

import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.util.NoSuchElementException;
import java.util.logging.Level;
import java.util.logging.Logger;

/* JADX INFO: renamed from: m4.l, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public final class C1176l implements Closeable, AutoCloseable {

    /* JADX INFO: renamed from: w, reason: collision with root package name */
    public static final Logger f14630w = Logger.getLogger(C1176l.class.getName());

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

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

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    public int f14633s;

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    public C1173i f14634t;

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    public C1173i f14635u;

    /* JADX INFO: renamed from: v, reason: collision with root package name */
    public final byte[] f14636v;

    public C1176l(File file) throws IOException {
        byte[] bArr = new byte[16];
        this.f14636v = bArr;
        if (!file.exists()) {
            File file2 = new File(file.getPath() + ".tmp");
            RandomAccessFile randomAccessFile = new RandomAccessFile(file2, "rwd");
            try {
                randomAccessFile.setLength(4096L);
                randomAccessFile.seek(0L);
                byte[] bArr2 = new byte[16];
                int[] iArr = {4096, 0, 0, 0};
                int i = 0;
                for (int i5 = 0; i5 < 4; i5++) {
                    V(bArr2, i, iArr[i5]);
                    i += 4;
                }
                randomAccessFile.write(bArr2);
                randomAccessFile.close();
                if (!file2.renameTo(file)) {
                    throw new IOException("Rename failed!");
                }
            } catch (Throwable th) {
                randomAccessFile.close();
                throw th;
            }
        }
        RandomAccessFile randomAccessFile2 = new RandomAccessFile(file, "rwd");
        this.f14631q = randomAccessFile2;
        randomAccessFile2.seek(0L);
        randomAccessFile2.readFully(bArr);
        int iR = r(0, bArr);
        this.f14632r = iR;
        if (iR > randomAccessFile2.length()) {
            throw new IOException("File is truncated. Expected length: " + this.f14632r + ", Actual length: " + randomAccessFile2.length());
        }
        this.f14633s = r(4, bArr);
        int iR2 = r(8, bArr);
        int iR3 = r(12, bArr);
        this.f14634t = n(iR2);
        this.f14635u = n(iR3);
    }

    public static void V(byte[] bArr, int i, int i5) {
        bArr[i] = (byte) (i5 >> 24);
        bArr[i + 1] = (byte) (i5 >> 16);
        bArr[i + 2] = (byte) (i5 >> 8);
        bArr[i + 3] = (byte) i5;
    }

    public static int r(int i, byte[] bArr) {
        return ((bArr[i] & 255) << 24) + ((bArr[i + 1] & 255) << 16) + ((bArr[i + 2] & 255) << 8) + (bArr[i + 3] & 255);
    }

    public final void A(int i, int i5, int i8, byte[] bArr) throws IOException {
        int iO = O(i);
        int i9 = iO + i8;
        int i10 = this.f14632r;
        RandomAccessFile randomAccessFile = this.f14631q;
        if (i9 <= i10) {
            randomAccessFile.seek(iO);
            randomAccessFile.readFully(bArr, i5, i8);
            return;
        }
        int i11 = i10 - iO;
        randomAccessFile.seek(iO);
        randomAccessFile.readFully(bArr, i5, i11);
        randomAccessFile.seek(16L);
        randomAccessFile.readFully(bArr, i5 + i11, i8 - i11);
    }

    public final void H(byte[] bArr, int i, int i5) throws IOException {
        int iO = O(i);
        int i8 = iO + i5;
        int i9 = this.f14632r;
        RandomAccessFile randomAccessFile = this.f14631q;
        if (i8 <= i9) {
            randomAccessFile.seek(iO);
            randomAccessFile.write(bArr, 0, i5);
            return;
        }
        int i10 = i9 - iO;
        randomAccessFile.seek(iO);
        randomAccessFile.write(bArr, 0, i10);
        randomAccessFile.seek(16L);
        randomAccessFile.write(bArr, i10, i5 - i10);
    }

    public final int K() {
        if (this.f14633s == 0) {
            return 16;
        }
        C1173i c1173i = this.f14635u;
        int i = c1173i.f14625a;
        int i5 = this.f14634t.f14625a;
        return i >= i5 ? (i - i5) + 4 + c1173i.f14626b + 16 : (((i + 4) + c1173i.f14626b) + this.f14632r) - i5;
    }

    public final int O(int i) {
        int i5 = this.f14632r;
        return i < i5 ? i : (i + 16) - i5;
    }

    public final void Q(int i, int i5, int i8, int i9) throws IOException {
        int[] iArr = {i, i5, i8, i9};
        byte[] bArr = this.f14636v;
        int i10 = 0;
        for (int i11 = 0; i11 < 4; i11++) {
            V(bArr, i10, iArr[i11]);
            i10 += 4;
        }
        RandomAccessFile randomAccessFile = this.f14631q;
        randomAccessFile.seek(0L);
        randomAccessFile.write(bArr);
    }

    public final void a(byte[] bArr) {
        int iO;
        int length = bArr.length;
        synchronized (this) {
            if (length >= 0) {
                if (length <= bArr.length) {
                    g(length);
                    boolean zM = m();
                    if (zM) {
                        iO = 16;
                    } else {
                        C1173i c1173i = this.f14635u;
                        iO = O(c1173i.f14625a + 4 + c1173i.f14626b);
                    }
                    C1173i c1173i2 = new C1173i(iO, length);
                    V(this.f14636v, 0, length);
                    H(this.f14636v, iO, 4);
                    H(bArr, iO + 4, length);
                    Q(this.f14632r, this.f14633s + 1, zM ? iO : this.f14634t.f14625a, iO);
                    this.f14635u = c1173i2;
                    this.f14633s++;
                    if (zM) {
                        this.f14634t = c1173i2;
                    }
                }
            }
            throw new IndexOutOfBoundsException();
        }
    }

    public final synchronized void b() {
        Q(4096, 0, 0, 0);
        this.f14633s = 0;
        C1173i c1173i = C1173i.f14624c;
        this.f14634t = c1173i;
        this.f14635u = c1173i;
        if (this.f14632r > 4096) {
            RandomAccessFile randomAccessFile = this.f14631q;
            randomAccessFile.setLength(4096);
            randomAccessFile.getChannel().force(true);
        }
        this.f14632r = 4096;
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public final synchronized void close() {
        this.f14631q.close();
    }

    public final void g(int i) throws IOException {
        int i5 = i + 4;
        int iK = this.f14632r - K();
        if (iK >= i5) {
            return;
        }
        int i8 = this.f14632r;
        do {
            iK += i8;
            i8 <<= 1;
        } while (iK < i5);
        RandomAccessFile randomAccessFile = this.f14631q;
        randomAccessFile.setLength(i8);
        randomAccessFile.getChannel().force(true);
        C1173i c1173i = this.f14635u;
        int iO = O(c1173i.f14625a + 4 + c1173i.f14626b);
        if (iO < this.f14634t.f14625a) {
            FileChannel channel = randomAccessFile.getChannel();
            channel.position(this.f14632r);
            long j8 = iO - 4;
            if (channel.transferTo(16L, j8, channel) != j8) {
                throw new AssertionError("Copied insufficient number of bytes!");
            }
        }
        int i9 = this.f14635u.f14625a;
        int i10 = this.f14634t.f14625a;
        if (i9 < i10) {
            int i11 = (this.f14632r + i9) - 16;
            Q(i8, this.f14633s, i10, i11);
            this.f14635u = new C1173i(i11, this.f14635u.f14626b);
        } else {
            Q(i8, this.f14633s, i10, i9);
        }
        this.f14632r = i8;
    }

    public final synchronized void j(InterfaceC1175k interfaceC1175k) {
        int iO = this.f14634t.f14625a;
        for (int i = 0; i < this.f14633s; i++) {
            C1173i c1173iN = n(iO);
            interfaceC1175k.a(new C1174j(this, c1173iN), c1173iN.f14626b);
            iO = O(c1173iN.f14625a + 4 + c1173iN.f14626b);
        }
    }

    public final synchronized boolean m() {
        return this.f14633s == 0;
    }

    public final C1173i n(int i) throws IOException {
        if (i == 0) {
            return C1173i.f14624c;
        }
        RandomAccessFile randomAccessFile = this.f14631q;
        randomAccessFile.seek(i);
        return new C1173i(i, randomAccessFile.readInt());
    }

    public final String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(C1176l.class.getSimpleName());
        sb.append("[fileLength=");
        sb.append(this.f14632r);
        sb.append(", size=");
        sb.append(this.f14633s);
        sb.append(", first=");
        sb.append(this.f14634t);
        sb.append(", last=");
        sb.append(this.f14635u);
        sb.append(", element lengths=[");
        try {
            j(new K.g(sb));
        } catch (IOException e8) {
            f14630w.log(Level.WARNING, "read error", (Throwable) e8);
        }
        sb.append("]]");
        return sb.toString();
    }

    public final synchronized void u() {
        try {
            if (m()) {
                throw new NoSuchElementException();
            }
            if (this.f14633s == 1) {
                b();
            } else {
                C1173i c1173i = this.f14634t;
                int iO = O(c1173i.f14625a + 4 + c1173i.f14626b);
                A(iO, 0, 4, this.f14636v);
                int iR = r(0, this.f14636v);
                Q(this.f14632r, this.f14633s - 1, iO, this.f14635u.f14625a);
                this.f14633s--;
                this.f14634t = new C1173i(iO, iR);
            }
        } catch (Throwable th) {
            throw th;
        }
    }
}
