package okhttp3.internal.http2;

import java.io.Closeable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import kotlin.jvm.internal.o;
import okhttp3.internal.Util;
import okhttp3.internal.http2.Hpack;
import p5.C2740j;
import p5.I;
import x4.AbstractC3250p;

/* JADX INFO: loaded from: classes3.dex */
public final class Http2Writer implements Closeable {

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public static final Logger f22577f;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final I f22578a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final C2740j f22579b;

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

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

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final Hpack.Writer f22582e;

    public static final class Companion {
        public /* synthetic */ Companion(int i6) {
            this();
        }

        private Companion() {
        }
    }

    static {
        new Companion(0);
        f22577f = Logger.getLogger(Http2.class.getName());
    }

    public Http2Writer(I sink) {
        o.h(sink, "sink");
        this.f22578a = sink;
        C2740j c2740j = new C2740j();
        this.f22579b = c2740j;
        this.f22580c = 16384;
        this.f22582e = new Hpack.Writer(c2740j);
    }

    public final synchronized void a(Settings peerSettings) {
        try {
            o.h(peerSettings, "peerSettings");
            if (this.f22581d) {
                throw new IOException("closed");
            }
            int i6 = this.f22580c;
            int i7 = peerSettings.f22592a;
            if ((i7 & 32) != 0) {
                i6 = peerSettings.f22593b[5];
            }
            this.f22580c = i6;
            if (((i7 & 2) != 0 ? peerSettings.f22593b[1] : -1) != -1) {
                Hpack.Writer writer = this.f22582e;
                int i8 = (i7 & 2) != 0 ? peerSettings.f22593b[1] : -1;
                writer.getClass();
                int iMin = Math.min(i8, 16384);
                int i9 = writer.f22458d;
                if (i9 != iMin) {
                    if (iMin < i9) {
                        writer.f22456b = Math.min(writer.f22456b, iMin);
                    }
                    writer.f22457c = true;
                    writer.f22458d = iMin;
                    int i10 = writer.f22462h;
                    if (iMin < i10) {
                        if (iMin == 0) {
                            Header[] headerArr = writer.f22459e;
                            AbstractC3250p.H(headerArr, null, 0, headerArr.length);
                            writer.f22460f = writer.f22459e.length - 1;
                            writer.f22461g = 0;
                            writer.f22462h = 0;
                        } else {
                            writer.a(i10 - iMin);
                        }
                    }
                }
            }
            c(0, 0, 4, 1);
            this.f22578a.flush();
        } catch (Throwable th) {
            throw th;
        }
    }

    public final synchronized void b(boolean z6, int i6, C2740j c2740j, int i7) {
        if (this.f22581d) {
            throw new IOException("closed");
        }
        c(i6, i7, 0, z6 ? 1 : 0);
        if (i7 > 0) {
            o.e(c2740j);
            this.f22578a.write(c2740j, i7);
        }
    }

    public final void c(int i6, int i7, int i8, int i9) {
        Level level = Level.FINE;
        Logger logger = f22577f;
        if (logger.isLoggable(level)) {
            Http2.f22463a.getClass();
            logger.fine(Http2.a(false, i6, i7, i8, i9));
        }
        if (i7 > this.f22580c) {
            throw new IllegalArgumentException(("FRAME_SIZE_ERROR length > " + this.f22580c + ": " + i7).toString());
        }
        if ((Integer.MIN_VALUE & i6) != 0) {
            throw new IllegalArgumentException(Z0.o.l("reserved bit set: ", i6).toString());
        }
        byte[] bArr = Util.f22179a;
        I i10 = this.f22578a;
        o.h(i10, "<this>");
        i10.h((i7 >>> 16) & 255);
        i10.h((i7 >>> 8) & 255);
        i10.h(i7 & 255);
        i10.h(i8 & 255);
        i10.h(i9 & 255);
        i10.b(i6 & Integer.MAX_VALUE);
    }

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

    public final synchronized void d(int i6, ErrorCode errorCode, byte[] bArr) {
        if (this.f22581d) {
            throw new IOException("closed");
        }
        if (errorCode.f22435a == -1) {
            throw new IllegalArgumentException("errorCode.httpCode == -1");
        }
        c(0, bArr.length + 8, 7, 0);
        this.f22578a.b(i6);
        this.f22578a.b(errorCode.f22435a);
        if (bArr.length != 0) {
            this.f22578a.u(bArr);
        }
        this.f22578a.flush();
    }

    public final synchronized void f(boolean z6, int i6, ArrayList arrayList) {
        if (this.f22581d) {
            throw new IOException("closed");
        }
        this.f22582e.d(arrayList);
        long j4 = this.f22579b.f23100b;
        long jMin = Math.min(this.f22580c, j4);
        int i7 = j4 == jMin ? 4 : 0;
        if (z6) {
            i7 |= 1;
        }
        c(i6, (int) jMin, 1, i7);
        this.f22578a.write(this.f22579b, jMin);
        if (j4 > jMin) {
            long j6 = j4 - jMin;
            while (j6 > 0) {
                long jMin2 = Math.min(this.f22580c, j6);
                j6 -= jMin2;
                c(i6, (int) jMin2, 9, j6 == 0 ? 4 : 0);
                this.f22578a.write(this.f22579b, jMin2);
            }
        }
    }

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

    public final synchronized void g(int i6, int i7, boolean z6) {
        if (this.f22581d) {
            throw new IOException("closed");
        }
        c(0, 8, 6, z6 ? 1 : 0);
        this.f22578a.b(i6);
        this.f22578a.b(i7);
        this.f22578a.flush();
    }

    public final synchronized void i(int i6, ErrorCode errorCode) {
        if (this.f22581d) {
            throw new IOException("closed");
        }
        if (errorCode.f22435a == -1) {
            throw new IllegalArgumentException("Failed requirement.");
        }
        c(i6, 4, 3, 0);
        this.f22578a.b(errorCode.f22435a);
        this.f22578a.flush();
    }

    public final synchronized void t(int i6, long j4) {
        if (this.f22581d) {
            throw new IOException("closed");
        }
        if (j4 == 0 || j4 > 2147483647L) {
            throw new IllegalArgumentException(("windowSizeIncrement == 0 || windowSizeIncrement > 0x7fffffffL: " + j4).toString());
        }
        c(i6, 4, 8, 0);
        this.f22578a.b((int) j4);
        this.f22578a.flush();
    }
}
