package p138.p176.p291.p296.p297.p300;

import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.util.NoSuchElementException;
import java.util.logging.Level;
import java.util.logging.Logger;
import p138.p139.p143.p144.C2803;

/* JADX INFO: renamed from: ދ.ހ.ށ.ބ.ށ.ވ.ހ, reason: contains not printable characters */
/* JADX INFO: loaded from: classes.dex */
public class C5705 implements Closeable {

    /* JADX INFO: renamed from: މ, reason: contains not printable characters */
    public static final Logger f16306 = Logger.getLogger(C5705.class.getName());

    /* JADX INFO: renamed from: ރ, reason: contains not printable characters */
    public final RandomAccessFile f16307;

    /* JADX INFO: renamed from: ބ, reason: contains not printable characters */
    public int f16308;

    /* JADX INFO: renamed from: ޅ, reason: contains not printable characters */
    public int f16309;

    /* JADX INFO: renamed from: ކ, reason: contains not printable characters */
    public C5707 f16310;

    /* JADX INFO: renamed from: އ, reason: contains not printable characters */
    public C5707 f16311;

    /* JADX INFO: renamed from: ވ, reason: contains not printable characters */
    public final byte[] f16312 = new byte[16];

    /* JADX INFO: renamed from: ދ.ހ.ށ.ބ.ށ.ވ.ހ$֏, reason: contains not printable characters */
    public class C5706 implements InterfaceC5709 {

        /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
        public boolean f16313 = true;

        /* JADX INFO: renamed from: ؠ, reason: contains not printable characters */
        public final /* synthetic */ StringBuilder f16314;

        public C5706(C5705 c5705, StringBuilder sb) {
            this.f16314 = sb;
        }

        @Override // p138.p176.p291.p296.p297.p300.C5705.InterfaceC5709
        /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
        public void mo9093(InputStream inputStream, int i) {
            if (this.f16313) {
                this.f16313 = false;
            } else {
                this.f16314.append(", ");
            }
            this.f16314.append(i);
        }
    }

    /* JADX INFO: renamed from: ދ.ހ.ށ.ބ.ށ.ވ.ހ$ؠ, reason: contains not printable characters */
    public static class C5707 {

        /* JADX INFO: renamed from: ހ, reason: contains not printable characters */
        public static final C5707 f16315 = new C5707(0, 0);

        /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
        public final int f16316;

        /* JADX INFO: renamed from: ؠ, reason: contains not printable characters */
        public final int f16317;

        public C5707(int i, int i2) {
            this.f16316 = i;
            this.f16317 = i2;
        }

        public String toString() {
            StringBuilder sb = new StringBuilder();
            sb.append(C5707.class.getSimpleName());
            sb.append("[position = ");
            sb.append(this.f16316);
            sb.append(", length = ");
            return C2803.m4882(sb, this.f16317, "]");
        }
    }

    /* JADX INFO: renamed from: ދ.ހ.ށ.ބ.ށ.ވ.ހ$ހ, reason: contains not printable characters */
    public final class C5708 extends InputStream {

        /* JADX INFO: renamed from: ރ, reason: contains not printable characters */
        public int f16318;

        /* JADX INFO: renamed from: ބ, reason: contains not printable characters */
        public int f16319;

        public /* synthetic */ C5708(C5707 c5707, C5706 c5706) {
            int i = c5707.f16316 + 4;
            int i2 = C5705.this.f16308;
            this.f16318 = i >= i2 ? (i + 16) - i2 : i;
            this.f16319 = c5707.f16317;
        }

        @Override // java.io.InputStream
        public int read() throws IOException {
            if (this.f16319 == 0) {
                return -1;
            }
            C5705.this.f16307.seek(this.f16318);
            int i = C5705.this.f16307.read();
            this.f16318 = C5705.m9078(C5705.this, this.f16318 + 1);
            this.f16319--;
            return i;
        }

        @Override // java.io.InputStream
        public int read(byte[] bArr, int i, int i2) throws IOException {
            if (bArr == null) {
                throw new NullPointerException("buffer");
            }
            if ((i | i2) < 0 || i2 > bArr.length - i) {
                throw new ArrayIndexOutOfBoundsException();
            }
            int i3 = this.f16319;
            if (i3 <= 0) {
                return -1;
            }
            if (i2 > i3) {
                i2 = i3;
            }
            C5705.this.m9084(this.f16318, bArr, i, i2);
            this.f16318 = C5705.m9078(C5705.this, this.f16318 + i2);
            this.f16319 -= i2;
            return i2;
        }
    }

