package okio;

import java.util.Arrays;
import o1.AbstractC0668d;

/* JADX INFO: loaded from: classes2.dex */
public final class y {

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

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

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

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

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

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

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

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

    public static final class a {
        private a() {
        }

        public /* synthetic */ a(kotlin.jvm.internal.g gVar) {
            this();
        }
    }

    public y() {
        this.f6071a = new byte[8192];
        this.f6075e = true;
        this.f6074d = false;
    }

    public final void a() {
        y yVar = this.f6077g;
        int i2 = 0;
        if (!(yVar != this)) {
            throw new IllegalStateException("cannot compact");
        }
        kotlin.jvm.internal.l.b(yVar);
        if (yVar.f6075e) {
            int i3 = this.f6073c - this.f6072b;
            y yVar2 = this.f6077g;
            kotlin.jvm.internal.l.b(yVar2);
            int i4 = 8192 - yVar2.f6073c;
            y yVar3 = this.f6077g;
            kotlin.jvm.internal.l.b(yVar3);
            if (!yVar3.f6074d) {
                y yVar4 = this.f6077g;
                kotlin.jvm.internal.l.b(yVar4);
                i2 = yVar4.f6072b;
            }
            if (i3 > i4 + i2) {
                return;
            }
            y yVar5 = this.f6077g;
            kotlin.jvm.internal.l.b(yVar5);
            g(yVar5, i3);
            b();
            A.b(this);
        }
    }

    public final y b() {
        y yVar = this.f6076f;
        if (yVar == this) {
            yVar = null;
        }
        y yVar2 = this.f6077g;
        kotlin.jvm.internal.l.b(yVar2);
        yVar2.f6076f = this.f6076f;
        y yVar3 = this.f6076f;
        kotlin.jvm.internal.l.b(yVar3);
        yVar3.f6077g = this.f6077g;
        this.f6076f = null;
        this.f6077g = null;
        return yVar;
    }

    public final y c(y segment) {
        kotlin.jvm.internal.l.e(segment, "segment");
        segment.f6077g = this;
        segment.f6076f = this.f6076f;
        y yVar = this.f6076f;
        kotlin.jvm.internal.l.b(yVar);
        yVar.f6077g = segment;
        this.f6076f = segment;
        return segment;
    }

    public final y d() {
        this.f6074d = true;
        return new y(this.f6071a, this.f6072b, this.f6073c, true, false);
    }

    public final y e(int i2) {
        y yVarC;
        if (!(i2 > 0 && i2 <= this.f6073c - this.f6072b)) {
            throw new IllegalArgumentException("byteCount out of range");
        }
        if (i2 >= 1024) {
            yVarC = d();
        } else {
            yVarC = A.c();
            byte[] bArr = this.f6071a;
            byte[] bArr2 = yVarC.f6071a;
            int i3 = this.f6072b;
            AbstractC0668d.e(bArr, bArr2, 0, i3, i3 + i2, 2, null);
        }
        yVarC.f6073c = yVarC.f6072b + i2;
        this.f6072b += i2;
        y yVar = this.f6077g;
        kotlin.jvm.internal.l.b(yVar);
        yVar.c(yVarC);
        return yVarC;
    }

    public final y f() {
        byte[] bArr = this.f6071a;
        byte[] bArrCopyOf = Arrays.copyOf(bArr, bArr.length);
        kotlin.jvm.internal.l.d(bArrCopyOf, "java.util.Arrays.copyOf(this, size)");
        return new y(bArrCopyOf, this.f6072b, this.f6073c, false, true);
    }

    public final void g(y sink, int i2) {
        kotlin.jvm.internal.l.e(sink, "sink");
        if (!sink.f6075e) {
            throw new IllegalStateException("only owner can write");
        }
        int i3 = sink.f6073c;
        if (i3 + i2 > 8192) {
            if (sink.f6074d) {
                throw new IllegalArgumentException();
            }
            int i4 = sink.f6072b;
            if ((i3 + i2) - i4 > 8192) {
                throw new IllegalArgumentException();
            }
            byte[] bArr = sink.f6071a;
            AbstractC0668d.e(bArr, bArr, 0, i4, i3, 2, null);
            sink.f6073c -= sink.f6072b;
            sink.f6072b = 0;
        }
        byte[] bArr2 = this.f6071a;
        byte[] bArr3 = sink.f6071a;
        int i5 = sink.f6073c;
        int i6 = this.f6072b;
        AbstractC0668d.c(bArr2, bArr3, i5, i6, i6 + i2);
        sink.f6073c += i2;
        this.f6072b += i2;
    }

    public y(byte[] data, int i2, int i3, boolean z2, boolean z3) {
        kotlin.jvm.internal.l.e(data, "data");
        this.f6071a = data;
        this.f6072b = i2;
        this.f6073c = i3;
        this.f6074d = z2;
        this.f6075e = z3;
    }
}
