package kr;

import com.google.firebase.crashlytics.internal.metadata.UserMetadata;
import java.util.Arrays;

/* loaded from: classes4.dex */
public final class v {

    /* renamed from: h, reason: collision with root package name */
    public static final a f26684h = new a(null);

    /* renamed from: a, reason: collision with root package name */
    public final byte[] f26685a;

    /* renamed from: b, reason: collision with root package name */
    public int f26686b;

    /* renamed from: c, reason: collision with root package name */
    public int f26687c;

    /* renamed from: d, reason: collision with root package name */
    public boolean f26688d;

    /* renamed from: e, reason: collision with root package name */
    public boolean f26689e;

    /* renamed from: f, reason: collision with root package name */
    public v f26690f;

    /* renamed from: g, reason: collision with root package name */
    public v f26691g;

    /* loaded from: classes4.dex */
    public static final class a {
        private a() {
        }

        public /* synthetic */ a(kotlin.jvm.internal.k kVar) {
            this();
        }
    }

    public v() {
        this.f26685a = new byte[UserMetadata.MAX_INTERNAL_KEY_SIZE];
        this.f26689e = true;
        this.f26688d = false;
    }

    public final void a() {
        v vVar = this.f26691g;
        int i10 = 0;
        if (!(vVar != this)) {
            throw new IllegalStateException("cannot compact".toString());
        }
        kotlin.jvm.internal.t.d(vVar);
        if (vVar.f26689e) {
            int i11 = this.f26687c - this.f26686b;
            v vVar2 = this.f26691g;
            kotlin.jvm.internal.t.d(vVar2);
            int i12 = 8192 - vVar2.f26687c;
            v vVar3 = this.f26691g;
            kotlin.jvm.internal.t.d(vVar3);
            if (!vVar3.f26688d) {
                v vVar4 = this.f26691g;
                kotlin.jvm.internal.t.d(vVar4);
                i10 = vVar4.f26686b;
            }
            if (i11 > i12 + i10) {
                return;
            }
            v vVar5 = this.f26691g;
            kotlin.jvm.internal.t.d(vVar5);
            g(vVar5, i11);
            b();
            w.b(this);
        }
    }

    public final v b() {
        v vVar = this.f26690f;
        if (vVar == this) {
            vVar = null;
        }
        v vVar2 = this.f26691g;
        kotlin.jvm.internal.t.d(vVar2);
        vVar2.f26690f = this.f26690f;
        v vVar3 = this.f26690f;
        kotlin.jvm.internal.t.d(vVar3);
        vVar3.f26691g = this.f26691g;
        this.f26690f = null;
        this.f26691g = null;
        return vVar;
    }

    public final v c(v segment) {
        kotlin.jvm.internal.t.g(segment, "segment");
        segment.f26691g = this;
        segment.f26690f = this.f26690f;
        v vVar = this.f26690f;
        kotlin.jvm.internal.t.d(vVar);
        vVar.f26691g = segment;
        this.f26690f = segment;
        return segment;
    }

    public final v d() {
        this.f26688d = true;
        return new v(this.f26685a, this.f26686b, this.f26687c, true, false);
    }

    public final v e(int i10) {
        v c10;
        if (!(i10 > 0 && i10 <= this.f26687c - this.f26686b)) {
            throw new IllegalArgumentException("byteCount out of range".toString());
        }
        if (i10 >= 1024) {
            c10 = d();
        } else {
            c10 = w.c();
            byte[] bArr = this.f26685a;
            byte[] bArr2 = c10.f26685a;
            int i11 = this.f26686b;
            sm.o.m(bArr, bArr2, 0, i11, i11 + i10, 2, null);
        }
        c10.f26687c = c10.f26686b + i10;
        this.f26686b += i10;
        v vVar = this.f26691g;
        kotlin.jvm.internal.t.d(vVar);
        vVar.c(c10);
        return c10;
    }

    public final v f() {
        byte[] bArr = this.f26685a;
        byte[] copyOf = Arrays.copyOf(bArr, bArr.length);
        kotlin.jvm.internal.t.f(copyOf, "copyOf(...)");
        return new v(copyOf, this.f26686b, this.f26687c, false, true);
    }

    public final void g(v sink, int i10) {
        kotlin.jvm.internal.t.g(sink, "sink");
        if (!sink.f26689e) {
            throw new IllegalStateException("only owner can write".toString());
        }
        int i11 = sink.f26687c;
        if (i11 + i10 > 8192) {
            if (sink.f26688d) {
                throw new IllegalArgumentException();
            }
            int i12 = sink.f26686b;
            if ((i11 + i10) - i12 > 8192) {
                throw new IllegalArgumentException();
            }
            byte[] bArr = sink.f26685a;
            sm.o.m(bArr, bArr, 0, i12, i11, 2, null);
            sink.f26687c -= sink.f26686b;
            sink.f26686b = 0;
        }
        byte[] bArr2 = this.f26685a;
        byte[] bArr3 = sink.f26685a;
        int i13 = sink.f26687c;
        int i14 = this.f26686b;
        sm.o.g(bArr2, bArr3, i13, i14, i14 + i10);
        sink.f26687c += i10;
        this.f26686b += i10;
    }

    public v(byte[] data, int i10, int i11, boolean z10, boolean z11) {
        kotlin.jvm.internal.t.g(data, "data");
        this.f26685a = data;
        this.f26686b = i10;
        this.f26687c = i11;
        this.f26688d = z10;
        this.f26689e = z11;
    }
}
