package i;

import com.cvte.vman.instance.InvokeCmd;
import java.util.Arrays;

/* JADX INFO: compiled from: Segment.kt */
/* JADX INFO: loaded from: classes.dex */
public final class y {
    public final byte[] a;
    public int b;

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

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

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

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public y f1056f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public y f1057g;

    public y() {
        this.a = new byte[InvokeCmd.CMD_TOUCH_CTRL_START];
        this.f1055e = true;
        this.f1054d = false;
    }

    public final void a() {
        int i2 = 0;
        if (!(this.f1057g != this)) {
            throw new IllegalStateException("cannot compact".toString());
        }
        y yVar = this.f1057g;
        h.x.d.k.b(yVar);
        if (yVar.f1055e) {
            int i3 = this.f1053c - this.b;
            y yVar2 = this.f1057g;
            h.x.d.k.b(yVar2);
            int i4 = 8192 - yVar2.f1053c;
            y yVar3 = this.f1057g;
            h.x.d.k.b(yVar3);
            if (!yVar3.f1054d) {
                y yVar4 = this.f1057g;
                h.x.d.k.b(yVar4);
                i2 = yVar4.b;
            }
            if (i3 > i4 + i2) {
                return;
            }
            y yVar5 = this.f1057g;
            h.x.d.k.b(yVar5);
            g(yVar5, i3);
            b();
            z.b(this);
        }
    }

    public final y b() {
        y yVar = this.f1056f;
        if (yVar == this) {
            yVar = null;
        }
        y yVar2 = this.f1057g;
        h.x.d.k.b(yVar2);
        yVar2.f1056f = this.f1056f;
        y yVar3 = this.f1056f;
        h.x.d.k.b(yVar3);
        yVar3.f1057g = this.f1057g;
        this.f1056f = null;
        this.f1057g = null;
        return yVar;
    }

    public final y c(y yVar) {
        h.x.d.k.d(yVar, "segment");
        yVar.f1057g = this;
        yVar.f1056f = this.f1056f;
        y yVar2 = this.f1056f;
        h.x.d.k.b(yVar2);
        yVar2.f1057g = yVar;
        this.f1056f = yVar;
        return yVar;
    }

    public final y d() {
        this.f1054d = true;
        return new y(this.a, this.b, this.f1053c, true, false);
    }

    public final y e(int i2) {
        y yVarC;
        if (!(i2 > 0 && i2 <= this.f1053c - this.b)) {
            throw new IllegalArgumentException("byteCount out of range".toString());
        }
        if (i2 >= 1024) {
            yVarC = d();
        } else {
            yVarC = z.c();
            byte[] bArr = this.a;
            byte[] bArr2 = yVarC.a;
            int i3 = this.b;
            h.s.d.e(bArr, bArr2, 0, i3, i3 + i2, 2, null);
        }
        yVarC.f1053c = yVarC.b + i2;
        this.b += i2;
        y yVar = this.f1057g;
        h.x.d.k.b(yVar);
        yVar.c(yVarC);
        return yVarC;
    }

    public final y f() {
        byte[] bArr = this.a;
        byte[] bArrCopyOf = Arrays.copyOf(bArr, bArr.length);
        h.x.d.k.c(bArrCopyOf, "java.util.Arrays.copyOf(this, size)");
        return new y(bArrCopyOf, this.b, this.f1053c, false, true);
    }

    public final void g(y yVar, int i2) {
        h.x.d.k.d(yVar, "sink");
        if (!yVar.f1055e) {
            throw new IllegalStateException("only owner can write".toString());
        }
        int i3 = yVar.f1053c;
        if (i3 + i2 > 8192) {
            if (yVar.f1054d) {
                throw new IllegalArgumentException();
            }
            int i4 = yVar.b;
            if ((i3 + i2) - i4 > 8192) {
                throw new IllegalArgumentException();
            }
            byte[] bArr = yVar.a;
            h.s.d.e(bArr, bArr, 0, i4, i3, 2, null);
            yVar.f1053c -= yVar.b;
            yVar.b = 0;
        }
        byte[] bArr2 = this.a;
        byte[] bArr3 = yVar.a;
        int i5 = yVar.f1053c;
        int i6 = this.b;
        h.s.d.c(bArr2, bArr3, i5, i6, i6 + i2);
        yVar.f1053c += i2;
        this.b += i2;
    }

    public y(byte[] bArr, int i2, int i3, boolean z, boolean z2) {
        h.x.d.k.d(bArr, "data");
        this.a = bArr;
        this.b = i2;
        this.f1053c = i3;
        this.f1054d = z;
        this.f1055e = z2;
    }
}
