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.jvm.internal.l;
import okhttp3.internal.Util;
import okhttp3.internal.http2.Hpack;
import okio.C0688f;
import okio.g;

/* JADX INFO: loaded from: classes2.dex */
public final class Http2Writer implements Closeable {
    public static final Companion Companion = new Companion(null);
    private static final Logger logger = Logger.getLogger(Http2.class.getName());
    private final boolean client;
    private boolean closed;
    private final C0688f hpackBuffer;
    private final Hpack.Writer hpackWriter;
    private int maxFrameSize;
    private final g sink;

    public static final class Companion {
        private Companion() {
        }

        public /* synthetic */ Companion(kotlin.jvm.internal.g gVar) {
            this();
        }
    }

    public Http2Writer(g sink, boolean z2) {
        l.e(sink, "sink");
        this.sink = sink;
        this.client = z2;
        C0688f c0688f = new C0688f();
        this.hpackBuffer = c0688f;
        this.maxFrameSize = 16384;
        this.hpackWriter = new Hpack.Writer(0, false, c0688f, 3, null);
    }

    private final void writeContinuationFrames(int i2, long j2) throws IOException {
        while (j2 > 0) {
            long jMin = Math.min(this.maxFrameSize, j2);
            j2 -= jMin;
            frameHeader(i2, (int) jMin, 9, j2 == 0 ? 4 : 0);
            this.sink.write(this.hpackBuffer, jMin);
        }
    }

