content: package okhttp3.internal.ws;
  
  import java.io.Closeable;
  import java.io.IOException;
  import java.net.ProtocolException;
  import java.util.concurrent.TimeUnit;
  import kotlin.Metadata;
  import kotlin.jvm.internal.Intrinsics;
  import mt.Log1F380D;
  import okhttp3.HttpUrl;
  import okhttp3.internal.Util;
  import okio.Buffer;
  import okio.BufferedSource;
  import okio.ByteString;
  
  @Metadata(bv = {1, 0, 3}, d1 = {"\u0000P\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\u0005\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\t\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u0012\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\b\n\u0002\b\u0004\n\u0002\u0010\u0002\n\u0002\b\b\u0018\u00002\u00020\u0001:\u0001&B-\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¢\u0006\u0002\u0010\nJ\b\u0010\u001e\u001a\u00020\u001fH\u0016J\u0006\u0010 \u001a\u00020\u001fJ\b\u0010!\u001a\u00020\u001fH\u0002J\b\u0010\"\u001a\u00020\u001fH\u0002J\b\u0010#\u001a\u00020\u001fH\u0002J\b\u0010$\u001a\u00020\u001fH\u0002J\b\u0010%\u001a\u00020\u001fH\u0002R\u000e\u0010\u000b\u001a\u00020\u0003X\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\f\u001a\u00020\rX\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\u0006\u001a\u00020\u0007X\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\u000e\u001a\u00020\u000fX\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\u0002\u001a\u00020\u0003X\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\u0010\u001a\u00020\u0003X\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\u0011\u001a\u00020\u0003X\u000e¢\u0006\u0002\n\u0000R\u0010\u0010\u0012\u001a\u0004\u0018\u00010\u0013X\u0004¢\u0006\u0002\n\u0000R\u0010\u0010\u0014\u001a\u0004\u0018\u00010\u0015X\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\u0016\u001a\u00020\rX\u0004¢\u0006\u0002\n\u0000R\u0010\u0010\u0017\u001a\u0004\u0018\u00010\u0018X\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\t\u001a\u00020\u0003X\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\u0019\u001a\u00020\u001aX\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\b\u001a\u00020\u0003X\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\u001b\u001a\u00020\u0003X\u000e¢\u0006\u0002\n\u0000R\u0011\u0010\u0004\u001a\u00020\u0005¢\u0006\b\n\u0000\u001a\u0004\b\u001c\u0010\u001d¨\u0006'"}, d2 = {"Lokhttp3/internal/ws/WebSocketReader;", "Ljava/io/Closeable;", "isClient", "", "source", "Lokio/BufferedSource;", "frameCallback", "Lokhttp3/internal/ws/WebSocketReader$FrameCallback;", "perMessageDeflate", "noContextTakeover", "(ZLokio/BufferedSource;Lokhttp3/internal/ws/WebSocketReader$FrameCallback;ZZ)V", "closed", "controlFrameBuffer", "Lokio/Buffer;", "frameLength", "", "isControlFrame", "isFinalFrame", "maskCursor", "Lokio/Buffer$UnsafeCursor;", "maskKey", "", "messageFrameBuffer", "messageInflater", "Lokhttp3/internal/ws/MessageInflater;", "opcode", "", "readingCompressedMessage", "getSource", "()Lokio/BufferedSource;", "close", "", "processNextFrame", "readControlFrame", "readHeader", "readMessage", "readMessageFrame", "readUntilNonControlFrame", "FrameCallback", "okhttp"}, k = 1, mv = {1, 4, 0})
  /* compiled from: 01E7 */
  public final class WebSocketReader implements Closeable {
      private boolean closed;
      private final Buffer controlFrameBuffer = new Buffer();
      private final FrameCallback frameCallback;
      private long frameLength;
      private final boolean isClient;
      private boolean isControlFrame;
      private boolean isFinalFrame;
      private final Buffer.UnsafeCursor maskCursor;
      private final byte[] maskKey;
      private final Buffer messageFrameBuffer = new Buffer();
      private MessageInflater messageInflater;
      private final boolean noContextTakeover;
      private int opcode;
      private final boolean perMessageDeflate;
      private boolean readingCompressedMessage;
      private final BufferedSource source;
  
      @Metadata(bv = {1, 0, 3}, d1 = {"\u0000&\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0000\n\u0002\u0010\u0002\n\u0000\n\u0002\u0010\b\n\u0000\n\u0002\u0010\u000e\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0002\b\u0004\bf\u0018\u00002\u00020\u0001J\u0018\u0010\u0002\u001a\u00020\u00032\u0006\u0010\u0004\u001a\u00020\u00052\u0006\u0010\u0006\u001a\u00020\u0007H&J\u0010\u0010\b\u001a\u00020\u00032\u0006\u0010\t\u001a\u00020\u0007H&J\u0010\u0010\b\u001a\u00020\u00032\u0006\u0010\n\u001a\u00020\u000bH&J\u0010\u0010\f\u001a\u00020\u00032\u0006\u0010\r\u001a\u00020\u000bH&J\u0010\u0010\u000e\u001a\u00020\u00032\u0006\u0010\r\u001a\u00020\u000bH&¨\u0006\u000f"}, d2 = {"Lokhttp3/internal/ws/WebSocketReader$FrameCallback;", "", "onReadClose", "", "code", "", "reason", "", "onReadMessage", "text", "bytes", "Lokio/ByteString;", "onReadPing", "payload", "onReadPong", "okhttp"}, k = 1, mv = {1, 4, 0})
      /* compiled from: WebSocketReader.kt */
      public interface FrameCallback {
          void onReadClose(int i, String str);
  
          void onReadMessage(String str) throws IOException;
  
          void onReadMessage(ByteString byteString) throws IOException;
  
          void onReadPing(ByteString byteString);
  
          void onReadPong(ByteString byteString);
      }
  
      public WebSocketReader(boolean isClient2, BufferedSource source2, FrameCallback frameCallback2, boolean perMessageDeflate2, boolean noContextTakeover2) {
          Intrinsics.checkNotNullParameter(source2, "source");
          Intrinsics.checkNotNullParameter(frameCallback2, "frameCallback");
          this.isClient = isClient2;
          this.source = source2;
          this.frameCallback = frameCallback2;
          this.perMessageDeflate = perMessageDeflate2;
          this.noContextTakeover = noContextTakeover2;
          Buffer.UnsafeCursor unsafeCursor = null;
          this.maskKey = isClient2 ? null : new byte[4];
          this.maskCursor = !isClient2 ? new Buffer.UnsafeCursor() : unsafeCursor;
      }
  
      private final void readControlFrame() throws IOException {
          long j = this.frameLength;
          if (j > 0) {
              this.source.readFully(this.controlFrameBuffer, j);
              if (!this.isClient) {
                  Buffer buffer = this.controlFrameBuffer;
                  Buffer.UnsafeCursor unsafeCursor = this.maskCursor;
                  Intrinsics.checkNotNull(unsafeCursor);
                  buffer.readAndWriteUnsafe(unsafeCursor);
                  this.maskCursor.seek(0);
                  WebSocketProtocol webSocketProtocol = WebSocketProtocol.INSTANCE;
                  Buffer.UnsafeCursor unsafeCursor2 = this.maskCursor;
                  byte[] bArr = this.maskKey;
                  Intrinsics.checkNotNull(bArr);
                  webSocketProtocol.toggleMask(unsafeCursor2, bArr);
                  this.maskCursor.close();
              }
          }
          switch (this.opcode) {
              case 8:
                  short s = 1005;
                  String str = HttpUrl.FRAGMENT_ENCODE_SET;
                  long size = this.controlFrameBuffer.size();
                  if (size != 1) {
                      if (size != 0) {
                          s = this.controlFrameBuffer.readShort();
                          str = this.controlFrameBuffer.readUtf8();
                          String closeCodeExceptionMessage = WebSocketProtocol.INSTANCE.closeCodeExceptionMessage(s);
                          if (closeCodeExceptionMessage != null) {
                              throw new ProtocolException(closeCodeExceptionMessage);
                          }
                      }
                      this.frameCallback.onReadClose(s, str);
                      this.closed = true;
                      return;
                  }
                  throw new ProtocolException("Malformed close payload length of 1.");
              case 9:
                  this.frameCallback.onReadPing(this.controlFrameBuffer.readByteString());
                  return;
              case 10:
                  this.frameCallback.onReadPong(this.controlFrameBuffer.readByteString());
                  return;
              default:
                  StringBuilder append = new StringBuilder().append("Unknown control opcode: ");
                  String hexString = Util.toHexString(this.opcode);
                  Log1F380D.a((Object) hexString);
                  throw new ProtocolException(append.append(hexString).toString());
          }
      }
  
      private final void readUntilNonControlFrame() throws IOException {
          while (!this.closed) {
              readHeader();
              if (this.isControlFrame) {
                  readControlFrame();
              } else {
                  return;
              }
          }
      }
  
      public void close() throws IOException {
          MessageInflater messageInflater2 = this.messageInflater;
          if (messageInflater2 != null) {
              messageInflater2.close();
          }
      }
  
      public final BufferedSource getSource() {
          return this.source;
      }
  
      public final void processNextFrame() throws IOException {
          readHeader();
          if (this.isControlFrame) {
              readControlFrame();
          } else {
              readMessageFrame();
          }
      }
  
      /* JADX INFO: finally extract failed */
      private final void readHeader() throws IOException, ProtocolException {
          boolean z;
          if (!this.closed) {
              long timeoutNanos = this.source.timeout().timeoutNanos();
              this.source.timeout().clearTimeout();
              try {
                  int and = Util.and(this.source.readByte(), 255);
                  this.source.timeout().timeout(timeoutNanos, TimeUnit.NANOSECONDS);
                  int i = and & 15;
                  this.opcode = i;
                  boolean z2 = true;
                  boolean z3 = (and & 128) != 0;
                  this.isFinalFrame = z3;
                  boolean z4 = (and & 8) != 0;
                  this.isControlFrame = z4;
                  if (!z4 || z3) {
                      boolean z5 = (and & 64) != 0;
                      switch (i) {
                          case 1:
                          case 2:
                              if (!z5) {
                                  z = false;
                              } else if (this.perMessageDeflate) {
                                  z = true;
                              } else {
                                  throw new ProtocolException("Unexpected rsv1 flag");
                              }
                              this.readingCompressedMessage = z;
                              break;
                          default:
                              if (z5) {
                                  throw new ProtocolException("Unexpected rsv1 flag");
                              }
                              break;
                      }
                      if (!((and & 32) != 0)) {
                          if (!((and & 16) != 0)) {
                              int and2 = Util.and(this.source.readByte(), 255);
                              if ((and2 & 128) == 0) {
                                  z2 = false;
                              }
                              if (z2 == this.isClient) {
                                  throw new ProtocolException(this.isClient ? "Server-sent frames must not be masked." : "Client-sent frames must be masked.");
                              }
                              long j = (long) (and2 & 127);
                              this.frameLength = j;
                              if (j == ((long) WebSocketProtocol.PAYLOAD_SHORT)) {
                                  this.frameLength = (long) Util.and(this.source.readShort(), 65535);
                              } else if (j == ((long) 127)) {
                                  long readLong = this.source.readLong();
                                  this.frameLength = readLong;
                                  if (readLong < 0) {
                                      StringBuilder append = new StringBuilder().append("Frame length 0x");
                                      String hexString = Util.toHexString(this.frameLength);
                                      Log1F380D.a((Object) hexString);
                                      throw new ProtocolException(append.append(hexString).append(" > 0x7FFFFFFFFFFFFFFF").toString());
                                  }
                              }
                              if (this.isControlFrame && this.frameLength > 125) {
                                  throw new ProtocolException("Control frame must be less than 125B.");
                              } else if (z2) {
                                  BufferedSource bufferedSource = this.source;
                                  byte[] bArr = this.maskKey;
                                  Intrinsics.checkNotNull(bArr);
                                  bufferedSource.readFully(bArr);
                              }
                          } else {
                              throw new ProtocolException("Unexpected rsv3 flag");
                          }
                      } else {
                          throw new ProtocolException("Unexpected rsv2 flag");
                      }
                  } else {
                      throw new ProtocolException("Control frames must be final.");
                  }
              } catch (Throwable th) {
                  this.source.timeout().timeout(timeoutNanos, TimeUnit.NANOSECONDS);
                  throw th;
              }
          } else {
              throw new IOException("closed");
          }
      }
  
      private final void readMessage() throws IOException {
          while (!this.closed) {
              long j = this.frameLength;
              if (j > 0) {
                  this.source.readFully(this.messageFrameBuffer, j);
                  if (!this.isClient) {
                      Buffer buffer = this.messageFrameBuffer;
                      Buffer.UnsafeCursor unsafeCursor = this.maskCursor;
                      Intrinsics.checkNotNull(unsafeCursor);
                      buffer.readAndWriteUnsafe(unsafeCursor);
                      this.maskCursor.seek(this.messageFrameBuffer.size() - this.frameLength);
                      WebSocketProtocol webSocketProtocol = WebSocketProtocol.INSTANCE;
                      Buffer.UnsafeCursor unsafeCursor2 = this.maskCursor;
                      byte[] bArr = this.maskKey;
                      Intrinsics.checkNotNull(bArr);
                      webSocketProtocol.toggleMask(unsafeCursor2, bArr);
                      this.maskCursor.close();
                  }
              }
              if (!this.isFinalFrame) {
                  readUntilNonControlFrame();
                  if (this.opcode != 0) {
                      StringBuilder append = new StringBuilder().append("Expected continuation opcode. Got: ");
                      String hexString = Util.toHexString(this.opcode);
                      Log1F380D.a((Object) hexString);
                      throw new ProtocolException(append.append(hexString).toString());
                  }
              } else {
                  return;
              }
          }
          throw new IOException("closed");
      }
  
      private final void readMessageFrame() throws IOException {
          int i = this.opcode;
          if (i == 1 || i == 2) {
              readMessage();
              if (this.readingCompressedMessage) {
                  MessageInflater messageInflater2 = this.messageInflater;
                  if (messageInflater2 == null) {
                      messageInflater2 = new MessageInflater(this.noContextTakeover);
                      this.messageInflater = messageInflater2;
                  }
                  messageInflater2.inflate(this.messageFrameBuffer);
              }
              if (i == 1) {
                  this.frameCallback.onReadMessage(this.messageFrameBuffer.readUtf8());
              } else {
                  this.frameCallback.onReadMessage(this.messageFrameBuffer.readByteString());
              }
          } else {
              StringBuilder append = new StringBuilder().append("Unknown opcode: ");
              String hexString = Util.toHexString(i);
              Log1F380D.a((Object) hexString);
              throw new ProtocolException(append.append(hexString).toString());
          }
      }
  }
  