package defpackage;

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: ᵭᵝʡʦ, reason: contains not printable characters */
/* JADX INFO: loaded from: classes3.dex */
public final class C4155 implements Closeable {

    /* JADX INFO: renamed from: ˈᵲᵢ, reason: contains not printable characters */
    public static final Logger f15846 = Logger.getLogger(C4155.class.getName());

    /* JADX INFO: renamed from: ˌᴵ, reason: contains not printable characters */
    public final RandomAccessFile f15847;

    /* JADX INFO: renamed from: ᵔᵖᵹᵫ, reason: contains not printable characters */
    public int f15848;

    /* JADX INFO: renamed from: ᵜᵪʾ, reason: contains not printable characters */
    public int f15849;

    /* JADX INFO: renamed from: ᵨᵼⁱ, reason: contains not printable characters */
    public C4240 f15850;

    /* JADX INFO: renamed from: ᵯᵷʽ, reason: contains not printable characters */
    public C4240 f15851;

    /* JADX INFO: renamed from: ᵶˇᵱ, reason: contains not printable characters */
    public final byte[] f15852;

    public C4155(File file) throws IOException {
        byte[] bArr = new byte[16];
        this.f15852 = 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++) {
                    m9070(bArr2, i, iArr[i2]);
                    i += 4;
                }
                randomAccessFile.write(bArr2);
                randomAccessFile.close();
                if (!file2.renameTo(file)) {
                    C1685.m4568("Rename failed!");
                    throw null;
                }
            } catch (Throwable th) {
                randomAccessFile.close();
                throw th;
            }
        }
        RandomAccessFile randomAccessFile2 = new RandomAccessFile(file, "rwd");
        this.f15847 = randomAccessFile2;
        randomAccessFile2.seek(0L);
        randomAccessFile2.readFully(bArr);
        int iM9069 = m9069(0, bArr);
        this.f15849 = iM9069;
        if (iM9069 <= randomAccessFile2.length()) {
            this.f15848 = m9069(4, bArr);
            int iM90692 = m9069(8, bArr);
            int iM90693 = m9069(12, bArr);
            this.f15850 = m9077(iM90692);
            this.f15851 = m9077(iM90693);
            return;
        }
        throw new IOException("File is truncated. Expected length: " + this.f15849 + ", Actual length: " + randomAccessFile2.length());
    }

    /* JADX INFO: renamed from: ˈᵲᵢ, reason: contains not printable characters */
    public static int m9069(int i, byte[] bArr) {
        return ((bArr[i] & 255) << 24) + ((bArr[i + 1] & 255) << 16) + ((bArr[i + 2] & 255) << 8) + (bArr[i + 3] & 255);
    }

    /* JADX INFO: renamed from: ᵛˀˊ, reason: contains not printable characters */
    public static void m9070(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;
    }

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

    public final String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(C4155.class.getSimpleName());
        sb.append("[fileLength=");
        sb.append(this.f15849);
        sb.append(", size=");
        sb.append(this.f15848);
        sb.append(", first=");
        sb.append(this.f15850);
        sb.append(", last=");
        sb.append(this.f15851);
        sb.append(", element lengths=[");
        try {
            m9080(new C5559(sb));
        } catch (IOException e) {
            f15846.log(Level.WARNING, "read error", (Throwable) e);
        }
        sb.append("]]");
        return sb.toString();
    }

    /* JADX INFO: renamed from: ˀᵗ, reason: contains not printable characters */
    public final void m9071(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.f15852;
            if (i5 >= 4) {
                RandomAccessFile randomAccessFile = this.f15847;
                randomAccessFile.seek(0L);
                randomAccessFile.write(bArr);
                return;
            } else {
                m9070(bArr, i6, iArr[i5]);
                i6 += 4;
                i5++;
            }
        }
    }

    /* JADX INFO: renamed from: ˈⁱᵡ, reason: contains not printable characters */
    public final void m9072(byte[] bArr, int i, int i2) throws IOException {
        int iM9073 = m9073(i);
        int i3 = iM9073 + i2;
        int i4 = this.f15849;
        RandomAccessFile randomAccessFile = this.f15847;
        if (i3 <= i4) {
            randomAccessFile.seek(iM9073);
            randomAccessFile.write(bArr, 0, i2);
            return;
        }
        int i5 = i4 - iM9073;
        randomAccessFile.seek(iM9073);
        randomAccessFile.write(bArr, 0, i5);
        randomAccessFile.seek(16L);
        randomAccessFile.write(bArr, i5, i2 - i5);
    }

    /* JADX INFO: renamed from: ˊˌˊ, reason: contains not printable characters */
    public final int m9073(int i) {
        int i2 = this.f15849;
        return i < i2 ? i : (i + 16) - i2;
    }

    /* JADX INFO: renamed from: ˏːˆ, reason: contains not printable characters */
    public final synchronized boolean m9074() {
        return this.f15848 == 0;
    }

    /* JADX INFO: renamed from: ːˏᵟᵯ, reason: contains not printable characters */
    public final synchronized void m9075() {
        m9071(4096, 0, 0, 0);
        this.f15848 = 0;
        C4240 c4240 = C4240.f16167;
        this.f15850 = c4240;
        this.f15851 = c4240;
        if (this.f15849 > 4096) {
            RandomAccessFile randomAccessFile = this.f15847;
            randomAccessFile.setLength(4096L);
            randomAccessFile.getChannel().force(true);
        }
        this.f15849 = 4096;
    }

    /* JADX INFO: renamed from: ˑˊᵲ, reason: contains not printable characters */
    public final void m9076(int i, int i2, int i3, byte[] bArr) throws IOException {
        int iM9073 = m9073(i);
        int i4 = iM9073 + i3;
        int i5 = this.f15849;
        RandomAccessFile randomAccessFile = this.f15847;
        if (i4 <= i5) {
            randomAccessFile.seek(iM9073);
            randomAccessFile.readFully(bArr, i2, i3);
            return;
        }
        int i6 = i5 - iM9073;
        randomAccessFile.seek(iM9073);
        randomAccessFile.readFully(bArr, i2, i6);
        randomAccessFile.seek(16L);
        randomAccessFile.readFully(bArr, i2 + i6, i3 - i6);
    }

    /* JADX INFO: renamed from: ᵔᵖᵹᵫ, reason: contains not printable characters */
    public final C4240 m9077(int i) throws IOException {
        if (i == 0) {
            return C4240.f16167;
        }
        RandomAccessFile randomAccessFile = this.f15847;
        randomAccessFile.seek(i);
        return new C4240(i, randomAccessFile.readInt(), 0);
    }

    /* JADX INFO: renamed from: ᵟᵹᵙ, reason: contains not printable characters */
    public final void m9078(int i) throws IOException {
        int i2 = i + 4;
        int iM9079 = this.f15849 - m9079();
        if (iM9079 >= i2) {
            return;
        }
        int i3 = this.f15849;
        do {
            iM9079 += i3;
            i3 <<= 1;
        } while (iM9079 < i2);
        RandomAccessFile randomAccessFile = this.f15847;
        randomAccessFile.setLength(i3);
        randomAccessFile.getChannel().force(true);
        C4240 c4240 = this.f15851;
        int iM9073 = m9073(c4240.f16172 + 4 + c4240.f16171);
        if (iM9073 < this.f15850.f16172) {
            FileChannel channel = randomAccessFile.getChannel();
            channel.position(this.f15849);
            long j = iM9073 - 4;
            if (channel.transferTo(16L, j, channel) != j) {
                throw new AssertionError("Copied insufficient number of bytes!");
            }
        }
        int i4 = this.f15851.f16172;
        int i5 = this.f15850.f16172;
        if (i4 < i5) {
            int i6 = (this.f15849 + i4) - 16;
            m9071(i3, this.f15848, i5, i6);
            this.f15851 = new C4240(i6, this.f15851.f16171, 0);
        } else {
            m9071(i3, this.f15848, i5, i4);
        }
        this.f15849 = i3;
    }

    /* JADX INFO: renamed from: ᵨˈʼﹶ, reason: contains not printable characters */
    public final int m9079() {
        if (this.f15848 == 0) {
            return 16;
        }
        C4240 c4240 = this.f15851;
        int i = c4240.f16172;
        int i2 = this.f15850.f16172;
        return i >= i2 ? (i - i2) + 4 + c4240.f16171 + 16 : (((i + 4) + c4240.f16171) + this.f15849) - i2;
    }

    /* JADX INFO: renamed from: ᵱʡˏ, reason: contains not printable characters */
    public final synchronized void m9080(InterfaceC2067 interfaceC2067) {
        int iM9073 = this.f15850.f16172;
        for (int i = 0; i < this.f15848; i++) {
            C4240 c4240M9077 = m9077(iM9073);
            interfaceC2067.mo5510(new C2134(this, c4240M9077), c4240M9077.f16171);
            iM9073 = m9073(c4240M9077.f16172 + 4 + c4240M9077.f16171);
        }
    }

    /* JADX INFO: renamed from: ᵲᵶᵳˈ, reason: contains not printable characters */
    public final void m9081(byte[] bArr) {
        int iM9073;
        int length = bArr.length;
        synchronized (this) {
            if (length >= 0) {
                if (length <= bArr.length) {
                    m9078(length);
                    boolean zM9074 = m9074();
                    if (zM9074) {
                        iM9073 = 16;
                    } else {
                        C4240 c4240 = this.f15851;
                        iM9073 = m9073(c4240.f16172 + 4 + c4240.f16171);
                    }
                    C4240 c42402 = new C4240(iM9073, length, 0);
                    m9070(this.f15852, 0, length);
                    m9072(this.f15852, iM9073, 4);
                    m9072(bArr, iM9073 + 4, length);
                    m9071(this.f15849, this.f15848 + 1, zM9074 ? iM9073 : this.f15850.f16172, iM9073);
                    this.f15851 = c42402;
                    this.f15848++;
                    if (zM9074) {
                        this.f15850 = c42402;
                    }
                }
            }
            throw new IndexOutOfBoundsException();
        }
    }

    /* JADX INFO: renamed from: ᵸˆˁ, reason: contains not printable characters */
    public final synchronized void m9082() {
        try {
            if (m9074()) {
                throw new NoSuchElementException();
            }
            if (this.f15848 == 1) {
                m9075();
            } else {
                C4240 c4240 = this.f15850;
                int iM9073 = m9073(c4240.f16172 + 4 + c4240.f16171);
                m9076(iM9073, 0, 4, this.f15852);
                int iM9069 = m9069(0, this.f15852);
                m9071(this.f15849, this.f15848 - 1, iM9073, this.f15851.f16172);
                this.f15848--;
                this.f15850 = new C4240(iM9073, iM9069, 0);
            }
        } catch (Throwable th) {
            throw th;
        }
    }
}
