package okhttp3.internal.http2;

/* JADX INFO: compiled from: r8-map-id-ae6aab8d9a1033945587ffdd1bc6222159522c1d66e66a2c4cecb065134dd0a6 */
/* JADX INFO: loaded from: E:\ios\vf_dex\classes3.dex */
@kotlin.Metadata(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\u0082\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\u0007\u001a\u00020\u0005X\u0082\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\b\u001a\u00020\tX\u0082\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\u0082\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\u0002\u001a\u00020\u0003X\u0082\u0004¢\u0006\u0002\n\u0000¨\u0006;"}, d2 = {"Lokhttp3/internal/http2/Http2Writer;", "Ljava/io/Closeable;", "sink", "Lokio/BufferedSink;", "client", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "(Lokio/BufferedSink;Z)V", "closed", "hpackBuffer", "Lokio/Buffer;", "hpackWriter", "Lokhttp3/internal/http2/Hpack$Writer;", "getHpackWriter", "()Lokhttp3/internal/http2/Hpack$Writer;", "maxFrameSize", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "applyAndAckSettings", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "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", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "headers", "headerBlock", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "Lokhttp3/internal/http2/Header;", "maxDataLength", "ping", "ack", "payload1", "payload2", "pushPromise", "promisedStreamId", "requestHeaders", "rstStream", "settings", "windowUpdate", "windowSizeIncrement", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "writeContinuationFrames", "Companion", "okhttp"}, k = 1, mv = {1, 8, 0}, xi = 48)
@kotlin.jvm.internal.SourceDebugExtension
public final class Http2Writer implements java.io.Closeable {
    private static final java.util.logging.Logger logger = java.util.logging.Logger.getLogger(okhttp3.internal.http2.Http2.class.getName());
    private final boolean client;
    private boolean closed;

    @org.jetbrains.annotations.NotNull
    private final okio.Buffer hpackBuffer;

    @org.jetbrains.annotations.NotNull
    private final okhttp3.internal.http2.Hpack.Writer hpackWriter;
    private int maxFrameSize;

    @org.jetbrains.annotations.NotNull
    private final okio.BufferedSink sink;

    public Http2Writer(@org.jetbrains.annotations.NotNull okio.BufferedSink bufferedSink, boolean z) {
        kotlin.jvm.internal.Intrinsics.l(bufferedSink, "sink");
        this.sink = bufferedSink;
        this.client = z;
        okio.Buffer buffer = new okio.Buffer();
        this.hpackBuffer = buffer;
        this.maxFrameSize = okhttp3.internal.http2.Http2.INITIAL_MAX_FRAME_SIZE;
        this.hpackWriter = new okhttp3.internal.http2.Hpack.Writer(0, false, buffer, 3, null);
    }

    private final void writeContinuationFrames(int streamId, long byteCount) throws java.io.IOException {
        while (byteCount > 0) {
            long jMin = java.lang.Math.min(this.maxFrameSize, byteCount);
            byteCount -= jMin;
            frameHeader(streamId, (int) jMin, 9, byteCount == 0 ? 4 : 0);
            this.sink.write(this.hpackBuffer, jMin);
        }
    }

    public final synchronized void applyAndAckSettings(@org.jetbrains.annotations.NotNull okhttp3.internal.http2.Settings peerSettings) throws java.io.IOException {
        try {
            kotlin.jvm.internal.Intrinsics.l(peerSettings, "peerSettings");
            if (this.closed) {
                throw new java.io.IOException("closed");
            }
            this.maxFrameSize = peerSettings.getMaxFrameSize(this.maxFrameSize);
            if (peerSettings.getHeaderTableSize() != -1) {
                this.hpackWriter.resizeHeaderTable(peerSettings.getHeaderTableSize());
            }
            frameHeader(0, 0, 4, 1);
            this.sink.flush();
        } catch (java.lang.Throwable th) {
            throw th;
        }
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public synchronized void close() throws java.io.IOException {
        this.closed = true;
        this.sink.close();
    }

    public final synchronized void connectionPreface() throws java.io.IOException {
        try {
            if (this.closed) {
                throw new java.io.IOException("closed");
            }
            if (this.client) {
                java.util.logging.Logger logger2 = logger;
                if (logger2.isLoggable(java.util.logging.Level.FINE)) {
                    logger2.fine(okhttp3.internal.Util.format(">> CONNECTION " + okhttp3.internal.http2.Http2.CONNECTION_PREFACE.k(), new java.lang.Object[0]));
                }
                this.sink.U0(okhttp3.internal.http2.Http2.CONNECTION_PREFACE);
                this.sink.flush();
            }
        } catch (java.lang.Throwable th) {
            throw th;
        }
    }

    public final synchronized void data(boolean outFinished, int streamId, @org.jetbrains.annotations.Nullable okio.Buffer source, int byteCount) throws java.io.IOException {
        if (this.closed) {
            throw new java.io.IOException("closed");
        }
        dataFrame(streamId, outFinished ? 1 : 0, source, byteCount);
    }

    public final void dataFrame(int streamId, int flags, @org.jetbrains.annotations.Nullable okio.Buffer buffer, int byteCount) throws java.io.IOException {
        frameHeader(streamId, byteCount, 0, flags);
        if (byteCount > 0) {
            okio.BufferedSink bufferedSink = this.sink;
            kotlin.jvm.internal.Intrinsics.i(buffer);
            bufferedSink.write(buffer, byteCount);
        }
    }

    public final synchronized void flush() throws java.io.IOException {
        if (this.closed) {
            throw new java.io.IOException("closed");
        }
        this.sink.flush();
    }

    public final void frameHeader(int streamId, int length, int type, int flags) throws java.io.IOException {
        int i;
        int i2;
        int i5;
        int i6;
        java.util.logging.Logger logger2 = logger;
        if (logger2.isLoggable(java.util.logging.Level.FINE)) {
            i = streamId;
            i2 = length;
            i5 = type;
            i6 = flags;
            logger2.fine(okhttp3.internal.http2.Http2.INSTANCE.frameLog(false, i, i2, i5, i6));
        } else {
            i = streamId;
            i2 = length;
            i5 = type;
            i6 = flags;
        }
        if (i2 > this.maxFrameSize) {
            throw new java.lang.IllegalArgumentException(("FRAME_SIZE_ERROR length > " + this.maxFrameSize + ": " + i2).toString());
        }
        if ((Integer.MIN_VALUE & i) != 0) {
            throw new java.lang.IllegalArgumentException(("reserved bit set: " + i).toString());
        }
        okhttp3.internal.Util.writeMedium(this.sink, i2);
        this.sink.writeByte(i5 & 255);
        this.sink.writeByte(i6 & 255);
        this.sink.writeInt(Integer.MAX_VALUE & i);
    }

    @org.jetbrains.annotations.NotNull
    public final okhttp3.internal.http2.Hpack.Writer getHpackWriter() {
        return this.hpackWriter;
    }

    public final synchronized void goAway(int lastGoodStreamId, @org.jetbrains.annotations.NotNull okhttp3.internal.http2.ErrorCode errorCode, @org.jetbrains.annotations.NotNull byte[] debugData) throws java.io.IOException {
        try {
            kotlin.jvm.internal.Intrinsics.l(errorCode, "errorCode");
            kotlin.jvm.internal.Intrinsics.l(debugData, "debugData");
            if (this.closed) {
                throw new java.io.IOException("closed");
            }
            if (errorCode.getHttpCode() == -1) {
                throw new java.lang.IllegalArgumentException("errorCode.httpCode == -1");
            }
            frameHeader(0, debugData.length + 8, 7, 0);
            this.sink.writeInt(lastGoodStreamId);
            this.sink.writeInt(errorCode.getHttpCode());
            if (!(debugData.length == 0)) {
                this.sink.write(debugData);
            }
            this.sink.flush();
        } catch (java.lang.Throwable th) {
            throw th;
        }
    }

    public final synchronized void headers(boolean outFinished, int streamId, @org.jetbrains.annotations.NotNull java.util.List<okhttp3.internal.http2.Header> headerBlock) throws java.io.IOException {
        kotlin.jvm.internal.Intrinsics.l(headerBlock, "headerBlock");
        if (this.closed) {
            throw new java.io.IOException("closed");
        }
        this.hpackWriter.writeHeaders(headerBlock);
        long size = this.hpackBuffer.getSize();
        long jMin = java.lang.Math.min(this.maxFrameSize, size);
        int i = size == jMin ? 4 : 0;
        if (outFinished) {
            i |= 1;
        }
        frameHeader(streamId, (int) jMin, 1, i);
        this.sink.write(this.hpackBuffer, jMin);
        if (size > jMin) {
            writeContinuationFrames(streamId, size - jMin);
        }
    }

    /* JADX INFO: renamed from: maxDataLength, reason: from getter */
    public final int getMaxFrameSize() {
        return this.maxFrameSize;
    }

    public final synchronized void ping(boolean ack, int payload1, int payload2) throws java.io.IOException {
        if (this.closed) {
            throw new java.io.IOException("closed");
        }
        frameHeader(0, 8, 6, ack ? 1 : 0);
        this.sink.writeInt(payload1);
        this.sink.writeInt(payload2);
        this.sink.flush();
    }

    public final synchronized void pushPromise(int streamId, int promisedStreamId, @org.jetbrains.annotations.NotNull java.util.List<okhttp3.internal.http2.Header> requestHeaders) throws java.io.IOException {
        kotlin.jvm.internal.Intrinsics.l(requestHeaders, "requestHeaders");
        if (this.closed) {
            throw new java.io.IOException("closed");
        }
        this.hpackWriter.writeHeaders(requestHeaders);
        long size = this.hpackBuffer.getSize();
        int iMin = (int) java.lang.Math.min(((long) this.maxFrameSize) - 4, size);
        long j = iMin;
        frameHeader(streamId, iMin + 4, 5, size == j ? 4 : 0);
        this.sink.writeInt(promisedStreamId & Integer.MAX_VALUE);
        this.sink.write(this.hpackBuffer, j);
        if (size > j) {
            writeContinuationFrames(streamId, size - j);
        }
    }

    public final synchronized void rstStream(int streamId, @org.jetbrains.annotations.NotNull okhttp3.internal.http2.ErrorCode errorCode) throws java.io.IOException {
        kotlin.jvm.internal.Intrinsics.l(errorCode, "errorCode");
        if (this.closed) {
            throw new java.io.IOException("closed");
        }
        if (errorCode.getHttpCode() == -1) {
            throw new java.lang.IllegalArgumentException("Failed requirement.");
        }
        frameHeader(streamId, 4, 3, 0);
        this.sink.writeInt(errorCode.getHttpCode());
        this.sink.flush();
    }

    public final synchronized void settings(@org.jetbrains.annotations.NotNull okhttp3.internal.http2.Settings settings) throws java.io.IOException {
        int i;
        try {
            kotlin.jvm.internal.Intrinsics.l(settings, "settings");
            if (this.closed) {
                throw new java.io.IOException("closed");
            }
            int i2 = 0;
            frameHeader(0, settings.size() * 6, 4, 0);
            while (i2 < 10) {
                if (settings.isSet(i2)) {
                    if (i2 != 4) {
                        i = i2 != 7 ? i2 : 4;
                    } else {
                        i = 3;
                    }
                    this.sink.writeShort(i);
                    this.sink.writeInt(settings.get(i2));
                }
                i2++;
            }
            this.sink.flush();
        } catch (java.lang.Throwable th) {
            throw th;
        }
    }

    public final synchronized void windowUpdate(int streamId, long windowSizeIncrement) throws java.io.IOException {
        if (this.closed) {
            throw new java.io.IOException("closed");
        }
        if (windowSizeIncrement == 0 || windowSizeIncrement > 2147483647L) {
            throw new java.lang.IllegalArgumentException(("windowSizeIncrement == 0 || windowSizeIncrement > 0x7fffffffL: " + windowSizeIncrement).toString());
        }
        frameHeader(streamId, 4, 8, 0);
        this.sink.writeInt((int) windowSizeIncrement);
        this.sink.flush();
    }
}
