package r7;

import d5.AbstractC0844a;
import java.io.Closeable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import u.AbstractC1563h;
import x7.C;
import x7.C1731g;

/* JADX INFO: loaded from: classes.dex */
public final class x implements Closeable, AutoCloseable {

    /* JADX INFO: renamed from: v, reason: collision with root package name */
    public static final Logger f16698v = Logger.getLogger(f.class.getName());

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public final x7.A f16699q;

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    public final C1731g f16700r;

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

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

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    public final d f16703u;

    public x(x7.A a3) {
        P6.g.e(a3, "sink");
        this.f16699q = a3;
        C1731g c1731g = new C1731g();
        this.f16700r = c1731g;
        this.f16701s = 16384;
        this.f16703u = new d(c1731g);
    }

    public final synchronized void A(long j8, int i) {
        if (this.f16702t) {
            throw new IOException("closed");
        }
        if (j8 == 0 || j8 > 2147483647L) {
            throw new IllegalArgumentException(("windowSizeIncrement == 0 || windowSizeIncrement > 0x7fffffffL: " + j8).toString());
        }
        g(i, 4, 8, 0);
        this.f16699q.b((int) j8);
        this.f16699q.flush();
    }

    public final synchronized void a(A a3) {
        try {
            P6.g.e(a3, "peerSettings");
            if (this.f16702t) {
                throw new IOException("closed");
            }
            int i = this.f16701s;
            int i5 = a3.f16580a;
            if ((i5 & 32) != 0) {
                i = a3.f16581b[5];
            }
            this.f16701s = i;
            if (((i5 & 2) != 0 ? a3.f16581b[1] : -1) != -1) {
                d dVar = this.f16703u;
                int i8 = (i5 & 2) != 0 ? a3.f16581b[1] : -1;
                dVar.getClass();
                int iMin = Math.min(i8, 16384);
                int i9 = dVar.f16601d;
                if (i9 != iMin) {
                    if (iMin < i9) {
                        dVar.f16599b = Math.min(dVar.f16599b, iMin);
                    }
                    dVar.f16600c = true;
                    dVar.f16601d = iMin;
                    int i10 = dVar.f16605h;
                    if (iMin < i10) {
                        if (iMin == 0) {
                            C1512b[] c1512bArr = dVar.f16602e;
                            B6.j.L(c1512bArr, 0, c1512bArr.length);
                            dVar.f16603f = dVar.f16602e.length - 1;
                            dVar.f16604g = 0;
                            dVar.f16605h = 0;
                        } else {
                            dVar.a(i10 - iMin);
                        }
                    }
                }
            }
            g(0, 0, 4, 1);
            this.f16699q.flush();
        } catch (Throwable th) {
            throw th;
        }
    }

    public final synchronized void b(boolean z8, int i, C1731g c1731g, int i5) {
        if (this.f16702t) {
            throw new IOException("closed");
        }
        g(i, i5, 0, z8 ? 1 : 0);
        if (i5 > 0) {
            P6.g.b(c1731g);
            this.f16699q.X(c1731g, i5);
        }
    }

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

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

    public final void g(int i, int i5, int i8, int i9) {
        Level level = Level.FINE;
        Logger logger = f16698v;
        if (logger.isLoggable(level)) {
            logger.fine(f.a(false, i, i5, i8, i9));
        }
        if (i5 > this.f16701s) {
            throw new IllegalArgumentException(("FRAME_SIZE_ERROR length > " + this.f16701s + ": " + i5).toString());
        }
        if ((Integer.MIN_VALUE & i) != 0) {
            throw new IllegalArgumentException(AbstractC0844a.j("reserved bit set: ", i).toString());
        }
        byte[] bArr = l7.b.f14412a;
        x7.A a3 = this.f16699q;
        P6.g.e(a3, "<this>");
        a3.d0((i5 >>> 16) & 255);
        a3.d0((i5 >>> 8) & 255);
        a3.d0(i5 & 255);
        a3.d0(i8 & 255);
        a3.d0(i9 & 255);
        a3.b(i & Integer.MAX_VALUE);
    }

    public final synchronized void j(byte[] bArr, int i, int i5) {
        I1.a.t(i5, "errorCode");
        if (this.f16702t) {
            throw new IOException("closed");
        }
        if (AbstractC1563h.c(i5) == -1) {
            throw new IllegalArgumentException("errorCode.httpCode == -1");
        }
        g(0, bArr.length + 8, 7, 0);
        this.f16699q.b(i);
        this.f16699q.b(AbstractC1563h.c(i5));
        if (bArr.length != 0) {
            this.f16699q.h(bArr);
        }
        this.f16699q.flush();
    }

    public final synchronized void m(boolean z8, int i, ArrayList arrayList) {
        if (this.f16702t) {
            throw new IOException("closed");
        }
        this.f16703u.d(arrayList);
        long j8 = this.f16700r.f18765r;
        long jMin = Math.min(this.f16701s, j8);
        int i5 = j8 == jMin ? 4 : 0;
        if (z8) {
            i5 |= 1;
        }
        g(i, (int) jMin, 1, i5);
        this.f16699q.X(this.f16700r, jMin);
        if (j8 > jMin) {
            long j9 = j8 - jMin;
            while (j9 > 0) {
                long jMin2 = Math.min(this.f16701s, j9);
                j9 -= jMin2;
                g(i, (int) jMin2, 9, j9 == 0 ? 4 : 0);
                this.f16699q.X(this.f16700r, jMin2);
            }
        }
    }

    public final synchronized void n(int i, int i5, boolean z8) {
        if (this.f16702t) {
            throw new IOException("closed");
        }
        g(0, 8, 6, z8 ? 1 : 0);
        this.f16699q.b(i);
        this.f16699q.b(i5);
        this.f16699q.flush();
    }

    public final synchronized void r(int i, int i5) {
        I1.a.t(i5, "errorCode");
        if (this.f16702t) {
            throw new IOException("closed");
        }
        if (AbstractC1563h.c(i5) == -1) {
            throw new IllegalArgumentException("Failed requirement.");
        }
        g(i, 4, 3, 0);
        this.f16699q.b(AbstractC1563h.c(i5));
        this.f16699q.flush();
    }

    public final synchronized void u(A a3) {
        try {
            P6.g.e(a3, "settings");
            if (this.f16702t) {
                throw new IOException("closed");
            }
            g(0, Integer.bitCount(a3.f16580a) * 6, 4, 0);
            int i = 0;
            while (i < 10) {
                boolean z8 = true;
                if (((1 << i) & a3.f16580a) == 0) {
                    z8 = false;
                }
                if (z8) {
                    int i5 = i != 4 ? i != 7 ? i : 4 : 3;
                    x7.A a8 = this.f16699q;
                    if (a8.f18723s) {
                        throw new IllegalStateException("closed");
                    }
                    C1731g c1731g = a8.f18722r;
                    C cQ0 = c1731g.q0(2);
                    int i8 = cQ0.f18729c;
                    byte[] bArr = cQ0.f18727a;
                    bArr[i8] = (byte) ((i5 >>> 8) & 255);
                    bArr[i8 + 1] = (byte) (i5 & 255);
                    cQ0.f18729c = i8 + 2;
                    c1731g.f18765r += 2;
                    a8.a();
                    this.f16699q.b(a3.f16581b[i]);
                }
                i++;
            }
            this.f16699q.flush();
        } catch (Throwable th) {
            throw th;
        }
    }
}
