package g.i0.i;

import com.seewo.libmcuservice.constant.IMcuConstants;
import com.seewo.vcommons.constants.Platform;
import com.seewo.vcommons.mstar.MKeyEvent;
import g.i0.i.d;
import java.io.Closeable;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;

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

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

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final h.g f6362c;

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

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final h.f f6364e;

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

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

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public final d.b f6367h;

    public q(h.g gVar, boolean z) {
        this.f6362c = gVar;
        this.f6363d = z;
        h.f fVar = new h.f();
        this.f6364e = fVar;
        this.f6367h = new d.b(fVar);
        this.f6365f = 16384;
    }

    public void B(boolean z, int i2, List<c> list) throws IOException {
        if (this.f6366g) {
            throw new IOException("closed");
        }
        this.f6367h.e(list);
        long j2 = this.f6364e.f6529d;
        int iMin = (int) Math.min(this.f6365f, j2);
        long j3 = iMin;
        byte b2 = j2 == j3 ? (byte) 4 : (byte) 0;
        if (z) {
            b2 = (byte) (b2 | 1);
        }
        o(i2, iMin, (byte) 1, b2);
        this.f6362c.h(this.f6364e, j3);
        if (j2 > j3) {
            M(i2, j2 - j3);
        }
    }

    public synchronized void E(boolean z, int i2, int i3) {
        if (this.f6366g) {
            throw new IOException("closed");
        }
        o(0, 8, (byte) 6, z ? (byte) 1 : (byte) 0);
        this.f6362c.d(i2);
        this.f6362c.d(i3);
        this.f6362c.flush();
    }

    public synchronized void K(int i2, b bVar) {
        if (this.f6366g) {
            throw new IOException("closed");
        }
        if (bVar.httpCode == -1) {
            throw new IllegalArgumentException();
        }
        o(i2, 4, (byte) 3, (byte) 0);
        this.f6362c.d(bVar.httpCode);
        this.f6362c.flush();
    }

    public synchronized void L(int i2, long j2) {
        if (this.f6366g) {
            throw new IOException("closed");
        }
        if (j2 == 0 || j2 > 2147483647L) {
            e.b("windowSizeIncrement == 0 || windowSizeIncrement > 0x7fffffffL: %s", Long.valueOf(j2));
            throw null;
        }
        o(i2, 4, (byte) 8, (byte) 0);
        this.f6362c.d((int) j2);
        this.f6362c.flush();
    }

    public final void M(int i2, long j2) {
        while (j2 > 0) {
            int iMin = (int) Math.min(this.f6365f, j2);
            long j3 = iMin;
            j2 -= j3;
            o(i2, iMin, (byte) 9, j2 == 0 ? (byte) 4 : (byte) 0);
            this.f6362c.h(this.f6364e, j3);
        }
    }

    public synchronized void a(t tVar) {
        if (this.f6366g) {
            throw new IOException("closed");
        }
        int i2 = this.f6365f;
        int i3 = tVar.f6376a;
        if ((i3 & 32) != 0) {
            i2 = tVar.f6377b[5];
        }
        this.f6365f = i2;
        int i4 = i3 & 2;
        if ((i4 != 0 ? tVar.f6377b[1] : -1) != -1) {
            d.b bVar = this.f6367h;
            int i5 = i4 != 0 ? tVar.f6377b[1] : -1;
            Objects.requireNonNull(bVar);
            int iMin = Math.min(i5, 16384);
            int i6 = bVar.f6263d;
            if (i6 != iMin) {
                if (iMin < i6) {
                    bVar.f6261b = Math.min(bVar.f6261b, iMin);
                }
                bVar.f6262c = true;
                bVar.f6263d = iMin;
                int i7 = bVar.f6267h;
                if (iMin < i7) {
                    if (iMin == 0) {
                        bVar.a();
                    } else {
                        bVar.b(i7 - iMin);
                    }
                }
            }
        }
        o(0, 0, (byte) 4, (byte) 1);
        this.f6362c.flush();
    }

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

    public synchronized void flush() {
        if (this.f6366g) {
            throw new IOException("closed");
        }
        this.f6362c.flush();
    }

    public synchronized void i(boolean z, int i2, h.f fVar, int i3) {
        if (this.f6366g) {
            throw new IOException("closed");
        }
        o(i2, i3, (byte) 0, z ? (byte) 1 : (byte) 0);
        if (i3 > 0) {
            this.f6362c.h(fVar, i3);
        }
    }

    public void o(int i2, int i3, byte b2, byte b3) {
        Logger logger = f6361b;
        if (logger.isLoggable(Level.FINE)) {
            logger.fine(e.a(false, i2, i3, b2, b3));
        }
        int i4 = this.f6365f;
        if (i3 > i4) {
            e.b("FRAME_SIZE_ERROR length > %d: %d", Integer.valueOf(i4), Integer.valueOf(i3));
            throw null;
        }
        if ((Integer.MIN_VALUE & i2) != 0) {
            e.b("reserved bit set: %s", Integer.valueOf(i2));
            throw null;
        }
        h.g gVar = this.f6362c;
        gVar.r((i3 >>> 16) & MKeyEvent.KEYCODE_SLEEP);
        gVar.r((i3 >>> 8) & MKeyEvent.KEYCODE_SLEEP);
        gVar.r(i3 & MKeyEvent.KEYCODE_SLEEP);
        this.f6362c.r(b2 & IMcuConstants.DEFAULT_VALUE);
        this.f6362c.r(b3 & IMcuConstants.DEFAULT_VALUE);
        this.f6362c.d(i2 & Platform.UNKNOWN);
    }

    public synchronized void z(int i2, b bVar, byte[] bArr) {
        if (this.f6366g) {
            throw new IOException("closed");
        }
        if (bVar.httpCode == -1) {
            e.b("errorCode.httpCode == -1", new Object[0]);
            throw null;
        }
        o(0, bArr.length + 8, (byte) 7, (byte) 0);
        this.f6362c.d(i2);
        this.f6362c.d(bVar.httpCode);
        if (bArr.length > 0) {
            this.f6362c.f(bArr);
        }
        this.f6362c.flush();
    }
}
