package R4;

import e4.AbstractC0564h;
import java.io.Closeable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import r4.AbstractC0983h;

/* JADX INFO: loaded from: classes.dex */
public final class z implements Closeable {
    public static final Logger p = Logger.getLogger(g.class.getName());

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public final X4.n f2767k;

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public final X4.e f2768l;

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

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

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    public final C0186e f2771o;

    public z(X4.n nVar) {
        AbstractC0983h.e("sink", nVar);
        this.f2767k = nVar;
        X4.e eVar = new X4.e();
        this.f2768l = eVar;
        this.f2769m = 16384;
        this.f2771o = new C0186e(eVar);
    }

    public final synchronized void a(D d3) {
        try {
            AbstractC0983h.e("peerSettings", d3);
            if (this.f2770n) {
                throw new IOException("closed");
            }
            int i = this.f2769m;
            int i5 = d3.f2645a;
            if ((i5 & 32) != 0) {
                i = d3.f2646b[5];
            }
            this.f2769m = i;
            if (((i5 & 2) != 0 ? d3.f2646b[1] : -1) != -1) {
                C0186e c0186e = this.f2771o;
                int i6 = (i5 & 2) != 0 ? d3.f2646b[1] : -1;
                c0186e.getClass();
                int iMin = Math.min(i6, 16384);
                int i7 = c0186e.f2672d;
                if (i7 != iMin) {
                    if (iMin < i7) {
                        c0186e.f2670b = Math.min(c0186e.f2670b, iMin);
                    }
                    c0186e.f2671c = true;
                    c0186e.f2672d = iMin;
                    int i8 = c0186e.f2676h;
                    if (iMin < i8) {
                        if (iMin == 0) {
                            C0184c[] c0184cArr = c0186e.f2673e;
                            AbstractC0564h.R(c0184cArr, 0, c0184cArr.length);
                            c0186e.f2674f = c0186e.f2673e.length - 1;
                            c0186e.f2675g = 0;
                            c0186e.f2676h = 0;
                        } else {
                            c0186e.a(i8 - iMin);
                        }
                    }
                }
            }
            f(0, 0, 4, 1);
            this.f2767k.flush();
        } catch (Throwable th) {
            throw th;
        }
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public final synchronized void close() {
        this.f2770n = true;
        this.f2767k.close();
    }

    public final synchronized void e(boolean z5, int i, X4.e eVar, int i5) {
        if (this.f2770n) {
            throw new IOException("closed");
        }
        f(i, i5, 0, z5 ? 1 : 0);
        if (i5 > 0) {
            AbstractC0983h.b(eVar);
            this.f2767k.q(eVar, i5);
        }
    }

    public final void f(int i, int i5, int i6, int i7) {
        Level level = Level.FINE;
        Logger logger = p;
        if (logger.isLoggable(level)) {
            logger.fine(g.a(false, i, i5, i6, i7));
        }
        if (i5 > this.f2769m) {
            throw new IllegalArgumentException(("FRAME_SIZE_ERROR length > " + this.f2769m + ": " + i5).toString());
        }
        if ((Integer.MIN_VALUE & i) != 0) {
            throw new IllegalArgumentException(AbstractC0983h.h("reserved bit set: ", Integer.valueOf(i)).toString());
        }
        byte[] bArr = L4.b.f2100a;
        X4.n nVar = this.f2767k;
        AbstractC0983h.e("<this>", nVar);
        nVar.f((i5 >>> 16) & 255);
        nVar.f((i5 >>> 8) & 255);
        nVar.f(i5 & 255);
        nVar.f(i6 & 255);
        nVar.f(i7 & 255);
        nVar.h(i & Integer.MAX_VALUE);
    }

    public final synchronized void flush() {
        if (this.f2770n) {
            throw new IOException("closed");
        }
        this.f2767k.flush();
    }

    public final synchronized void h(int i, EnumC0183b enumC0183b, byte[] bArr) {
        if (this.f2770n) {
            throw new IOException("closed");
        }
        if (enumC0183b.f2653k == -1) {
            throw new IllegalArgumentException("errorCode.httpCode == -1");
        }
        f(0, bArr.length + 8, 7, 0);
        this.f2767k.h(i);
        this.f2767k.h(enumC0183b.f2653k);
        if (bArr.length != 0) {
            X4.n nVar = this.f2767k;
            if (nVar.f3340m) {
                throw new IllegalStateException("closed");
            }
            nVar.f3339l.v(bArr.length, bArr);
            nVar.a();
        }
        this.f2767k.flush();
    }

    public final synchronized void k(boolean z5, int i, ArrayList arrayList) {
        if (this.f2770n) {
            throw new IOException("closed");
        }
        this.f2771o.d(arrayList);
        long j5 = this.f2768l.f3321l;
        long jMin = Math.min(this.f2769m, j5);
        int i5 = j5 == jMin ? 4 : 0;
        if (z5) {
            i5 |= 1;
        }
        f(i, (int) jMin, 1, i5);
        this.f2767k.q(this.f2768l, jMin);
        if (j5 > jMin) {
            long j6 = j5 - jMin;
            while (j6 > 0) {
                long jMin2 = Math.min(this.f2769m, j6);
                j6 -= jMin2;
                f(i, (int) jMin2, 9, j6 == 0 ? 4 : 0);
                this.f2767k.q(this.f2768l, jMin2);
            }
        }
    }

    public final synchronized void l(int i, boolean z5, int i5) {
        if (this.f2770n) {
            throw new IOException("closed");
        }
        f(0, 8, 6, z5 ? 1 : 0);
        this.f2767k.h(i);
        this.f2767k.h(i5);
        this.f2767k.flush();
    }

    public final synchronized void m(int i, EnumC0183b enumC0183b) {
        if (this.f2770n) {
            throw new IOException("closed");
        }
        if (enumC0183b.f2653k == -1) {
            throw new IllegalArgumentException("Failed requirement.");
        }
        f(i, 4, 3, 0);
        this.f2767k.h(enumC0183b.f2653k);
        this.f2767k.flush();
    }

    public final synchronized void n(long j5, int i) {
        if (this.f2770n) {
            throw new IOException("closed");
        }
        if (j5 == 0 || j5 > 2147483647L) {
            throw new IllegalArgumentException(AbstractC0983h.h("windowSizeIncrement == 0 || windowSizeIncrement > 0x7fffffffL: ", Long.valueOf(j5)).toString());
        }
        f(i, 4, 8, 0);
        this.f2767k.h((int) j5);
        this.f2767k.flush();
    }
}
