package okhttp3.internal.http2;

import d.b.a.a.a;
import g.h0.c.g;
import g.h0.c.l;
import j.d;
import j.f;
import java.io.Closeable;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import jxl.SheetSettings;
import okhttp3.internal.Util;
import okhttp3.internal.http2.Hpack;

/* JADX INFO: compiled from: Http2Writer.kt */
/* JADX INFO: loaded from: classes.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 d hpackBuffer;
    private final Hpack.Writer hpackWriter;
    private int maxFrameSize;
    private final f sink;

    /* JADX INFO: compiled from: Http2Writer.kt */
    public static final class Companion {
        private Companion() {
        }

        public /* synthetic */ Companion(g gVar) {
            this();
        }
    }

    public Http2Writer(f fVar, boolean z) {
        l.f(fVar, "sink");
        this.sink = fVar;
        this.client = z;
        d dVar = new d();
        this.hpackBuffer = dVar;
        this.maxFrameSize = 16384;
        this.hpackWriter = new Hpack.Writer(0, false, dVar, 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 settings) throws IOException {
        l.f(settings, "peerSettings");
        if (this.closed) {
            throw new IOException("closed");
        }
        this.maxFrameSize = settings.getMaxFrameSize(this.maxFrameSize);
        if (settings.getHeaderTableSize() != -1) {
            this.hpackWriter.resizeHeaderTable(settings.getHeaderTableSize());
        }
        frameHeader(0, 0, 4, 1);
        this.sink.flush();
    }

    @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 {
        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.d(), new Object[0]));
            }
            this.sink.write(Http2.CONNECTION_PREFACE);
            this.sink.flush();
        }
    }

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

    public final void dataFrame(int i2, int i3, d dVar, int i4) throws IOException {
        frameHeader(i2, i4, 0, i3);
        if (i4 > 0) {
            f fVar = this.sink;
            l.c(dVar);
            fVar.write(dVar, 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)) {
            StringBuilder sbB = a.B("FRAME_SIZE_ERROR length > ");
            sbB.append(this.maxFrameSize);
            sbB.append(": ");
            sbB.append(i3);
            throw new IllegalArgumentException(sbB.toString().toString());
        }
        if (!((((int) 2147483648L) & i2) == 0)) {
            throw new IllegalArgumentException(a.j("reserved bit set: ", i2).toString());
        }
        Util.writeMedium(this.sink, i3);
        this.sink.writeByte(i4 & SheetSettings.DEFAULT_DEFAULT_ROW_HEIGHT);
        this.sink.writeByte(i5 & SheetSettings.DEFAULT_DEFAULT_ROW_HEIGHT);
        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[] bArr) throws IOException {
        l.f(errorCode, "errorCode");
        l.f(bArr, "debugData");
        if (this.closed) {
            throw new IOException("closed");
        }
        if (!(errorCode.getHttpCode() != -1)) {
            throw new IllegalArgumentException("errorCode.httpCode == -1".toString());
        }
        frameHeader(0, bArr.length + 8, 7, 0);
        this.sink.writeInt(i2);
        this.sink.writeInt(errorCode.getHttpCode());
        if (!(bArr.length == 0)) {
            this.sink.write(bArr);
        }
        this.sink.flush();
    }

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

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

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

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

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

    public final synchronized void settings(Settings settings) throws IOException {
        l.f(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();
    }

    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();
    }
}
