content: package okhttp3.internal.ws;
  
  import java.io.Closeable;
  import java.io.IOException;
  import java.util.Random;
  import kotlin.Metadata;
  import kotlin.jvm.internal.Intrinsics;
  import okio.Buffer;
  import okio.BufferedSink;
  import okio.ByteString;
  
  @Metadata(bv = {1, 0, 3}, d1 = {"\u0000V\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\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\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u0012\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0007\n\u0002\u0010\u0002\n\u0002\b\u0002\n\u0002\u0010\b\n\u0000\n\u0002\u0018\u0002\n\u0002\b\t\u0018\u00002\u00020\u0001B5\u0012\u0006\u0010\u0002\u001a\u00020\u0003\u0012\u0006\u0010\u0004\u001a\u00020\u0005\u0012\u0006\u0010\u0006\u001a\u00020\u0007\u0012\u0006\u0010\b\u001a\u00020\u0003\u0012\u0006\u0010\t\u001a\u00020\u0003\u0012\u0006\u0010\n\u001a\u00020\u000b¢\u0006\u0002\u0010\fJ\b\u0010\u001b\u001a\u00020\u001cH\u0016J\u0018\u0010\u001d\u001a\u00020\u001c2\u0006\u0010\u001e\u001a\u00020\u001f2\b\u0010 \u001a\u0004\u0018\u00010!J\u0018\u0010\"\u001a\u00020\u001c2\u0006\u0010#\u001a\u00020\u001f2\u0006\u0010$\u001a\u00020!H\u0002J\u0016\u0010%\u001a\u00020\u001c2\u0006\u0010&\u001a\u00020\u001f2\u0006\u0010'\u001a\u00020!J\u000e\u0010(\u001a\u00020\u001c2\u0006\u0010$\u001a\u00020!J\u000e\u0010)\u001a\u00020\u001c2\u0006\u0010$\u001a\u00020!R\u000e\u0010\u0002\u001a\u00020\u0003X\u0004¢\u0006\u0002\n\u0000R\u0010\u0010\r\u001a\u0004\u0018\u00010\u000eX\u0004¢\u0006\u0002\n\u0000R\u0010\u0010\u000f\u001a\u0004\u0018\u00010\u0010X\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\u0011\u001a\u00020\u0012X\u0004¢\u0006\u0002\n\u0000R\u0010\u0010\u0013\u001a\u0004\u0018\u00010\u0014X\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\n\u001a\u00020\u000bX\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\t\u001a\u00020\u0003X\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\b\u001a\u00020\u0003X\u0004¢\u0006\u0002\n\u0000R\u0011\u0010\u0006\u001a\u00020\u0007¢\u0006\b\n\u0000\u001a\u0004\b\u0015\u0010\u0016R\u0011\u0010\u0004\u001a\u00020\u0005¢\u0006\b\n\u0000\u001a\u0004\b\u0017\u0010\u0018R\u000e\u0010\u0019\u001a\u00020\u0012X\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\u001a\u001a\u00020\u0003X\u000e¢\u0006\u0002\n\u0000¨\u0006*"}, d2 = {"Lokhttp3/internal/ws/WebSocketWriter;", "Ljava/io/Closeable;", "isClient", "", "sink", "Lokio/BufferedSink;", "random", "Ljava/util/Random;", "perMessageDeflate", "noContextTakeover", "minimumDeflateSize", "", "(ZLokio/BufferedSink;Ljava/util/Random;ZZJ)V", "maskCursor", "Lokio/Buffer$UnsafeCursor;", "maskKey", "", "messageBuffer", "Lokio/Buffer;", "messageDeflater", "Lokhttp3/internal/ws/MessageDeflater;", "getRandom", "()Ljava/util/Random;", "getSink", "()Lokio/BufferedSink;", "sinkBuffer", "writerClosed", "close", "", "writeClose", "code", "", "reason", "Lokio/ByteString;", "writeControlFrame", "opcode", "payload", "writeMessageFrame", "formatOpcode", "data", "writePing", "writePong", "okhttp"}, k = 1, mv = {1, 4, 0})
  /* compiled from: WebSocketWriter.kt */
  public final class WebSocketWriter implements Closeable {
      private final boolean isClient;
      private final Buffer.UnsafeCursor maskCursor;
      private final byte[] maskKey;
      private final Buffer messageBuffer = new Buffer();
      private MessageDeflater messageDeflater;
      private final long minimumDeflateSize;
      private final boolean noContextTakeover;
      private final boolean perMessageDeflate;
      private final Random random;
      private final BufferedSink sink;
      private final Buffer sinkBuffer;
      private boolean writerClosed;
  
      public WebSocketWriter(boolean isClient2, BufferedSink sink2, Random random2, boolean perMessageDeflate2, boolean noContextTakeover2, long minimumDeflateSize2) {
          Intrinsics.checkNotNullParameter(sink2, "sink");
          Intrinsics.checkNotNullParameter(random2, "random");
          this.isClient = isClient2;
          this.sink = sink2;
          this.random = random2;
          this.perMessageDeflate = perMessageDeflate2;
          this.noContextTakeover = noContextTakeover2;
          this.minimumDeflateSize = minimumDeflateSize2;
          this.sinkBuffer = sink2.getBuffer();
          Buffer.UnsafeCursor unsafeCursor = null;
          this.maskKey = isClient2 ? new byte[4] : null;
          this.maskCursor = isClient2 ? new Buffer.UnsafeCursor() : unsafeCursor;
      }
  
      private final void writeControlFrame(int opcode, ByteString payload) throws IOException {
          if (!this.writerClosed) {
              int size = payload.size();
              if (((long) size) <= 125) {
                  this.sinkBuffer.writeByte(opcode | 128);
                  int i = size;
                  if (this.isClient) {
                      this.sinkBuffer.writeByte(i | 128);
                      Random random2 = this.random;
                      byte[] bArr = this.maskKey;
                      Intrinsics.checkNotNull(bArr);
                      random2.nextBytes(bArr);
                      this.sinkBuffer.write(this.maskKey);
                      if (size > 0) {
                          long size2 = this.sinkBuffer.size();
                          this.sinkBuffer.write(payload);
                          Buffer buffer = this.sinkBuffer;
                          Buffer.UnsafeCursor unsafeCursor = this.maskCursor;
                          Intrinsics.checkNotNull(unsafeCursor);
                          buffer.readAndWriteUnsafe(unsafeCursor);
                          this.maskCursor.seek(size2);
                          WebSocketProtocol.INSTANCE.toggleMask(this.maskCursor, this.maskKey);
                          this.maskCursor.close();
                      }
                  } else {
                      this.sinkBuffer.writeByte(i);
                      this.sinkBuffer.write(payload);
                  }
                  this.sink.flush();
                  return;
              }
              throw new IllegalArgumentException("Payload size must be less than or equal to 125".toString());
          }
          throw new IOException("closed");
      }
  
      public void close() {
          MessageDeflater messageDeflater2 = this.messageDeflater;
          if (messageDeflater2 != null) {
              messageDeflater2.close();
          }
      }
  
      public final Random getRandom() {
          return this.random;
      }
  
      public final BufferedSink getSink() {
          return this.sink;
      }
  
      public final void writeClose(int code, ByteString reason) throws IOException {
          ByteString byteString = ByteString.EMPTY;
          if (!(code == 0 && reason == null)) {
              if (code != 0) {
                  WebSocketProtocol.INSTANCE.validateCloseCode(code);
              }
              Buffer buffer = new Buffer();
              buffer.writeShort(code);
              if (reason != null) {
                  buffer.write(reason);
              }
              byteString = buffer.readByteString();
          }
          try {
              writeControlFrame(8, byteString);
          } finally {
              this.writerClosed = true;
          }
      }
  
      public final void writeMessageFrame(int formatOpcode, ByteString data) throws IOException {
          Intrinsics.checkNotNullParameter(data, "data");
          if (!this.writerClosed) {
              this.messageBuffer.write(data);
              int i = formatOpcode | 128;
              if (this.perMessageDeflate && ((long) data.size()) >= this.minimumDeflateSize) {
                  MessageDeflater messageDeflater2 = this.messageDeflater;
                  if (messageDeflater2 == null) {
                      messageDeflater2 = new MessageDeflater(this.noContextTakeover);
                      this.messageDeflater = messageDeflater2;
                  }
                  messageDeflater2.deflate(this.messageBuffer);
                  i |= 64;
              }
              long size = this.messageBuffer.size();
              this.sinkBuffer.writeByte(i);
              int i2 = 0;
              if (this.isClient) {
                  i2 = 0 | 128;
              }
              if (size <= 125) {
                  this.sinkBuffer.writeByte(i2 | ((int) size));
              } else if (size <= WebSocketProtocol.PAYLOAD_SHORT_MAX) {
                  this.sinkBuffer.writeByte(i2 | WebSocketProtocol.PAYLOAD_SHORT);
                  this.sinkBuffer.writeShort((int) size);
              } else {
                  this.sinkBuffer.writeByte(i2 | 127);
                  this.sinkBuffer.writeLong(size);
              }
              if (this.isClient) {
                  Random random2 = this.random;
                  byte[] bArr = this.maskKey;
                  Intrinsics.checkNotNull(bArr);
                  random2.nextBytes(bArr);
                  this.sinkBuffer.write(this.maskKey);
                  if (size > 0) {
                      Buffer buffer = this.messageBuffer;
                      Buffer.UnsafeCursor unsafeCursor = this.maskCursor;
                      Intrinsics.checkNotNull(unsafeCursor);
                      buffer.readAndWriteUnsafe(unsafeCursor);
                      this.maskCursor.seek(0);
                      WebSocketProtocol.INSTANCE.toggleMask(this.maskCursor, this.maskKey);
                      this.maskCursor.close();
                  }
              }
              this.sinkBuffer.write(this.messageBuffer, size);
              this.sink.emit();
              return;
          }
          throw new IOException("closed");
      }
  
      public final void writePing(ByteString payload) throws IOException {
          Intrinsics.checkNotNullParameter(payload, "payload");
          writeControlFrame(9, payload);
      }
  
      public final void writePong(ByteString payload) throws IOException {
          Intrinsics.checkNotNullParameter(payload, "payload");
          writeControlFrame(10, payload);
      }
  }
  