package o;

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;

/* compiled from: r8-map-id-3b7cc9102578c509342fa2f0fabed2bfc72239cd258d579fc679bfcbc198b048 */
/* loaded from: classes.dex */
public final class NZ implements Closeable {
    public static final Logger r = Logger.getLogger(NZ.class.getName());
    public final RandomAccessFile l;
    public int m;
    public int n;

    /* renamed from: o, reason: collision with root package name */
    public KZ f258o;
    public KZ p;
    public final byte[] q;

    public NZ(File file) throws IOException {
        byte[] bArr = new byte[16];
        this.q = 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 i2 = 0; i2 < 4; i2++) {
                    z(bArr2, i, iArr[i2]);
                    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.l = randomAccessFile2;
        randomAccessFile2.seek(0L);
        randomAccessFile2.readFully(bArr);
        int iJ = j(0, bArr);
        this.m = iJ;
        if (iJ > randomAccessFile2.length()) {
            throw new IOException("File is truncated. Expected length: " + this.m + ", Actual length: " + randomAccessFile2.length());
        }
        this.n = j(4, bArr);
        int iJ2 = j(8, bArr);
        int iJ3 = j(12, bArr);
        this.f258o = g(iJ2);
        this.p = g(iJ3);
    }

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

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

    public final void b(byte[] bArr) {
        int iW;
        int length = bArr.length;
        synchronized (this) {
            if (length >= 0) {
                if (length <= bArr.length) {
                    c(length);
                    boolean zF = f();
                    if (zF) {
                        iW = 16;
                    } else {
                        KZ kz = this.p;
                        iW = w(kz.b + 4 + kz.c);
                    }
                    KZ kz2 = new KZ(iW, length, 0);
                    z(this.q, 0, length);
                    q(this.q, iW, 4);
                    q(bArr, iW + 4, length);
                    y(this.m, this.n + 1, zF ? iW : this.f258o.b, iW);
                    this.p = kz2;
                    this.n++;
                    if (zF) {
                        this.f258o = kz2;
                    }
                }
            }
            throw new IndexOutOfBoundsException();
        }
    }

    public final void c(int i) throws IOException {
        int i2 = i + 4;
        int iU = this.m - u();
        if (iU >= i2) {
            return;
        }
        int i3 = this.m;
        do {
            iU += i3;
            i3 <<= 1;
        } while (iU < i2);
        RandomAccessFile randomAccessFile = this.l;
        randomAccessFile.setLength(i3);
        randomAccessFile.getChannel().force(true);
        KZ kz = this.p;
        int iW = w(kz.b + 4 + kz.c);
        if (iW < this.f258o.b) {
            FileChannel channel = randomAccessFile.getChannel();
            channel.position(this.m);
            long j = iW - 4;
            if (channel.transferTo(16L, j, channel) != j) {
                throw new AssertionError("Copied insufficient number of bytes!");
            }
        }
        int i4 = this.p.b;
        int i5 = this.f258o.b;
        if (i4 < i5) {
            int i6 = (this.m + i4) - 16;
            y(i3, this.n, i5, i6);
            this.p = new KZ(i6, this.p.c, 0);
        } else {
            y(i3, this.n, i5, i4);
        }
        this.m = i3;
    }

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

    public final synchronized void d(MZ mz) {
        int iW = this.f258o.b;
        for (int i = 0; i < this.n; i++) {
            KZ kzG = g(iW);
            mz.b(new LZ(this, kzG), kzG.c);
            iW = w(kzG.b + 4 + kzG.c);
        }
    }

    public final synchronized boolean f() {
        return this.n == 0;
    }

    public final KZ g(int i) throws IOException {
        if (i == 0) {
            return KZ.d;
        }
        RandomAccessFile randomAccessFile = this.l;
        randomAccessFile.seek(i);
        return new KZ(i, randomAccessFile.readInt(), 0);
    }

    public final synchronized void l() {
        if (f()) {
            throw new NoSuchElementException();
        }
        if (this.n == 1) {
            synchronized (this) {
                y(4096, 0, 0, 0);
                this.n = 0;
                KZ kz = KZ.d;
                this.f258o = kz;
                this.p = kz;
                if (this.m > 4096) {
                    RandomAccessFile randomAccessFile = this.l;
                    randomAccessFile.setLength(4096);
                    randomAccessFile.getChannel().force(true);
                }
                this.m = 4096;
            }
        } else {
            KZ kz2 = this.f258o;
            int iW = w(kz2.b + 4 + kz2.c);
            m(this.q, iW, 0, 4);
            int iJ = j(0, this.q);
            y(this.m, this.n - 1, iW, this.p.b);
            this.n--;
            this.f258o = new KZ(iW, iJ, 0);
        }
    }

    public final void m(byte[] bArr, int i, int i2, int i3) throws IOException {
        int iW = w(i);
        int i4 = iW + i3;
        int i5 = this.m;
        RandomAccessFile randomAccessFile = this.l;
        if (i4 <= i5) {
            randomAccessFile.seek(iW);
            randomAccessFile.readFully(bArr, i2, i3);
            return;
        }
        int i6 = i5 - iW;
        randomAccessFile.seek(iW);
        randomAccessFile.readFully(bArr, i2, i6);
        randomAccessFile.seek(16L);
        randomAccessFile.readFully(bArr, i2 + i6, i3 - i6);
    }

    public final void q(byte[] bArr, int i, int i2) throws IOException {
        int iW = w(i);
        int i3 = iW + i2;
        int i4 = this.m;
        RandomAccessFile randomAccessFile = this.l;
        if (i3 <= i4) {
            randomAccessFile.seek(iW);
            randomAccessFile.write(bArr, 0, i2);
            return;
        }
        int i5 = i4 - iW;
        randomAccessFile.seek(iW);
        randomAccessFile.write(bArr, 0, i5);
        randomAccessFile.seek(16L);
        randomAccessFile.write(bArr, i5, i2 - i5);
    }

    public final String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(NZ.class.getSimpleName());
        sb.append("[fileLength=");
        sb.append(this.m);
        sb.append(", size=");
        sb.append(this.n);
        sb.append(", first=");
        sb.append(this.f258o);
        sb.append(", last=");
        sb.append(this.p);
        sb.append(", element lengths=[");
        try {
            d(new C2160pe(sb));
        } catch (IOException e) {
            r.log(Level.WARNING, "read error", (Throwable) e);
        }
        sb.append("]]");
        return sb.toString();
    }

    public final int u() {
        if (this.n == 0) {
            return 16;
        }
        KZ kz = this.p;
        int i = kz.b;
        int i2 = this.f258o.b;
        return i >= i2 ? (i - i2) + 4 + kz.c + 16 : (((i + 4) + kz.c) + this.m) - i2;
    }

    public final int w(int i) {
        int i2 = this.m;
        return i < i2 ? i : (i + 16) - i2;
    }

    public final void y(int i, int i2, int i3, int i4) throws IOException {
        int[] iArr = {i, i2, i3, i4};
        int i5 = 0;
        int i6 = 0;
        while (true) {
            byte[] bArr = this.q;
            if (i5 >= 4) {
                RandomAccessFile randomAccessFile = this.l;
                randomAccessFile.seek(0L);
                randomAccessFile.write(bArr);
                return;
            } else {
                z(bArr, i6, iArr[i5]);
                i6 += 4;
                i5++;
            }
        }
    }
}
