package ir;

import com.google.android.gms.dynamite.descriptors.com.google.android.gms.measurement.dynamite.ModuleDescriptor;
import java.io.Closeable;
import java.io.IOException;
import java.util.Random;
import kotlin.jvm.internal.t;
import kr.d;

/* loaded from: classes3.dex */
public final class h implements Closeable {
    private final d.a A;

    /* renamed from: p, reason: collision with root package name */
    private final boolean f23436p;

    /* renamed from: q, reason: collision with root package name */
    private final kr.e f23437q;

    /* renamed from: r, reason: collision with root package name */
    private final Random f23438r;

    /* renamed from: s, reason: collision with root package name */
    private final boolean f23439s;

    /* renamed from: t, reason: collision with root package name */
    private final boolean f23440t;

    /* renamed from: u, reason: collision with root package name */
    private final long f23441u;

    /* renamed from: v, reason: collision with root package name */
    private final kr.d f23442v;

    /* renamed from: w, reason: collision with root package name */
    private final kr.d f23443w;

    /* renamed from: x, reason: collision with root package name */
    private boolean f23444x;

    /* renamed from: y, reason: collision with root package name */
    private a f23445y;

    /* renamed from: z, reason: collision with root package name */
    private final byte[] f23446z;

    public h(boolean z10, kr.e sink, Random random, boolean z11, boolean z12, long j10) {
        t.g(sink, "sink");
        t.g(random, "random");
        this.f23436p = z10;
        this.f23437q = sink;
        this.f23438r = random;
        this.f23439s = z11;
        this.f23440t = z12;
        this.f23441u = j10;
        this.f23442v = new kr.d();
        this.f23443w = sink.k();
        this.f23446z = z10 ? new byte[4] : null;
        this.A = z10 ? new d.a() : null;
    }

    private final void b(int i10, kr.g gVar) {
        if (this.f23444x) {
            throw new IOException("closed");
        }
        int size = gVar.size();
        if (!(((long) size) <= 125)) {
            throw new IllegalArgumentException("Payload size must be less than or equal to 125".toString());
        }
        this.f23443w.F(i10 | 128);
        if (this.f23436p) {
            this.f23443w.F(size | 128);
            Random random = this.f23438r;
            byte[] bArr = this.f23446z;
            t.d(bArr);
            random.nextBytes(bArr);
            this.f23443w.z0(this.f23446z);
            if (size > 0) {
                long X = this.f23443w.X();
                this.f23443w.i0(gVar);
                kr.d dVar = this.f23443w;
                d.a aVar = this.A;
                t.d(aVar);
                dVar.z(aVar);
                this.A.d(X);
                f.f23424a.b(this.A, this.f23446z);
                this.A.close();
            }
        } else {
            this.f23443w.F(size);
            this.f23443w.i0(gVar);
        }
        this.f23437q.flush();
    }

    public final void a(int i10, kr.g gVar) {
        kr.g gVar2 = kr.g.f26650t;
        if (i10 != 0 || gVar != null) {
            if (i10 != 0) {
                f.f23424a.c(i10);
            }
            kr.d dVar = new kr.d();
            dVar.u(i10);
            if (gVar != null) {
                dVar.i0(gVar);
            }
            gVar2 = dVar.C();
        }
        try {
            b(8, gVar2);
        } finally {
            this.f23444x = true;
        }
    }

    public final void c(int i10, kr.g data) {
        t.g(data, "data");
        if (this.f23444x) {
            throw new IOException("closed");
        }
        this.f23442v.i0(data);
        int i11 = i10 | 128;
        if (this.f23439s && data.size() >= this.f23441u) {
            a aVar = this.f23445y;
            if (aVar == null) {
                aVar = new a(this.f23440t);
                this.f23445y = aVar;
            }
            aVar.a(this.f23442v);
            i11 |= 64;
        }
        long X = this.f23442v.X();
        this.f23443w.F(i11);
        int i12 = this.f23436p ? 128 : 0;
        if (X <= 125) {
            this.f23443w.F(((int) X) | i12);
        } else if (X <= 65535) {
            this.f23443w.F(i12 | 126);
            this.f23443w.u((int) X);
        } else {
            this.f23443w.F(i12 | ModuleDescriptor.MODULE_VERSION);
            this.f23443w.u0(X);
        }
        if (this.f23436p) {
            Random random = this.f23438r;
            byte[] bArr = this.f23446z;
            t.d(bArr);
            random.nextBytes(bArr);
            this.f23443w.z0(this.f23446z);
            if (X > 0) {
                kr.d dVar = this.f23442v;
                d.a aVar2 = this.A;
                t.d(aVar2);
                dVar.z(aVar2);
                this.A.d(0L);
                f.f23424a.b(this.A, this.f23446z);
                this.A.close();
            }
        }
        this.f23443w.U(this.f23442v, X);
        this.f23437q.i();
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public void close() {
        a aVar = this.f23445y;
        if (aVar != null) {
            aVar.close();
        }
    }

    public final void d(kr.g payload) {
        t.g(payload, "payload");
        b(9, payload);
    }

    public final void e(kr.g payload) {
        t.g(payload, "payload");
        b(10, payload);
    }
}
