package d;

import java.util.Arrays;

/* JADX INFO: compiled from: Segment.kt */
/* JADX INFO: loaded from: classes.dex */
public final class v {

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

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

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

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

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

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public v f1611f;
    public v g;

    public v() {
        this.f1606a = new byte[8192];
        this.f1610e = true;
        this.f1609d = false;
    }

    public final void a() {
        int i = 0;
        if (!(this.g != this)) {
            throw new IllegalStateException("cannot compact".toString());
        }
        v vVar = this.g;
        if (vVar == null) {
            c.q.d.j.g();
            throw null;
        }
        if (vVar.f1610e) {
            int i2 = this.f1608c - this.f1607b;
            if (vVar == null) {
                c.q.d.j.g();
                throw null;
            }
            int i3 = 8192 - vVar.f1608c;
            if (vVar == null) {
                c.q.d.j.g();
                throw null;
            }
            if (!vVar.f1609d) {
                if (vVar == null) {
                    c.q.d.j.g();
                    throw null;
                }
                i = vVar.f1607b;
            }
            if (i2 > i3 + i) {
                return;
            }
            v vVar2 = this.g;
            if (vVar2 == null) {
                c.q.d.j.g();
                throw null;
            }
            g(vVar2, i2);
            b();
            w.a(this);
        }
    }

    public final v b() {
        v vVar = this.f1611f;
        if (vVar == this) {
            vVar = null;
        }
        v vVar2 = this.g;
        if (vVar2 == null) {
            c.q.d.j.g();
            throw null;
        }
        vVar2.f1611f = this.f1611f;
        v vVar3 = this.f1611f;
        if (vVar3 == null) {
            c.q.d.j.g();
            throw null;
        }
        vVar3.g = vVar2;
        this.f1611f = null;
        this.g = null;
        return vVar;
    }

    public final v c(v vVar) {
        c.q.d.j.c(vVar, "segment");
        vVar.g = this;
        vVar.f1611f = this.f1611f;
        v vVar2 = this.f1611f;
        if (vVar2 == null) {
            c.q.d.j.g();
            throw null;
        }
        vVar2.g = vVar;
        this.f1611f = vVar;
        return vVar;
    }

    public final v d() {
        this.f1609d = true;
        return new v(this.f1606a, this.f1607b, this.f1608c, true, false);
    }

    public final v e(int i) {
        v vVarD;
        if (!(i > 0 && i <= this.f1608c - this.f1607b)) {
            throw new IllegalArgumentException("byteCount out of range".toString());
        }
        if (i >= 1024) {
            vVarD = d();
        } else {
            v vVarB = w.b();
            b.a(this.f1606a, this.f1607b, vVarB.f1606a, 0, i);
            vVarD = vVarB;
        }
        vVarD.f1608c = vVarD.f1607b + i;
        this.f1607b += i;
        v vVar = this.g;
        if (vVar != null) {
            vVar.c(vVarD);
            return vVarD;
        }
        c.q.d.j.g();
        throw null;
    }

    public final v f() {
        byte[] bArr = this.f1606a;
        byte[] bArrCopyOf = Arrays.copyOf(bArr, bArr.length);
        c.q.d.j.b(bArrCopyOf, "java.util.Arrays.copyOf(this, size)");
        return new v(bArrCopyOf, this.f1607b, this.f1608c, false, true);
    }

    public final void g(v vVar, int i) {
        c.q.d.j.c(vVar, "sink");
        if (!vVar.f1610e) {
            throw new IllegalStateException("only owner can write".toString());
        }
        int i2 = vVar.f1608c;
        if (i2 + i > 8192) {
            if (vVar.f1609d) {
                throw new IllegalArgumentException();
            }
            int i3 = vVar.f1607b;
            if ((i2 + i) - i3 > 8192) {
                throw new IllegalArgumentException();
            }
            byte[] bArr = vVar.f1606a;
            b.a(bArr, i3, bArr, 0, i2 - i3);
            vVar.f1608c -= vVar.f1607b;
            vVar.f1607b = 0;
        }
        b.a(this.f1606a, this.f1607b, vVar.f1606a, vVar.f1608c, i);
        vVar.f1608c += i;
        this.f1607b += i;
    }

    public v(byte[] bArr, int i, int i2, boolean z, boolean z2) {
        c.q.d.j.c(bArr, "data");
        this.f1606a = bArr;
        this.f1607b = i;
        this.f1608c = i2;
        this.f1609d = z;
        this.f1610e = z2;
    }
}