    /* JADX INFO: renamed from: ދ.ހ.ށ.ބ.ށ.ވ.ހ$ށ, reason: contains not printable characters */
    public interface InterfaceC5709 {
        /* JADX INFO: renamed from: ֏ */
        void mo9093(InputStream inputStream, int i);
    }

    public C5705(File file) throws IOException {
        if (!file.exists()) {
            File file2 = new File(file.getPath() + ".tmp");
            RandomAccessFile randomAccessFile = new RandomAccessFile(file2, "rwd");
            try {
                randomAccessFile.setLength(4096L);
                randomAccessFile.seek(0L);
                byte[] bArr = new byte[16];
                int[] iArr = {4096, 0, 0, 0};
                int i = 0;
                for (int i2 = 0; i2 < 4; i2++) {
                    m9080(bArr, i, iArr[i2]);
                    i += 4;
                }
                randomAccessFile.write(bArr);
                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.f16307 = randomAccessFile2;
        randomAccessFile2.seek(0L);
        this.f16307.readFully(this.f16312);
        int iM9079 = m9079(this.f16312, 0);
        this.f16308 = iM9079;
        if (iM9079 > this.f16307.length()) {
            StringBuilder sbM4889 = C2803.m4889("File is truncated. Expected length: ");
            sbM4889.append(this.f16308);
            sbM4889.append(", Actual length: ");
            sbM4889.append(this.f16307.length());
            throw new IOException(sbM4889.toString());
        }
        this.f16309 = m9079(this.f16312, 4);
        int iM90792 = m9079(this.f16312, 8);
        int iM90793 = m9079(this.f16312, 12);
        this.f16310 = m9087(iM90792);
        this.f16311 = m9087(iM90793);
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public static /* synthetic */ int m9078(C5705 c5705, int i) {
        int i2 = c5705.f16308;
        return i < i2 ? i : (i + 16) - i2;
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public static int m9079(byte[] bArr, int i) {
        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 m9080(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 synchronized void close() {
        this.f16307.close();
    }

    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(C5705.class.getSimpleName());
        sb.append('[');
        sb.append("fileLength=");
        sb.append(this.f16308);
        sb.append(", size=");
        sb.append(this.f16309);
        sb.append(", first=");
        sb.append(this.f16310);
        sb.append(", last=");
        sb.append(this.f16311);
        sb.append(", element lengths=[");
        try {
            m9085(new C5706(this, sb));
        } catch (IOException e) {
            f16306.log(Level.WARNING, "read error", (Throwable) e);
        }
        sb.append("]]");
        return sb.toString();
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public synchronized void m9081() {
        m9083(4096, 0, 0, 0);
        this.f16309 = 0;
        this.f16310 = C5707.f16315;
        this.f16311 = C5707.f16315;
        if (this.f16308 > 4096) {
            this.f16307.setLength(4096);
            this.f16307.getChannel().force(true);
        }
        this.f16308 = 4096;
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public final void m9082(int i) throws IOException {
        int i2 = i + 4;
        int iM9092 = this.f16308 - m9092();
        if (iM9092 >= i2) {
            return;
        }
        int i3 = this.f16308;
        do {
            iM9092 += i3;
            i3 <<= 1;
        } while (iM9092 < i2);
        this.f16307.setLength(i3);
        this.f16307.getChannel().force(true);
        C5707 c5707 = this.f16311;
        int iM9090 = m9090(c5707.f16316 + 4 + c5707.f16317);
        if (iM9090 < this.f16310.f16316) {
            FileChannel channel = this.f16307.getChannel();
            channel.position(this.f16308);
            long j = iM9090 - 4;
            if (channel.transferTo(16L, j, channel) != j) {
                throw new AssertionError("Copied insufficient number of bytes!");
            }
        }
        int i4 = this.f16311.f16316;
        int i5 = this.f16310.f16316;
        if (i4 < i5) {
            int i6 = (this.f16308 + i4) - 16;
            m9083(i3, this.f16309, i5, i6);
            this.f16311 = new C5707(i6, this.f16311.f16317);
        } else {
            m9083(i3, this.f16309, i5, i4);
        }
        this.f16308 = i3;
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public final void m9083(int i, int i2, int i3, int i4) throws IOException {
        byte[] bArr = this.f16312;
        int[] iArr = {i, i2, i3, i4};
        int i5 = 0;
        for (int i6 = 0; i6 < 4; i6++) {
            m9080(bArr, i5, iArr[i6]);
            i5 += 4;
        }
        this.f16307.seek(0L);
        this.f16307.write(this.f16312);
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public final void m9084(int i, byte[] bArr, int i2, int i3) throws IOException {
        int i4 = this.f16308;
        if (i >= i4) {
            i = (i + 16) - i4;
        }
        int i5 = i + i3;
        int i6 = this.f16308;
        if (i5 <= i6) {
            this.f16307.seek(i);
            this.f16307.readFully(bArr, i2, i3);
            return;
        }
        int i7 = i6 - i;
        this.f16307.seek(i);
        this.f16307.readFully(bArr, i2, i7);
        this.f16307.seek(16L);
        this.f16307.readFully(bArr, i2 + i7, i3 - i7);
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public synchronized void m9085(InterfaceC5709 interfaceC5709) {
        int iM9090 = this.f16310.f16316;
        for (int i = 0; i < this.f16309; i++) {
            C5707 c5707M9087 = m9087(iM9090);
            interfaceC5709.mo9093(new C5708(c5707M9087, null), c5707M9087.f16317);
            iM9090 = m9090(c5707M9087.f16316 + 4 + c5707M9087.f16317);
        }
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public synchronized void m9086(byte[] bArr, int i, int i2) {
        if (bArr == null) {
            throw new NullPointerException("buffer");
        }
        if ((i | i2) < 0 || i2 > bArr.length - i) {
            throw new IndexOutOfBoundsException();
        }
        m9082(i2);
        boolean zM9089 = m9089();
        C5707 c5707 = new C5707(zM9089 ? 16 : m9090(this.f16311.f16316 + 4 + this.f16311.f16317), i2);
        m9080(this.f16312, 0, i2);
        m9088(c5707.f16316, this.f16312, 0, 4);
        m9088(c5707.f16316 + 4, bArr, i, i2);
        m9083(this.f16308, this.f16309 + 1, zM9089 ? c5707.f16316 : this.f16310.f16316, c5707.f16316);
        this.f16311 = c5707;
        this.f16309++;
        if (zM9089) {
            this.f16310 = c5707;
        }
    }

    /* JADX INFO: renamed from: ؠ, reason: contains not printable characters */
    public final C5707 m9087(int i) throws IOException {
        if (i == 0) {
            return C5707.f16315;
        }
        this.f16307.seek(i);
        return new C5707(i, this.f16307.readInt());
    }

    /* JADX INFO: renamed from: ؠ, reason: contains not printable characters */
    public final void m9088(int i, byte[] bArr, int i2, int i3) throws IOException {
        int i4 = this.f16308;
        if (i >= i4) {
            i = (i + 16) - i4;
        }
        int i5 = i + i3;
        int i6 = this.f16308;
        if (i5 <= i6) {
            this.f16307.seek(i);
            this.f16307.write(bArr, i2, i3);
            return;
        }
        int i7 = i6 - i;
        this.f16307.seek(i);
        this.f16307.write(bArr, i2, i7);
        this.f16307.seek(16L);
        this.f16307.write(bArr, i2 + i7, i3 - i7);
    }

    /* JADX INFO: renamed from: ؠ, reason: contains not printable characters */
    public synchronized boolean m9089() {
        return this.f16309 == 0;
    }

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

    /* JADX INFO: renamed from: ހ, reason: contains not printable characters */
    public synchronized void m9091() {
        if (m9089()) {
            throw new NoSuchElementException();
        }
        if (this.f16309 == 1) {
            m9081();
        } else {
            int iM9090 = m9090(this.f16310.f16316 + 4 + this.f16310.f16317);
            m9084(iM9090, this.f16312, 0, 4);
            int iM9079 = m9079(this.f16312, 0);
            m9083(this.f16308, this.f16309 - 1, iM9090, this.f16311.f16316);
            this.f16309--;
            this.f16310 = new C5707(iM9090, iM9079);
        }
    }

    /* JADX INFO: renamed from: ރ, reason: contains not printable characters */
    public int m9092() {
        if (this.f16309 == 0) {
            return 16;
        }
        C5707 c5707 = this.f16311;
        int i = c5707.f16316;
        int i2 = this.f16310.f16316;
        return i >= i2 ? (i - i2) + 4 + c5707.f16317 + 16 : (((i + 4) + c5707.f16317) + this.f16308) - i2;
    }
}
