content: package okhttp3.internal.http2;
  
  import java.io.Closeable;
  import java.io.IOException;
  import java.util.List;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  import kotlin.Metadata;
  import kotlin.jvm.internal.DefaultConstructorMarker;
  import kotlin.jvm.internal.Intrinsics;
  import mt.Log1F380D;
  import okhttp3.internal.Util;
  import okhttp3.internal.http2.Hpack;
  import okio.Buffer;
  import okio.BufferedSink;
  
  @Metadata(bv = {1, 0, 3}, d1 = {"\u0000\\\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u000b\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0010\b\n\u0000\n\u0002\u0010\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0011\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u0012\n\u0002\b\u0002\n\u0002\u0010 \n\u0002\u0018\u0002\n\u0002\b\f\n\u0002\u0010\t\n\u0002\b\u0003\u0018\u0000 :2\u00020\u0001:\u0001:B\u0015\u0012\u0006\u0010\u0002\u001a\u00020\u0003\u0012\u0006\u0010\u0004\u001a\u00020\u0005¢\u0006\u0002\u0010\u0006J\u000e\u0010\u0010\u001a\u00020\u00112\u0006\u0010\u0012\u001a\u00020\u0013J\b\u0010\u0014\u001a\u00020\u0011H\u0016J\u0006\u0010\u0015\u001a\u00020\u0011J(\u0010\u0016\u001a\u00020\u00112\u0006\u0010\u0017\u001a\u00020\u00052\u0006\u0010\u0018\u001a\u00020\u000f2\b\u0010\u0019\u001a\u0004\u0018\u00010\t2\u0006\u0010\u001a\u001a\u00020\u000fJ(\u0010\u001b\u001a\u00020\u00112\u0006\u0010\u0018\u001a\u00020\u000f2\u0006\u0010\u001c\u001a\u00020\u000f2\b\u0010\u001d\u001a\u0004\u0018\u00010\t2\u0006\u0010\u001a\u001a\u00020\u000fJ\u0006\u0010\u001e\u001a\u00020\u0011J&\u0010\u001f\u001a\u00020\u00112\u0006\u0010\u0018\u001a\u00020\u000f2\u0006\u0010 \u001a\u00020\u000f2\u0006\u0010!\u001a\u00020\u000f2\u0006\u0010\u001c\u001a\u00020\u000fJ\u001e\u0010\"\u001a\u00020\u00112\u0006\u0010#\u001a\u00020\u000f2\u0006\u0010$\u001a\u00020%2\u0006\u0010&\u001a\u00020'J$\u0010(\u001a\u00020\u00112\u0006\u0010\u0017\u001a\u00020\u00052\u0006\u0010\u0018\u001a\u00020\u000f2\f\u0010)\u001a\b\u0012\u0004\u0012\u00020+0*J\u0006\u0010,\u001a\u00020\u000fJ\u001e\u0010-\u001a\u00020\u00112\u0006\u0010.\u001a\u00020\u00052\u0006\u0010/\u001a\u00020\u000f2\u0006\u00100\u001a\u00020\u000fJ$\u00101\u001a\u00020\u00112\u0006\u0010\u0018\u001a\u00020\u000f2\u0006\u00102\u001a\u00020\u000f2\f\u00103\u001a\b\u0012\u0004\u0012\u00020+0*J\u0016\u00104\u001a\u00020\u00112\u0006\u0010\u0018\u001a\u00020\u000f2\u0006\u0010$\u001a\u00020%J\u000e\u00105\u001a\u00020\u00112\u0006\u00105\u001a\u00020\u0013J\u0016\u00106\u001a\u00020\u00112\u0006\u0010\u0018\u001a\u00020\u000f2\u0006\u00107\u001a\u000208J\u0018\u00109\u001a\u00020\u00112\u0006\u0010\u0018\u001a\u00020\u000f2\u0006\u0010\u001a\u001a\u000208H\u0002R\u000e\u0010\u0004\u001a\u00020\u0005X\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\u0007\u001a\u00020\u0005X\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\b\u001a\u00020\tX\u0004¢\u0006\u0002\n\u0000R\u0011\u0010\n\u001a\u00020\u000b¢\u0006\b\n\u0000\u001a\u0004\b\f\u0010\rR\u000e\u0010\u000e\u001a\u00020\u000fX\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\u0002\u001a\u00020\u0003X\u0004¢\u0006\u0002\n\u0000¨\u0006;"}, d2 = {"Lokhttp3/internal/http2/Http2Writer;", "Ljava/io/Closeable;", "sink", "Lokio/BufferedSink;", "client", "", "(Lokio/BufferedSink;Z)V", "closed", "hpackBuffer", "Lokio/Buffer;", "hpackWriter", "Lokhttp3/internal/http2/Hpack$Writer;", "getHpackWriter", "()Lokhttp3/internal/http2/Hpack$Writer;", "maxFrameSize", "", "applyAndAckSettings", "", "peerSettings", "Lokhttp3/internal/http2/Settings;", "close", "connectionPreface", "data", "outFinished", "streamId", "source", "byteCount", "dataFrame", "flags", "buffer", "flush", "frameHeader", "length", "type", "goAway", "lastGoodStreamId", "errorCode", "Lokhttp3/internal/http2/ErrorCode;", "debugData", "", "headers", "headerBlock", "", "Lokhttp3/internal/http2/Header;", "maxDataLength", "ping", "ack", "payload1", "payload2", "pushPromise", "promisedStreamId", "requestHeaders", "rstStream", "settings", "windowUpdate", "windowSizeIncrement", "", "writeContinuationFrames", "Companion", "okhttp"}, k = 1, mv = {1, 4, 0})
  /* compiled from: 01D6 */
  public final class Http2Writer implements Closeable {
      public static final Companion Companion = new Companion((DefaultConstructorMarker) null);
      private static final Logger logger = Logger.getLogger(Http2.class.getName());
      private final boolean client;
      private boolean closed;
      private final Buffer hpackBuffer;
      private final Hpack.Writer hpackWriter;
      private int maxFrameSize = 16384;
      private final BufferedSink sink;
  
      @Metadata(bv = {1, 0, 3}, d1 = {"\u0000\u0014\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\b\u0003\u0018\u00002\u00020\u0001B\u0007\b\u0002¢\u0006\u0002\u0010\u0002R\u0016\u0010\u0003\u001a\n \u0005*\u0004\u0018\u00010\u00040\u0004X\u0004¢\u0006\u0002\n\u0000¨\u0006\u0006"}, d2 = {"Lokhttp3/internal/http2/Http2Writer$Companion;", "", "()V", "logger", "Ljava/util/logging/Logger;", "kotlin.jvm.PlatformType", "okhttp"}, k = 1, mv = {1, 4, 0})
      /* compiled from: Http2Writer.kt */
      public static final class Companion {
          private Companion() {
          }
  
          public /* synthetic */ Companion(DefaultConstructorMarker $constructor_marker) {
              this();
          }
      }
  
      public Http2Writer(BufferedSink sink2, boolean client2) {
          Intrinsics.checkNotNullParameter(sink2, "sink");
          this.sink = sink2;
          this.client = client2;
          Buffer buffer = new Buffer();
          this.hpackBuffer = buffer;
          this.hpackWriter = new Hpack.Writer(0, false, buffer, 3, (DefaultConstructorMarker) null);
      }
  
      private final void writeContinuationFrames(int streamId, long byteCount) throws IOException {
          long j = byteCount;
          while (j > 0) {
              long min = Math.min((long) this.maxFrameSize, j);
              j -= min;
              frameHeader(streamId, (int) min, 9, j == 0 ? 4 : 0);
              this.sink.write(this.hpackBuffer, min);
          }
      }
  
      public final synchronized void applyAndAckSettings(Settings peerSettings) throws IOException {
          Intrinsics.checkNotNullParameter(peerSettings, "peerSettings");
          if (!this.closed) {
              this.maxFrameSize = peerSettings.getMaxFrameSize(this.maxFrameSize);
              if (peerSettings.getHeaderTableSize() != -1) {
                  this.hpackWriter.resizeHeaderTable(peerSettings.getHeaderTableSize());
              }
              frameHeader(0, 0, 4, 1);
              this.sink.flush();
          } else {
              throw new IOException("closed");
          }
      }
  
      public synchronized void close() throws IOException {
          this.closed = true;
          this.sink.close();
      }
  
      public final synchronized void connectionPreface() throws IOException {
          if (this.closed) {
              throw new IOException("closed");
          } else if (this.client) {
              Logger logger2 = logger;
              if (logger2.isLoggable(Level.FINE)) {
                  String format = Util.format(">> CONNECTION " + Http2.CONNECTION_PREFACE.hex(), new Object[0]);
                  Log1F380D.a((Object) format);
                  logger2.fine(format);
              }
              this.sink.write(Http2.CONNECTION_PREFACE);
              this.sink.flush();
          }
      }
  
      public final synchronized void data(boolean outFinished, int streamId, Buffer source, int byteCount) throws IOException {
          if (!this.closed) {
              int i = 0;
              if (outFinished) {
                  i = 0 | 1;
              }
              dataFrame(streamId, i, source, byteCount);
          } else {
              throw new IOException("closed");
          }
      }
  
      public final void dataFrame(int streamId, int flags, Buffer buffer, int byteCount) throws IOException {
          frameHeader(streamId, byteCount, 0, flags);
          if (byteCount > 0) {
              BufferedSink bufferedSink = this.sink;
              Intrinsics.checkNotNull(buffer);
              bufferedSink.write(buffer, (long) byteCount);
          }
      }
  
      public final synchronized void flush() throws IOException {
          if (!this.closed) {
              this.sink.flush();
          } else {
              throw new IOException("closed");
          }
      }
  
      public final void frameHeader(int streamId, int length, int type, int flags) throws IOException {
          Logger logger2 = logger;
          if (logger2.isLoggable(Level.FINE)) {
              logger2.fine(Http2.INSTANCE.frameLog(false, streamId, length, type, flags));
          }
          boolean z = true;
          if (length <= this.maxFrameSize) {
              if ((((int) 2147483648L) & streamId) != 0) {
                  z = false;
              }
              if (z) {
                  Util.writeMedium(this.sink, length);
                  this.sink.writeByte(type & 255);
                  this.sink.writeByte(flags & 255);
                  this.sink.writeInt(Integer.MAX_VALUE & streamId);
                  return;
              }
              throw new IllegalArgumentException(("reserved bit set: " + streamId).toString());
          }
          throw new IllegalArgumentException(("FRAME_SIZE_ERROR length > " + this.maxFrameSize + ": " + length).toString());
      }
  
      public final Hpack.Writer getHpackWriter() {
          return this.hpackWriter;
      }
  
      public final synchronized void goAway(int lastGoodStreamId, ErrorCode errorCode, byte[] debugData) throws IOException {
          Intrinsics.checkNotNullParameter(errorCode, "errorCode");
          Intrinsics.checkNotNullParameter(debugData, "debugData");
          if (!this.closed) {
              boolean z = false;
              if (errorCode.getHttpCode() != -1) {
                  frameHeader(0, debugData.length + 8, 7, 0);
                  this.sink.writeInt(lastGoodStreamId);
                  this.sink.writeInt(errorCode.getHttpCode());
                  if (debugData.length == 0) {
                      z = true;
                  }
                  if (!z) {
                      this.sink.write(debugData);
                  }
                  this.sink.flush();
              } else {
                  throw new IllegalArgumentException("errorCode.httpCode == -1".toString());
              }
          } else {
              throw new IOException("closed");
          }
      }
  
      public final synchronized void headers(boolean outFinished, int streamId, List<Header> headerBlock) throws IOException {
          Intrinsics.checkNotNullParameter(headerBlock, "headerBlock");
          if (!this.closed) {
              this.hpackWriter.writeHeaders(headerBlock);
              long size = this.hpackBuffer.size();
              long min = Math.min((long) this.maxFrameSize, size);
              int i = size == min ? 4 : 0;
              if (outFinished) {
                  i |= 1;
              }
              frameHeader(streamId, (int) min, 1, i);
              this.sink.write(this.hpackBuffer, min);
              if (size > min) {
                  writeContinuationFrames(streamId, size - min);
              }
          } else {
              throw new IOException("closed");
          }
      }
  
      public final int maxDataLength() {
          return this.maxFrameSize;
      }
  
      public final synchronized void ping(boolean ack, int payload1, int payload2) throws IOException {
          if (!this.closed) {
              frameHeader(0, 8, 6, ack ? 1 : 0);
              this.sink.writeInt(payload1);
              this.sink.writeInt(payload2);
              this.sink.flush();
          } else {
              throw new IOException("closed");
          }
      }
  
      public final synchronized void pushPromise(int streamId, int promisedStreamId, List<Header> requestHeaders) throws IOException {
          Intrinsics.checkNotNullParameter(requestHeaders, "requestHeaders");
          if (!this.closed) {
              this.hpackWriter.writeHeaders(requestHeaders);
              long size = this.hpackBuffer.size();
              int min = (int) Math.min(((long) this.maxFrameSize) - 4, size);
              frameHeader(streamId, min + 4, 5, size == ((long) min) ? 4 : 0);
              this.sink.writeInt(Integer.MAX_VALUE & promisedStreamId);
              this.sink.write(this.hpackBuffer, (long) min);
              if (size > ((long) min)) {
                  writeContinuationFrames(streamId, size - ((long) min));
              }
          } else {
              throw new IOException("closed");
          }
      }
  
      public final synchronized void rstStream(int streamId, ErrorCode errorCode) throws IOException {
          Intrinsics.checkNotNullParameter(errorCode, "errorCode");
          if (!this.closed) {
              if (errorCode.getHttpCode() != -1) {
                  frameHeader(streamId, 4, 3, 0);
                  this.sink.writeInt(errorCode.getHttpCode());
                  this.sink.flush();
              } else {
                  throw new IllegalArgumentException("Failed requirement.".toString());
              }
          } else {
              throw new IOException("closed");
          }
      }
  
      public final synchronized void settings(Settings settings) throws IOException {
          int i;
          Intrinsics.checkNotNullParameter(settings, "settings");
          if (!this.closed) {
              frameHeader(0, settings.size() * 6, 4, 0);
              for (int i2 = 0; i2 < 10; i2++) {
                  if (settings.isSet(i2)) {
                      switch (i2) {
                          case 4:
                              i = 3;
                              break;
                          case 7:
                              i = 4;
                              break;
                          default:
                              i = i2;
                              break;
                      }
                      this.sink.writeShort(i);
                      this.sink.writeInt(settings.get(i2));
                  }
              }
              this.sink.flush();
          } else {
              throw new IOException("closed");
          }
      }
  
      public final synchronized void windowUpdate(int streamId, long windowSizeIncrement) throws IOException {
          if (!this.closed) {
              if (windowSizeIncrement != 0 && windowSizeIncrement <= 2147483647L) {
                  frameHeader(streamId, 4, 8, 0);
                  this.sink.writeInt((int) windowSizeIncrement);
                  this.sink.flush();
              } else {
                  throw new IllegalArgumentException(("windowSizeIncrement == 0 || windowSizeIncrement > 0x7fffffffL: " + windowSizeIncrement).toString());
              }
          } else {
              throw new IOException("closed");
          }
      }
  }
  