package okhttp3.internal.ws;

import c.q.d.j;
import d.b0;
import d.f;
import d.g;
import d.i;
import d.y;
import java.io.IOException;
import java.util.Random;
import org.eclipse.paho.android.service.MqttServiceConstants;

/* JADX INFO: compiled from: WebSocketWriter.kt */
/* JADX INFO: loaded from: classes.dex */
public final class WebSocketWriter {
    private boolean activeWriter;
    private final f buffer;
    private final FrameSink frameSink;
    private final boolean isClient;
    private final f.a maskCursor;
    private final byte[] maskKey;
    private final Random random;
    private final g sink;
    private final f sinkBuffer;
    private boolean writerClosed;

    /* JADX INFO: compiled from: WebSocketWriter.kt */
    public final class FrameSink implements y {
        private boolean closed;
        private long contentLength;
        private int formatOpcode;
        private boolean isFirstFrame;

        public FrameSink() {
        }

        @Override // d.y, java.io.Closeable, java.lang.AutoCloseable
        public void close() throws IOException {
            if (this.closed) {
                throw new IOException("closed");
            }
            WebSocketWriter webSocketWriter = WebSocketWriter.this;
            webSocketWriter.writeMessageFrame(this.formatOpcode, webSocketWriter.getBuffer().b0(), this.isFirstFrame, true);
            this.closed = true;
            WebSocketWriter.this.setActiveWriter(false);
        }

        @Override // d.y, java.io.Flushable
        public void flush() throws IOException {
            if (this.closed) {
                throw new IOException("closed");
            }
            WebSocketWriter webSocketWriter = WebSocketWriter.this;
            webSocketWriter.writeMessageFrame(this.formatOpcode, webSocketWriter.getBuffer().b0(), this.isFirstFrame, false);
            this.isFirstFrame = false;
        }

        public final boolean getClosed() {
            return this.closed;
        }

        public final long getContentLength() {
            return this.contentLength;
        }

        public final int getFormatOpcode() {
            return this.formatOpcode;
        }

        public final boolean isFirstFrame() {
            return this.isFirstFrame;
        }

        public final void setClosed(boolean z) {
            this.closed = z;
        }

        public final void setContentLength(long j) {
            this.contentLength = j;
        }

        public final void setFirstFrame(boolean z) {
            this.isFirstFrame = z;
        }

        public final void setFormatOpcode(int i) {
            this.formatOpcode = i;
        }

        @Override // d.y
        public b0 timeout() {
            return WebSocketWriter.this.getSink().timeout();
        }

        @Override // d.y
        public void write(f fVar, long j) throws IOException {
            j.c(fVar, "source");
            if (this.closed) {
                throw new IOException("closed");
            }
            WebSocketWriter.this.getBuffer().write(fVar, j);
            boolean z = this.isFirstFrame && this.contentLength != -1 && WebSocketWriter.this.getBuffer().b0() > this.contentLength - ((long) 8192);
            long jG = WebSocketWriter.this.getBuffer().G();
            if (jG <= 0 || z) {
                return;
            }
            WebSocketWriter.this.writeMessageFrame(this.formatOpcode, jG, this.isFirstFrame, false);
            this.isFirstFrame = false;
        }
    }

    public WebSocketWriter(boolean z, g gVar, Random random) {
        j.c(gVar, "sink");
        j.c(random, "random");
        this.isClient = z;
        this.sink = gVar;
        this.random = random;
        this.sinkBuffer = gVar.a();
        this.buffer = new f();
        this.frameSink = new FrameSink();
        this.maskKey = this.isClient ? new byte[4] : null;
        this.maskCursor = this.isClient ? new f.a() : null;
    }

