package okhttp3.internal.ws;

import g.h0.c.l;
import j.d;
import j.f;
import j.h;
import java.io.Closeable;
import java.io.IOException;
import java.util.Random;

/* JADX INFO: compiled from: WebSocketWriter.kt */
/* JADX INFO: loaded from: classes.dex */
public final class WebSocketWriter implements Closeable {
    private final boolean isClient;
    private final d.a maskCursor;
    private final byte[] maskKey;
    private final d messageBuffer;
    private MessageDeflater messageDeflater;
    private final long minimumDeflateSize;
    private final boolean noContextTakeover;
    private final boolean perMessageDeflate;
    private final Random random;
    private final f sink;
    private final d sinkBuffer;
    private boolean writerClosed;

    public WebSocketWriter(boolean z, f fVar, Random random, boolean z2, boolean z3, long j2) {
        l.f(fVar, "sink");
        l.f(random, "random");
        this.isClient = z;
        this.sink = fVar;
        this.random = random;
        this.perMessageDeflate = z2;
        this.noContextTakeover = z3;
        this.minimumDeflateSize = j2;
        this.messageBuffer = new d();
        this.sinkBuffer = fVar.getBuffer();
        this.maskKey = z ? new byte[4] : null;
        this.maskCursor = z ? new d.a() : null;
    }

    private final void writeControlFrame(int i2, h hVar) throws IOException {
        if (this.writerClosed) {
            throw new IOException("closed");
        }
        int iC = hVar.c();
        if (!(((long) iC) <= 125)) {
            throw new IllegalArgumentException("Payload size must be less than or equal to 125".toString());
        }
        this.sinkBuffer.D(i2 | 128);
        if (this.isClient) {
            this.sinkBuffer.D(iC | 128);
            Random random = this.random;
            byte[] bArr = this.maskKey;
            l.c(bArr);
            random.nextBytes(bArr);
            this.sinkBuffer.z(this.maskKey);
            if (iC > 0) {
                d dVar = this.sinkBuffer;
                long j2 = dVar.f4601e;
                dVar.y(hVar);
                d dVar2 = this.sinkBuffer;
                d.a aVar = this.maskCursor;
                l.c(aVar);
                dVar2.m(aVar);
                this.maskCursor.b(j2);
                WebSocketProtocol.INSTANCE.toggleMask(this.maskCursor, this.maskKey);
                this.maskCursor.close();
            }
        } else {
            this.sinkBuffer.D(iC);
            this.sinkBuffer.y(hVar);
        }
        this.sink.flush();
    }

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

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

    public final f getSink() {
        return this.sink;
    }

    public final void writeClose(int i2, h hVar) throws IOException {
        h hVarI = h.f4611b;
        if (i2 != 0 || hVar != null) {
            if (i2 != 0) {
                WebSocketProtocol.INSTANCE.validateCloseCode(i2);
            }
            d dVar = new d();
            dVar.N(i2);
            if (hVar != null) {
                dVar.y(hVar);
            }
            hVarI = dVar.I();
        }
        try {
            writeControlFrame(8, hVarI);
        } finally {
            this.writerClosed = true;
        }
    }

    public final void writeMessageFrame(int i2, h hVar) throws IOException {
        l.f(hVar, "data");
        if (this.writerClosed) {
            throw new IOException("closed");
        }
        this.messageBuffer.y(hVar);
        int i3 = i2 | 128;
        if (this.perMessageDeflate && hVar.c() >= this.minimumDeflateSize) {
            MessageDeflater messageDeflater = this.messageDeflater;
            if (messageDeflater == null) {
                messageDeflater = new MessageDeflater(this.noContextTakeover);
                this.messageDeflater = messageDeflater;
            }
            messageDeflater.deflate(this.messageBuffer);
            i3 |= 64;
        }
        long j2 = this.messageBuffer.f4601e;
        this.sinkBuffer.D(i3);
        int i4 = this.isClient ? 128 : 0;
        if (j2 <= 125) {
            this.sinkBuffer.D(((int) j2) | i4);
        } else if (j2 <= WebSocketProtocol.PAYLOAD_SHORT_MAX) {
            this.sinkBuffer.D(i4 | 126);
            this.sinkBuffer.N((int) j2);
        } else {
            this.sinkBuffer.D(i4 | 127);
            this.sinkBuffer.L(j2);
        }
        if (this.isClient) {
            Random random = this.random;
            byte[] bArr = this.maskKey;
            l.c(bArr);
            random.nextBytes(bArr);
            this.sinkBuffer.z(this.maskKey);
            if (j2 > 0) {
                d dVar = this.messageBuffer;
                d.a aVar = this.maskCursor;
                l.c(aVar);
                dVar.m(aVar);
                this.maskCursor.b(0L);
                WebSocketProtocol.INSTANCE.toggleMask(this.maskCursor, this.maskKey);
                this.maskCursor.close();
            }
        }
        this.sinkBuffer.write(this.messageBuffer, j2);
        this.sink.emit();
    }

    public final void writePing(h hVar) throws IOException {
        l.f(hVar, "payload");
        writeControlFrame(9, hVar);
    }

    public final void writePong(h hVar) throws IOException {
        l.f(hVar, "payload");
        writeControlFrame(10, hVar);
    }
}