    public final synchronized void applyAndAckSettings(Settings peerSettings) throws IOException {
        try {
            l.e(peerSettings, "peerSettings");
            if (this.closed) {
                throw new 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 (Throwable th) {
            throw th;
        }
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public synchronized void close() throws IOException {
        this.closed = true;
        this.sink.close();
    }

    public final synchronized void connectionPreface() throws IOException {
        try {
            if (this.closed) {
                throw new IOException("closed");
            }
            if (this.client) {
                Logger logger2 = logger;
                if (logger2.isLoggable(Level.FINE)) {
                    logger2.fine(Util.format(">> CONNECTION " + Http2.CONNECTION_PREFACE.i(), new Object[0]));
                }
                this.sink.write(Http2.CONNECTION_PREFACE);
                this.sink.flush();
            }
        } catch (Throwable th) {
            throw th;
        }
    }

    public final synchronized void data(boolean z2, int i2, C0688f c0688f, int i3) throws IOException {
        if (this.closed) {
            throw new IOException("closed");
        }
        dataFrame(i2, z2 ? 1 : 0, c0688f, i3);
    }

    public final void dataFrame(int i2, int i3, C0688f c0688f, int i4) throws IOException {
        frameHeader(i2, i4, 0, i3);
        if (i4 > 0) {
            g gVar = this.sink;
            l.b(c0688f);
            gVar.write(c0688f, i4);
        }
    }

    public final synchronized void flush() throws IOException {
        if (this.closed) {
            throw new IOException("closed");
        }
        this.sink.flush();
    }

    public final void frameHeader(int i2, int i3, int i4, int i5) throws IOException {
        Logger logger2 = logger;
        if (logger2.isLoggable(Level.FINE)) {
            logger2.fine(Http2.INSTANCE.frameLog(false, i2, i3, i4, i5));
        }
        if (!(i3 <= this.maxFrameSize)) {
            throw new IllegalArgumentException(("FRAME_SIZE_ERROR length > " + this.maxFrameSize + ": " + i3).toString());
        }
        if (!((((int) 2147483648L) & i2) == 0)) {
            throw new IllegalArgumentException(("reserved bit set: " + i2).toString());
        }
        Util.writeMedium(this.sink, i3);
        this.sink.writeByte(i4 & 255);
        this.sink.writeByte(i5 & 255);
        this.sink.writeInt(i2 & Integer.MAX_VALUE);
    }

    public final Hpack.Writer getHpackWriter() {
        return this.hpackWriter;
    }

    public final synchronized void goAway(int i2, ErrorCode errorCode, byte[] debugData) throws IOException {
        try {
            l.e(errorCode, "errorCode");
            l.e(debugData, "debugData");
            if (this.closed) {
                throw new IOException("closed");
            }
            boolean z2 = true;
            if (!(errorCode.getHttpCode() != -1)) {
                throw new IllegalArgumentException("errorCode.httpCode == -1");
            }
            frameHeader(0, debugData.length + 8, 7, 0);
            this.sink.writeInt(i2);
            this.sink.writeInt(errorCode.getHttpCode());
            if (debugData.length != 0) {
                z2 = false;
            }
            if (!z2) {
                this.sink.write(debugData);
            }
            this.sink.flush();
        } finally {
        }
    }

    public final synchronized void headers(boolean z2, int i2, List<Header> headerBlock) throws IOException {
        l.e(headerBlock, "headerBlock");
        if (this.closed) {
            throw new IOException("closed");
        }
        this.hpackWriter.writeHeaders(headerBlock);
        long jG = this.hpackBuffer.G();
        long jMin = Math.min(this.maxFrameSize, jG);
        int i3 = jG == jMin ? 4 : 0;
        if (z2) {
            i3 |= 1;
        }
        frameHeader(i2, (int) jMin, 1, i3);
        this.sink.write(this.hpackBuffer, jMin);
        if (jG > jMin) {
            writeContinuationFrames(i2, jG - jMin);
        }
    }

    public final int maxDataLength() {
        return this.maxFrameSize;
    }

    public final synchronized void ping(boolean z2, int i2, int i3) throws IOException {
        if (this.closed) {
            throw new IOException("closed");
        }
        frameHeader(0, 8, 6, z2 ? 1 : 0);
        this.sink.writeInt(i2);
        this.sink.writeInt(i3);
        this.sink.flush();
    }

    public final synchronized void pushPromise(int i2, int i3, List<Header> requestHeaders) throws IOException {
        l.e(requestHeaders, "requestHeaders");
        if (this.closed) {
            throw new IOException("closed");
        }
        this.hpackWriter.writeHeaders(requestHeaders);
        long jG = this.hpackBuffer.G();
        int iMin = (int) Math.min(((long) this.maxFrameSize) - 4, jG);
        long j2 = iMin;
        frameHeader(i2, iMin + 4, 5, jG == j2 ? 4 : 0);
        this.sink.writeInt(i3 & Integer.MAX_VALUE);
        this.sink.write(this.hpackBuffer, j2);
        if (jG > j2) {
            writeContinuationFrames(i2, jG - j2);
        }
    }

    public final synchronized void rstStream(int i2, ErrorCode errorCode) throws IOException {
        l.e(errorCode, "errorCode");
        if (this.closed) {
            throw new IOException("closed");
        }
        if (!(errorCode.getHttpCode() != -1)) {
            throw new IllegalArgumentException("Failed requirement.");
        }
        frameHeader(i2, 4, 3, 0);
        this.sink.writeInt(errorCode.getHttpCode());
        this.sink.flush();
    }

    public final synchronized void settings(Settings settings) throws IOException {
        try {
            l.e(settings, "settings");
            if (this.closed) {
                throw new IOException("closed");
            }
            int i2 = 0;
            frameHeader(0, settings.size() * 6, 4, 0);
            while (i2 < 10) {
                if (settings.isSet(i2)) {
                    this.sink.writeShort(i2 != 4 ? i2 != 7 ? i2 : 4 : 3);
                    this.sink.writeInt(settings.get(i2));
                }
                i2++;
            }
            this.sink.flush();
        } catch (Throwable th) {
            throw th;
        }
    }

    public final synchronized void windowUpdate(int i2, long j2) throws IOException {
        if (this.closed) {
            throw new IOException("closed");
        }
        if (!(j2 != 0 && j2 <= 2147483647L)) {
            throw new IllegalArgumentException(("windowSizeIncrement == 0 || windowSizeIncrement > 0x7fffffffL: " + j2).toString());
        }
        frameHeader(i2, 4, 8, 0);
        this.sink.writeInt((int) j2);
        this.sink.flush();
    }
}