    private final void writeControlFrame(int i, i iVar) throws IOException {
        if (this.writerClosed) {
            throw new IOException("closed");
        }
        int iS = iVar.s();
        if (!(((long) iS) <= 125)) {
            throw new IllegalArgumentException("Payload size must be less than or equal to 125".toString());
        }
        this.sinkBuffer.j0(i | 128);
        if (this.isClient) {
            this.sinkBuffer.j0(iS | 128);
            Random random = this.random;
            byte[] bArr = this.maskKey;
            if (bArr == null) {
                j.g();
                throw null;
            }
            random.nextBytes(bArr);
            this.sinkBuffer.g0(this.maskKey);
            if (iS > 0) {
                long jB0 = this.sinkBuffer.b0();
                this.sinkBuffer.f0(iVar);
                f fVar = this.sinkBuffer;
                f.a aVar = this.maskCursor;
                if (aVar == null) {
                    j.g();
                    throw null;
                }
                fVar.Q(aVar);
                this.maskCursor.F(jB0);
                WebSocketProtocol.INSTANCE.toggleMask(this.maskCursor, this.maskKey);
                this.maskCursor.close();
            }
        } else {
            this.sinkBuffer.j0(iS);
            this.sinkBuffer.f0(iVar);
        }
        this.sink.flush();
    }

    public final boolean getActiveWriter() {
        return this.activeWriter;
    }

    public final f getBuffer() {
        return this.buffer;
    }

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

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

    public final y newMessageSink(int i, long j) {
        if (!(!this.activeWriter)) {
            throw new IllegalStateException("Another message writer is active. Did you call close()?".toString());
        }
        this.activeWriter = true;
        this.frameSink.setFormatOpcode(i);
        this.frameSink.setContentLength(j);
        this.frameSink.setFirstFrame(true);
        this.frameSink.setClosed(false);
        return this.frameSink;
    }

    public final void setActiveWriter(boolean z) {
        this.activeWriter = z;
    }

    public final void writeClose(int i, i iVar) throws IOException {
        i iVarR = i.f1572e;
        if (i != 0 || iVar != null) {
            if (i != 0) {
                WebSocketProtocol.INSTANCE.validateCloseCode(i);
            }
            f fVar = new f();
            fVar.o0(i);
            if (iVar != null) {
                fVar.f0(iVar);
            }
            iVarR = fVar.R();
        }
        try {
            writeControlFrame(8, iVarR);
        } finally {
            this.writerClosed = true;
        }
    }

    public final void writeMessageFrame(int i, long j, boolean z, boolean z2) throws IOException {
        if (this.writerClosed) {
            throw new IOException("closed");
        }
        if (!z) {
            i = 0;
        }
        if (z2) {
            i |= 128;
        }
        this.sinkBuffer.j0(i);
        int i2 = this.isClient ? 128 : 0;
        if (j <= 125) {
            this.sinkBuffer.j0(((int) j) | i2);
        } else if (j <= WebSocketProtocol.PAYLOAD_SHORT_MAX) {
            this.sinkBuffer.j0(i2 | WebSocketProtocol.PAYLOAD_SHORT);
            this.sinkBuffer.o0((int) j);
        } else {
            this.sinkBuffer.j0(i2 | 127);
            this.sinkBuffer.n0(j);
        }
        if (this.isClient) {
            Random random = this.random;
            byte[] bArr = this.maskKey;
            if (bArr == null) {
                j.g();
                throw null;
            }
            random.nextBytes(bArr);
            this.sinkBuffer.g0(this.maskKey);
            if (j > 0) {
                long jB0 = this.sinkBuffer.b0();
                this.sinkBuffer.write(this.buffer, j);
                f fVar = this.sinkBuffer;
                f.a aVar = this.maskCursor;
                if (aVar == null) {
                    j.g();
                    throw null;
                }
                fVar.Q(aVar);
                this.maskCursor.F(jB0);
                WebSocketProtocol.INSTANCE.toggleMask(this.maskCursor, this.maskKey);
                this.maskCursor.close();
            }
        } else {
            this.sinkBuffer.write(this.buffer, j);
        }
        this.sink.n();
    }

    public final void writePing(i iVar) throws IOException {
        j.c(iVar, MqttServiceConstants.PAYLOAD);
        writeControlFrame(9, iVar);
    }

    public final void writePong(i iVar) throws IOException {
        j.c(iVar, MqttServiceConstants.PAYLOAD);
        writeControlFrame(10, iVar);
    }
}
