package okhttp3.internal.ws;

import androidx.media3.container.NalUnitUtil;
import androidx.media3.extractor.ts.PsExtractor;
import defpackage.cr4;
import defpackage.kp6;
import defpackage.lg0;
import defpackage.pq6;
import defpackage.rh0;
import defpackage.vx8;
import defpackage.zo0;
import java.io.Closeable;
import java.io.IOException;
import java.util.Random;
import kotlin.Metadata;

/* JADX INFO: loaded from: classes3.dex */
@Metadata(d1 = {"\u0000X\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0010\u000b\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0010\t\n\u0002\b\u0003\n\u0002\u0010\b\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0015\n\u0002\u0018\u0002\n\u0002\b\u0004\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010\u0012\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0003\u0018\u00002\u00020\u0001B7\u0012\u0006\u0010\u0003\u001a\u00020\u0002\u0012\u0006\u0010\u0005\u001a\u00020\u0004\u0012\u0006\u0010\u0007\u001a\u00020\u0006\u0012\u0006\u0010\b\u001a\u00020\u0002\u0012\u0006\u0010\t\u001a\u00020\u0002\u0012\u0006\u0010\u000b\u001a\u00020\n¢\u0006\u0004\b\f\u0010\rJ\u001f\u0010\u0013\u001a\u00020\u00122\u0006\u0010\u000f\u001a\u00020\u000e2\u0006\u0010\u0011\u001a\u00020\u0010H\u0002¢\u0006\u0004\b\u0013\u0010\u0014J\u0015\u0010\u0015\u001a\u00020\u00122\u0006\u0010\u0011\u001a\u00020\u0010¢\u0006\u0004\b\u0015\u0010\u0016J\u0015\u0010\u0017\u001a\u00020\u00122\u0006\u0010\u0011\u001a\u00020\u0010¢\u0006\u0004\b\u0017\u0010\u0016J\u001f\u0010\u001a\u001a\u00020\u00122\u0006\u0010\u0018\u001a\u00020\u000e2\b\u0010\u0019\u001a\u0004\u0018\u00010\u0010¢\u0006\u0004\b\u001a\u0010\u0014J\u001d\u0010\u001d\u001a\u00020\u00122\u0006\u0010\u001b\u001a\u00020\u000e2\u0006\u0010\u001c\u001a\u00020\u0010¢\u0006\u0004\b\u001d\u0010\u0014J\u000f\u0010\u001e\u001a\u00020\u0012H\u0016¢\u0006\u0004\b\u001e\u0010\u001fR\u0014\u0010\u0003\u001a\u00020\u00028\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b\u0003\u0010 R\u0017\u0010\u0005\u001a\u00020\u00048\u0006¢\u0006\f\n\u0004\b\u0005\u0010!\u001a\u0004\b\"\u0010#R\u0017\u0010\u0007\u001a\u00020\u00068\u0006¢\u0006\f\n\u0004\b\u0007\u0010$\u001a\u0004\b%\u0010&R\u0014\u0010\b\u001a\u00020\u00028\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b\b\u0010 R\u0014\u0010\t\u001a\u00020\u00028\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b\t\u0010 R\u0014\u0010\u000b\u001a\u00020\n8\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b\u000b\u0010'R\u0014\u0010)\u001a\u00020(8\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b)\u0010*R\u0014\u0010+\u001a\u00020(8\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b+\u0010*R\u0016\u0010,\u001a\u00020\u00028\u0002@\u0002X\u0082\u000e¢\u0006\u0006\n\u0004\b,\u0010 R\u0018\u0010.\u001a\u0004\u0018\u00010-8\u0002@\u0002X\u0082\u000e¢\u0006\u0006\n\u0004\b.\u0010/R\u0016\u00101\u001a\u0004\u0018\u0001008\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b1\u00102R\u0016\u00104\u001a\u0004\u0018\u0001038\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b4\u00105¨\u00066"}, d2 = {"Lokhttp3/internal/ws/WebSocketWriter;", "Ljava/io/Closeable;", "", "isClient", "Lrh0;", "sink", "Ljava/util/Random;", "random", "perMessageDeflate", "noContextTakeover", "", "minimumDeflateSize", "<init>", "(ZLrh0;Ljava/util/Random;ZZJ)V", "", "opcode", "Lzo0;", "payload", "Le6a;", "writeControlFrame", "(ILzo0;)V", "writePing", "(Lzo0;)V", "writePong", "code", "reason", "writeClose", "formatOpcode", "data", "writeMessageFrame", "close", "()V", "Z", "Lrh0;", "getSink", "()Lrh0;", "Ljava/util/Random;", "getRandom", "()Ljava/util/Random;", "J", "Llg0;", "messageBuffer", "Llg0;", "sinkBuffer", "writerClosed", "Lokhttp3/internal/ws/MessageDeflater;", "messageDeflater", "Lokhttp3/internal/ws/MessageDeflater;", "", "maskKey", "[B", "Llg0$a;", "maskCursor", "Llg0$a;", "okhttp"}, k = 1, mv = {1, 8, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
@vx8
public final class WebSocketWriter implements Closeable {
    private final boolean isClient;

    @pq6
    private final lg0.a maskCursor;

    @pq6
    private final byte[] maskKey;

    @kp6
    private final lg0 messageBuffer;

    @pq6
    private MessageDeflater messageDeflater;
    private final long minimumDeflateSize;
    private final boolean noContextTakeover;
    private final boolean perMessageDeflate;

    @kp6
    private final Random random;

    @kp6
    private final rh0 sink;

    @kp6
    private final lg0 sinkBuffer;
    private boolean writerClosed;

    public WebSocketWriter(boolean z, @kp6 rh0 rh0Var, @kp6 Random random, boolean z2, boolean z3, long j) {
        cr4.g(rh0Var, "sink");
        cr4.g(random, "random");
        this.isClient = z;
        this.sink = rh0Var;
        this.random = random;
        this.perMessageDeflate = z2;
        this.noContextTakeover = z3;
        this.minimumDeflateSize = j;
        this.messageBuffer = new lg0();
        this.sinkBuffer = rh0Var.getB();
        this.maskKey = z ? new byte[4] : null;
        this.maskCursor = z ? new lg0.a() : null;
    }

    private final void writeControlFrame(int opcode, zo0 payload) throws IOException {
        if (this.writerClosed) {
            throw new IOException("closed");
        }
        int iE = payload.e();
        if (iE > 125) {
            throw new IllegalArgumentException("Payload size must be less than or equal to 125");
        }
        this.sinkBuffer.ak(opcode | 128);
        if (this.isClient) {
            this.sinkBuffer.ak(iE | 128);
            Random random = this.random;
            byte[] bArr = this.maskKey;
            cr4.d(bArr);
            random.nextBytes(bArr);
            this.sinkBuffer.m177write(this.maskKey);
            if (iE > 0) {
                lg0 lg0Var = this.sinkBuffer;
                long j = lg0Var.b;
                lg0Var.ah(payload);
                lg0 lg0Var2 = this.sinkBuffer;
                lg0.a aVar = this.maskCursor;
                cr4.d(aVar);
                lg0Var2.p(aVar);
                this.maskCursor.b(j);
                WebSocketProtocol.INSTANCE.toggleMask(this.maskCursor, this.maskKey);
                this.maskCursor.close();
            }
        } else {
            this.sinkBuffer.ak(iE);
            this.sinkBuffer.ah(payload);
        }
        this.sink.flush();
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public void close() throws Throwable {
        MessageDeflater messageDeflater = this.messageDeflater;
        if (messageDeflater != null) {
            messageDeflater.close();
        }
    }

    @kp6
    public final Random getRandom() {
        return this.random;
    }

    @kp6
    public final rh0 getSink() {
        return this.sink;
    }

    public final void writeClose(int code, @pq6 zo0 reason) throws IOException {
        zo0 zo0VarBb = zo0.d;
        if (code != 0 || reason != null) {
            if (code != 0) {
                WebSocketProtocol.INSTANCE.validateCloseCode(code);
            }
            lg0 lg0Var = new lg0();
            lg0Var.ap(code);
            if (reason != null) {
                lg0Var.ah(reason);
            }
            zo0VarBb = lg0Var.bb(lg0Var.b);
        }
        try {
            writeControlFrame(8, zo0VarBb);
        } finally {
            this.writerClosed = true;
        }
    }

    public final void writeMessageFrame(int formatOpcode, @kp6 zo0 data) throws IOException {
        cr4.g(data, "data");
        if (this.writerClosed) {
            throw new IOException("closed");
        }
        this.messageBuffer.ah(data);
        int i = formatOpcode | 128;
        if (this.perMessageDeflate && data.e() >= this.minimumDeflateSize) {
            MessageDeflater messageDeflater = this.messageDeflater;
            if (messageDeflater == null) {
                messageDeflater = new MessageDeflater(this.noContextTakeover);
                this.messageDeflater = messageDeflater;
            }
            messageDeflater.deflate(this.messageBuffer);
            i = formatOpcode | PsExtractor.AUDIO_STREAM;
        }
        long j = this.messageBuffer.b;
        this.sinkBuffer.ak(i);
        int i2 = this.isClient ? 128 : 0;
        if (j <= 125) {
            this.sinkBuffer.ak(i2 | ((int) j));
        } else if (j <= WebSocketProtocol.PAYLOAD_SHORT_MAX) {
            this.sinkBuffer.ak(i2 | WebSocketProtocol.PAYLOAD_SHORT);
            this.sinkBuffer.ap((int) j);
        } else {
            this.sinkBuffer.ak(i2 | 127);
            this.sinkBuffer.writeLong(j);
        }
        if (this.isClient) {
            Random random = this.random;
            byte[] bArr = this.maskKey;
            cr4.d(bArr);
            random.nextBytes(bArr);
            this.sinkBuffer.m177write(this.maskKey);
            if (j > 0) {
                lg0 lg0Var = this.messageBuffer;
                lg0.a aVar = this.maskCursor;
                cr4.d(aVar);
                lg0Var.p(aVar);
                this.maskCursor.b(0L);
                WebSocketProtocol.INSTANCE.toggleMask(this.maskCursor, this.maskKey);
                this.maskCursor.close();
            }
        }
        this.sinkBuffer.write(this.messageBuffer, j);
        this.sink.q();
    }

    public final void writePing(@kp6 zo0 payload) throws IOException {
        cr4.g(payload, "payload");
        writeControlFrame(9, payload);
    }

    public final void writePong(@kp6 zo0 payload) throws IOException {
        cr4.g(payload, "payload");
        writeControlFrame(10, payload);
    }
}
