package o;

import com.google.android.gms.common.api.Api;
import java.io.Closeable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;

/* compiled from: r8-map-id-3b7cc9102578c509342fa2f0fabed2bfc72239cd258d579fc679bfcbc198b048 */
/* loaded from: classes.dex */
public final class SF implements Closeable {
    public static final Logger q = Logger.getLogger(AF.class.getName());
    public final InterfaceC1592jf l;
    public final C0732af m;
    public int n;

    /* renamed from: o, reason: collision with root package name */
    public boolean f327o;
    public final C2415sF p;

    public SF(InterfaceC1592jf interfaceC1592jf) {
        this.l = interfaceC1592jf;
        C0732af c0732af = new C0732af();
        this.m = c0732af;
        this.n = 16384;
        this.p = new C2415sF(c0732af);
    }

    public final synchronized void b(C2880x70 c2880x70) {
        try {
            if (this.f327o) {
                throw new IOException("closed");
            }
            int i = this.n;
            int i2 = c2880x70.a;
            if ((i2 & 32) != 0) {
                i = c2880x70.b[5];
            }
            this.n = i;
            if (((i2 & 2) != 0 ? c2880x70.b[1] : -1) != -1) {
                C2415sF c2415sF = this.p;
                int iMin = Math.min((i2 & 2) != 0 ? c2880x70.b[1] : -1, 16384);
                int i3 = c2415sF.d;
                if (i3 != iMin) {
                    if (iMin < i3) {
                        c2415sF.b = Math.min(c2415sF.b, iMin);
                    }
                    c2415sF.c = true;
                    c2415sF.d = iMin;
                    int i4 = c2415sF.h;
                    if (iMin < i4) {
                        if (iMin == 0) {
                            JE[] jeArr = c2415sF.e;
                            Arrays.fill(jeArr, 0, jeArr.length, (Object) null);
                            c2415sF.f = c2415sF.e.length - 1;
                            c2415sF.g = 0;
                            c2415sF.h = 0;
                        } else {
                            c2415sF.a(i4 - iMin);
                        }
                    }
                }
            }
            d(0, 0, 4, 1);
            this.l.flush();
        } catch (Throwable th) {
            throw th;
        }
    }

    public final synchronized void c(boolean z, int i, C0732af c0732af, int i2) {
        if (this.f327o) {
            throw new IOException("closed");
        }
        d(i, i2, 0, z ? 1 : 0);
        if (i2 > 0) {
            this.l.e(i2, c0732af);
        }
    }

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

    public final void d(int i, int i2, int i3, int i4) {
        Level level = Level.FINE;
        Logger logger = q;
        if (logger.isLoggable(level)) {
            logger.fine(AF.a(false, i, i2, i3, i4));
        }
        if (i2 > this.n) {
            throw new IllegalArgumentException(("FRAME_SIZE_ERROR length > " + this.n + ": " + i2).toString());
        }
        if ((Integer.MIN_VALUE & i) != 0) {
            throw new IllegalArgumentException(AbstractC0126Ew.j(i, "reserved bit set: ").toString());
        }
        byte[] bArr = AbstractC2264qi0.a;
        InterfaceC1592jf interfaceC1592jf = this.l;
        interfaceC1592jf.writeByte((i2 >>> 16) & 255);
        interfaceC1592jf.writeByte((i2 >>> 8) & 255);
        interfaceC1592jf.writeByte(i2 & 255);
        interfaceC1592jf.writeByte(i3 & 255);
        interfaceC1592jf.writeByte(i4 & 255);
        interfaceC1592jf.writeInt(i & Api.BaseClientBuilder.API_PRIORITY_OTHER);
    }

    public final synchronized void f(byte[] bArr, int i, int i2) {
        if (this.f327o) {
            throw new IOException("closed");
        }
        if (AbstractC2462sn.w(i2) == -1) {
            throw new IllegalArgumentException("errorCode.httpCode == -1");
        }
        d(0, bArr.length + 8, 7, 0);
        this.l.writeInt(i);
        this.l.writeInt(AbstractC2462sn.w(i2));
        if (bArr.length != 0) {
            this.l.write(bArr);
        }
        this.l.flush();
    }

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

    public final synchronized void g(boolean z, int i, ArrayList arrayList) {
        if (this.f327o) {
            throw new IOException("closed");
        }
        this.p.d(arrayList);
        long j = this.m.m;
        long jMin = Math.min(this.n, j);
        int i2 = j == jMin ? 4 : 0;
        if (z) {
            i2 |= 1;
        }
        d(i, (int) jMin, 1, i2);
        this.l.e(jMin, this.m);
        if (j > jMin) {
            long j2 = j - jMin;
            while (j2 > 0) {
                long jMin2 = Math.min(this.n, j2);
                j2 -= jMin2;
                d(i, (int) jMin2, 9, j2 == 0 ? 4 : 0);
                this.l.e(jMin2, this.m);
            }
        }
    }

    public final synchronized void j(int i, boolean z, int i2) {
        if (this.f327o) {
            throw new IOException("closed");
        }
        d(0, 8, 6, z ? 1 : 0);
        this.l.writeInt(i);
        this.l.writeInt(i2);
        this.l.flush();
    }

    public final synchronized void l(int i, int i2) {
        if (this.f327o) {
            throw new IOException("closed");
        }
        if (AbstractC2462sn.w(i2) == -1) {
            throw new IllegalArgumentException("Failed requirement.");
        }
        d(i, 4, 3, 0);
        this.l.writeInt(AbstractC2462sn.w(i2));
        this.l.flush();
    }

    public final synchronized void m(int i, long j) {
        if (this.f327o) {
            throw new IOException("closed");
        }
        if (j == 0 || j > 2147483647L) {
            throw new IllegalArgumentException(("windowSizeIncrement == 0 || windowSizeIncrement > 0x7fffffffL: " + j).toString());
        }
        d(i, 4, 8, 0);
        this.l.writeInt((int) j);
        this.l.flush();
    }
}
