package okhttp3.internal.http2;

import c.h;
import c.j;
import c.k;
import c.q.d.g;
import d.a0;
import d.b0;
import d.d;
import d.f;
import d.y;
import java.io.EOFException;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.SocketTimeoutException;
import java.util.ArrayDeque;
import java.util.List;
import okhttp3.Headers;
import okhttp3.internal.Util;

/* JADX INFO: compiled from: Http2Stream.kt */
/* JADX INFO: loaded from: classes.dex */
public final class Http2Stream {
    public static final Companion Companion = new Companion(null);
    public static final long EMIT_BUFFER_SIZE = 16384;
    private long bytesLeftInWriteWindow;
    private final Http2Connection connection;
    private ErrorCode errorCode;
    private IOException errorException;
    private boolean hasResponseHeaders;
    private final ArrayDeque<Headers> headersQueue;
    private final int id;
    private final StreamTimeout readTimeout;
    private final FramingSink sink;
    private final FramingSource source;
    private long unacknowledgedBytesRead;
    private final StreamTimeout writeTimeout;

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

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

    /* JADX INFO: compiled from: Http2Stream.kt */
    public final class FramingSource implements a0 {
        private boolean closed;
        private boolean finished;
        private final long maxByteCount;
        private Headers trailers;
        private final f receiveBuffer = new f();
        private final f readBuffer = new f();

        public FramingSource(long j, boolean z) {
            this.maxByteCount = j;
            this.finished = z;
        }

        private final void updateConnectionFlowControl(long j) {
            boolean z = !Thread.holdsLock(Http2Stream.this);
            if (k.f1418a && !z) {
                throw new AssertionError("Assertion failed");
            }
            Http2Stream.this.getConnection().updateConnectionFlowControl$okhttp(j);
        }

        @Override // d.a0, java.io.Closeable, java.lang.AutoCloseable
        public void close() throws IOException {
            long jB0;
            synchronized (Http2Stream.this) {
                this.closed = true;
                jB0 = this.readBuffer.b0();
                this.readBuffer.clear();
                Http2Stream http2Stream = Http2Stream.this;
                if (http2Stream == null) {
                    throw new h("null cannot be cast to non-null type java.lang.Object");
                }
                http2Stream.notifyAll();
                j jVar = j.f1417a;
            }
            if (jB0 > 0) {
                updateConnectionFlowControl(jB0);
            }
            Http2Stream.this.cancelStreamIfNecessary$okhttp();
        }

        public final boolean getClosed$okhttp() {
            return this.closed;
        }

        public final boolean getFinished$okhttp() {
            return this.finished;
        }

        public final f getReadBuffer() {
            return this.readBuffer;
        }

        public final f getReceiveBuffer() {
            return this.receiveBuffer;
        }

        public final Headers getTrailers() {
            return this.trailers;
        }

        @Override // d.a0
        public long read(f fVar, long j) throws IOException {
            IOException errorException$okhttp;
            long j2;
            boolean z;
            c.q.d.j.c(fVar, "sink");
            if (!(j >= 0)) {
                throw new IllegalArgumentException(("byteCount < 0: " + j).toString());
            }
            do {
                synchronized (Http2Stream.this) {
                    Http2Stream.this.getReadTimeout$okhttp().enter();
                    try {
                        if (Http2Stream.this.getErrorCode$okhttp() != null) {
                            errorException$okhttp = Http2Stream.this.getErrorException$okhttp();
                            if (errorException$okhttp == null) {
                                ErrorCode errorCode$okhttp = Http2Stream.this.getErrorCode$okhttp();
                                if (errorCode$okhttp == null) {
                                    c.q.d.j.g();
                                    throw null;
                                }
                                errorException$okhttp = new StreamResetException(errorCode$okhttp);
                            }
                        } else {
                            errorException$okhttp = null;
                        }
                        if (this.closed) {
                            throw new IOException("stream closed");
                        }
                        if (this.readBuffer.b0() > 0) {
                            j2 = this.readBuffer.read(fVar, Math.min(j, this.readBuffer.b0()));
                            Http2Stream http2Stream = Http2Stream.this;
                            http2Stream.setUnacknowledgedBytesRead$okhttp(http2Stream.getUnacknowledgedBytesRead() + j2);
                            if (errorException$okhttp == null && Http2Stream.this.getUnacknowledgedBytesRead() >= Http2Stream.this.getConnection().getOkHttpSettings().getInitialWindowSize() / 2) {
                                Http2Stream.this.getConnection().writeWindowUpdateLater$okhttp(Http2Stream.this.getId(), Http2Stream.this.getUnacknowledgedBytesRead());
                                Http2Stream.this.setUnacknowledgedBytesRead$okhttp(0L);
                            }
                        } else if (this.finished || errorException$okhttp != null) {
                            j2 = -1;
                        } else {
                            Http2Stream.this.waitForIo$okhttp();
                            j2 = -1;
                            z = true;
                            Http2Stream.this.getReadTimeout$okhttp().exitAndThrowIfTimedOut();
                            j jVar = j.f1417a;
                        }
                        z = false;
                        Http2Stream.this.getReadTimeout$okhttp().exitAndThrowIfTimedOut();
                        j jVar2 = j.f1417a;
                    } catch (Throwable th) {
                        Http2Stream.this.getReadTimeout$okhttp().exitAndThrowIfTimedOut();
                        throw th;
                    }
                }
            } while (z);
            if (j2 != -1) {
                updateConnectionFlowControl(j2);
                return j2;
            }
            if (errorException$okhttp == null) {
                return -1L;
            }
            if (errorException$okhttp != null) {
                throw errorException$okhttp;
            }
            c.q.d.j.g();
            throw null;
        }

        public final void receive$okhttp(d.h hVar, long j) throws IOException {
            boolean z;
            boolean z2;
            c.q.d.j.c(hVar, "source");
            boolean z3 = !Thread.holdsLock(Http2Stream.this);
            if (k.f1418a && !z3) {
                throw new AssertionError("Assertion failed");
            }
            while (j > 0) {
                synchronized (Http2Stream.this) {
                    z = this.finished;
                    z2 = this.readBuffer.b0() + j > this.maxByteCount;
                    j jVar = j.f1417a;
                }
                if (z2) {
                    hVar.l(j);
                    Http2Stream.this.closeLater(ErrorCode.FLOW_CONTROL_ERROR);
                    return;
                }
                if (z) {
                    hVar.l(j);
                    return;
                }
                long j2 = hVar.read(this.receiveBuffer, j);
                if (j2 == -1) {
                    throw new EOFException();
                }
                j -= j2;
                synchronized (Http2Stream.this) {
                    boolean z4 = this.readBuffer.b0() == 0;
                    this.readBuffer.f(this.receiveBuffer);
                    if (z4) {
                        Http2Stream http2Stream = Http2Stream.this;
                        if (http2Stream == null) {
                            throw new h("null cannot be cast to non-null type java.lang.Object");
                        }
                        http2Stream.notifyAll();
                    }
                    j jVar2 = j.f1417a;
                }
            }
        }

        public final void setClosed$okhttp(boolean z) {
            this.closed = z;
        }

        public final void setFinished$okhttp(boolean z) {
            this.finished = z;
        }

        public final void setTrailers(Headers headers) {
            this.trailers = headers;
        }

        @Override // d.a0
        public b0 timeout() {
            return Http2Stream.this.getReadTimeout$okhttp();
        }
    }

    /* JADX INFO: compiled from: Http2Stream.kt */
    public final class StreamTimeout extends d {
        public StreamTimeout() {
        }

        public final void exitAndThrowIfTimedOut() throws IOException {
            if (exit()) {
                throw newTimeoutException(null);
            }
        }

        @Override // d.d
        protected IOException newTimeoutException(IOException iOException) {
            SocketTimeoutException socketTimeoutException = new SocketTimeoutException("timeout");
            if (iOException != null) {
                socketTimeoutException.initCause(iOException);
            }
            return socketTimeoutException;
        }

        @Override // d.d
        protected void timedOut() {
            Http2Stream.this.closeLater(ErrorCode.CANCEL);
        }
    }

    public Http2Stream(int i, Http2Connection http2Connection, boolean z, boolean z2, Headers headers) {
        c.q.d.j.c(http2Connection, "connection");
        this.id = i;
        this.connection = http2Connection;
        this.bytesLeftInWriteWindow = http2Connection.getPeerSettings().getInitialWindowSize();
        this.headersQueue = new ArrayDeque<>();
        this.source = new FramingSource(this.connection.getOkHttpSettings().getInitialWindowSize(), z2);
        this.sink = new FramingSink(z);
        this.readTimeout = new StreamTimeout();
        this.writeTimeout = new StreamTimeout();
        if (headers == null) {
            if (!isLocallyInitiated()) {
                throw new IllegalStateException("remotely-initiated streams should have headers".toString());
            }
        } else {
            if (!(!isLocallyInitiated())) {
                throw new IllegalStateException("locally-initiated streams shouldn't have headers yet".toString());
            }
            this.headersQueue.add(headers);
        }
    }

    private final boolean closeInternal(ErrorCode errorCode, IOException iOException) {
        boolean z = !Thread.holdsLock(this);
        if (k.f1418a && !z) {
            throw new AssertionError("Assertion failed");
        }
        synchronized (this) {
            if (this.errorCode != null) {
                return false;
            }
            if (this.source.getFinished$okhttp() && this.sink.getFinished()) {
                return false;
            }
            this.errorCode = errorCode;
            this.errorException = iOException;
            notifyAll();
            j jVar = j.f1417a;
            this.connection.removeStream$okhttp(this.id);
            return true;
        }
    }

    public final void addBytesToWriteWindow(long j) {
        this.bytesLeftInWriteWindow += j;
        if (j > 0) {
            notifyAll();
        }
    }

    public final void cancelStreamIfNecessary$okhttp() throws IOException {
        boolean zIsOpen;
        boolean z = true;
        boolean z2 = !Thread.holdsLock(this);
        if (k.f1418a && !z2) {
            throw new AssertionError("Assertion failed");
        }
        synchronized (this) {
            if (this.source.getFinished$okhttp() || !this.source.getClosed$okhttp() || (!this.sink.getFinished() && !this.sink.getClosed())) {
                z = false;
            }
            zIsOpen = isOpen();
            j jVar = j.f1417a;
        }
        if (z) {
            close(ErrorCode.CANCEL, null);
        } else {
            if (zIsOpen) {
                return;
            }
            this.connection.removeStream$okhttp(this.id);
        }
    }

    public final void checkOutNotClosed$okhttp() throws IOException {
        if (this.sink.getClosed()) {
            throw new IOException("stream closed");
        }
        if (this.sink.getFinished()) {
            throw new IOException("stream finished");
        }
        if (this.errorCode != null) {
            IOException iOException = this.errorException;
            if (iOException != null) {
                throw iOException;
            }
            ErrorCode errorCode = this.errorCode;
            if (errorCode != null) {
                throw new StreamResetException(errorCode);
            }
            c.q.d.j.g();
            throw null;
        }
    }

    public final void close(ErrorCode errorCode, IOException iOException) throws IOException {
        c.q.d.j.c(errorCode, "rstStatusCode");
        if (closeInternal(errorCode, iOException)) {
            this.connection.writeSynReset$okhttp(this.id, errorCode);
        }
    }

    public final void closeLater(ErrorCode errorCode) {
        c.q.d.j.c(errorCode, "errorCode");
        if (closeInternal(errorCode, null)) {
            this.connection.writeSynResetLater$okhttp(this.id, errorCode);
        }
    }

    public final void enqueueTrailers(Headers headers) {
        c.q.d.j.c(headers, "trailers");
        synchronized (this) {
            boolean z = true;
            if (!(!this.sink.getFinished())) {
                throw new IllegalStateException("already finished".toString());
            }
            if (headers.size() == 0) {
                z = false;
            }
            if (!z) {
                throw new IllegalArgumentException("trailers.size() == 0".toString());
            }
            this.sink.setTrailers(headers);
            j jVar = j.f1417a;
        }
    }

    public final long getBytesLeftInWriteWindow() {
        return this.bytesLeftInWriteWindow;
    }

    public final Http2Connection getConnection() {
        return this.connection;
    }

    public final synchronized ErrorCode getErrorCode$okhttp() {
        return this.errorCode;
    }

    public final IOException getErrorException$okhttp() {
        return this.errorException;
    }

    public final int getId() {
        return this.id;
    }

    public final StreamTimeout getReadTimeout$okhttp() {
        return this.readTimeout;
    }

    public final y getSink() {
        synchronized (this) {
            if (!(this.hasResponseHeaders || isLocallyInitiated())) {
                throw new IllegalStateException("reply before requesting the sink".toString());
            }
            j jVar = j.f1417a;
        }
        return this.sink;
    }

    public final FramingSink getSink$okhttp() {
        return this.sink;
    }

    public final a0 getSource() {
        return this.source;
    }

    public final FramingSource getSource$okhttp() {
        return this.source;
    }

    public final long getUnacknowledgedBytesRead() {
        return this.unacknowledgedBytesRead;
    }

    public final StreamTimeout getWriteTimeout$okhttp() {
        return this.writeTimeout;
    }

    public final boolean isLocallyInitiated() {
        return this.connection.getClient$okhttp() == ((this.id & 1) == 1);
    }

    public final synchronized boolean isOpen() {
        if (this.errorCode != null) {
            return false;
        }
        if ((this.source.getFinished$okhttp() || this.source.getClosed$okhttp()) && (this.sink.getFinished() || this.sink.getClosed())) {
            if (this.hasResponseHeaders) {
                return false;
            }
        }
        return true;
    }

    public final b0 readTimeout() {
        return this.readTimeout;
    }

    public final void receiveData(d.h hVar, int i) throws IOException {
        c.q.d.j.c(hVar, "source");
        boolean z = !Thread.holdsLock(this);
        if (k.f1418a && !z) {
            throw new AssertionError("Assertion failed");
        }
        this.source.receive$okhttp(hVar, i);
    }

    public final void receiveHeaders(Headers headers, boolean z) {
        boolean zIsOpen;
        c.q.d.j.c(headers, "headers");
        boolean z2 = !Thread.holdsLock(this);
        if (k.f1418a && !z2) {
            throw new AssertionError("Assertion failed");
        }
        synchronized (this) {
            if (this.hasResponseHeaders && z) {
                this.source.setTrailers(headers);
            } else {
                this.hasResponseHeaders = true;
                this.headersQueue.add(headers);
            }
            if (z) {
                this.source.setFinished$okhttp(true);
            }
            zIsOpen = isOpen();
            notifyAll();
            j jVar = j.f1417a;
        }
        if (zIsOpen) {
            return;
        }
        this.connection.removeStream$okhttp(this.id);
    }

    public final synchronized void receiveRstStream(ErrorCode errorCode) {
        c.q.d.j.c(errorCode, "errorCode");
        if (this.errorCode == null) {
            this.errorCode = errorCode;
            notifyAll();
        }
    }

    public final void setBytesLeftInWriteWindow$okhttp(long j) {
        this.bytesLeftInWriteWindow = j;
    }

    public final void setErrorCode$okhttp(ErrorCode errorCode) {
        this.errorCode = errorCode;
    }

    public final void setErrorException$okhttp(IOException iOException) {
        this.errorException = iOException;
    }

    public final void setUnacknowledgedBytesRead$okhttp(long j) {
        this.unacknowledgedBytesRead = j;
    }

    public final synchronized Headers takeHeaders() throws IOException {
        Headers headersRemoveFirst;
        this.readTimeout.enter();
        while (this.headersQueue.isEmpty() && this.errorCode == null) {
            try {
                waitForIo$okhttp();
            } catch (Throwable th) {
                this.readTimeout.exitAndThrowIfTimedOut();
                throw th;
            }
        }
        this.readTimeout.exitAndThrowIfTimedOut();
        if (!(!this.headersQueue.isEmpty())) {
            IOException iOException = this.errorException;
            if (iOException != null) {
                throw iOException;
            }
            ErrorCode errorCode = this.errorCode;
            if (errorCode != null) {
                throw new StreamResetException(errorCode);
            }
            c.q.d.j.g();
            throw null;
        }
        headersRemoveFirst = this.headersQueue.removeFirst();
        c.q.d.j.b(headersRemoveFirst, "headersQueue.removeFirst()");
        return headersRemoveFirst;
    }

    public final synchronized Headers trailers() throws IOException {
        Headers trailers;
        if (this.errorCode != null) {
            IOException iOException = this.errorException;
            if (iOException != null) {
                throw iOException;
            }
            ErrorCode errorCode = this.errorCode;
            if (errorCode != null) {
                throw new StreamResetException(errorCode);
            }
            c.q.d.j.g();
            throw null;
        }
        if (!(this.source.getFinished$okhttp() && this.source.getReceiveBuffer().y() && this.source.getReadBuffer().y())) {
            throw new IllegalStateException("too early; can't read the trailers yet".toString());
        }
        trailers = this.source.getTrailers();
        if (trailers == null) {
            trailers = Util.EMPTY_HEADERS;
        }
        return trailers;
    }

    public final void waitForIo$okhttp() throws InterruptedIOException {
        try {
            wait();
        } catch (InterruptedException unused) {
            Thread.currentThread().interrupt();
            throw new InterruptedIOException();
        }
    }

    public final void writeHeaders(List<Header> list, boolean z, boolean z2) throws IOException {
        boolean z3;
        c.q.d.j.c(list, "responseHeaders");
        boolean z4 = !Thread.holdsLock(this);
        if (k.f1418a && !z4) {
            throw new AssertionError("Assertion failed");
        }
        synchronized (this) {
            this.hasResponseHeaders = true;
            if (z) {
                this.sink.setFinished(true);
            }
            j jVar = j.f1417a;
        }
        if (!z2) {
            synchronized (this.connection) {
                z3 = this.connection.getBytesLeftInWriteWindow() == 0;
                j jVar2 = j.f1417a;
            }
            z2 = z3;
        }
        this.connection.writeHeaders$okhttp(this.id, z, list);
        if (z2) {
            this.connection.flush();
        }
    }

    public final b0 writeTimeout() {
        return this.writeTimeout;
    }

    /* JADX INFO: compiled from: Http2Stream.kt */
    public final class FramingSink implements y {
        private boolean closed;
        private boolean finished;
        private final f sendBuffer;
        private Headers trailers;

        public FramingSink(boolean z) {
            this.finished = z;
            this.sendBuffer = new f();
        }

        /* JADX WARN: Removed duplicated region for block: B:24:0x0072  */
        /*
            Code decompiled incorrectly, please refer to instructions dump.
            To view partially-correct add '--show-bad-code' argument
        */
        private final void emitFrame(boolean r12) throws java.io.IOException {
            /*
                r11 = this;
                okhttp3.internal.http2.Http2Stream r0 = okhttp3.internal.http2.Http2Stream.this
                monitor-enter(r0)
                okhttp3.internal.http2.Http2Stream r1 = okhttp3.internal.http2.Http2Stream.this     // Catch: java.lang.Throwable -> La4
                okhttp3.internal.http2.Http2Stream$StreamTimeout r1 = r1.getWriteTimeout$okhttp()     // Catch: java.lang.Throwable -> La4
                r1.enter()     // Catch: java.lang.Throwable -> La4
            Lc:
                okhttp3.internal.http2.Http2Stream r1 = okhttp3.internal.http2.Http2Stream.this     // Catch: java.lang.Throwable -> L99
                long r1 = r1.getBytesLeftInWriteWindow()     // Catch: java.lang.Throwable -> L99
                r3 = 0
                int r1 = (r1 > r3 ? 1 : (r1 == r3 ? 0 : -1))
                if (r1 > 0) goto L2e
                boolean r1 = r11.finished     // Catch: java.lang.Throwable -> L99
                if (r1 != 0) goto L2e
                boolean r1 = r11.closed     // Catch: java.lang.Throwable -> L99
                if (r1 != 0) goto L2e
                okhttp3.internal.http2.Http2Stream r1 = okhttp3.internal.http2.Http2Stream.this     // Catch: java.lang.Throwable -> L99
                okhttp3.internal.http2.ErrorCode r1 = r1.getErrorCode$okhttp()     // Catch: java.lang.Throwable -> L99
                if (r1 != 0) goto L2e
                okhttp3.internal.http2.Http2Stream r1 = okhttp3.internal.http2.Http2Stream.this     // Catch: java.lang.Throwable -> L99
                r1.waitForIo$okhttp()     // Catch: java.lang.Throwable -> L99
                goto Lc
            L2e:
                okhttp3.internal.http2.Http2Stream r1 = okhttp3.internal.http2.Http2Stream.this     // Catch: java.lang.Throwable -> La4
                okhttp3.internal.http2.Http2Stream$StreamTimeout r1 = r1.getWriteTimeout$okhttp()     // Catch: java.lang.Throwable -> La4
                r1.exitAndThrowIfTimedOut()     // Catch: java.lang.Throwable -> La4
                okhttp3.internal.http2.Http2Stream r1 = okhttp3.internal.http2.Http2Stream.this     // Catch: java.lang.Throwable -> La4
                r1.checkOutNotClosed$okhttp()     // Catch: java.lang.Throwable -> La4
                okhttp3.internal.http2.Http2Stream r1 = okhttp3.internal.http2.Http2Stream.this     // Catch: java.lang.Throwable -> La4
                long r1 = r1.getBytesLeftInWriteWindow()     // Catch: java.lang.Throwable -> La4
                d.f r3 = r11.sendBuffer     // Catch: java.lang.Throwable -> La4
                long r3 = r3.b0()     // Catch: java.lang.Throwable -> La4
                long r9 = java.lang.Math.min(r1, r3)     // Catch: java.lang.Throwable -> La4
                okhttp3.internal.http2.Http2Stream r1 = okhttp3.internal.http2.Http2Stream.this     // Catch: java.lang.Throwable -> La4
                long r2 = r1.getBytesLeftInWriteWindow()     // Catch: java.lang.Throwable -> La4
                long r2 = r2 - r9
                r1.setBytesLeftInWriteWindow$okhttp(r2)     // Catch: java.lang.Throwable -> La4
                c.j r1 = c.j.f1417a     // Catch: java.lang.Throwable -> La4
                monitor-exit(r0)
                okhttp3.internal.http2.Http2Stream r0 = okhttp3.internal.http2.Http2Stream.this
                okhttp3.internal.http2.Http2Stream$StreamTimeout r0 = r0.getWriteTimeout$okhttp()
                r0.enter()
                if (r12 == 0) goto L72
                d.f r12 = r11.sendBuffer     // Catch: java.lang.Throwable -> L70
                long r0 = r12.b0()     // Catch: java.lang.Throwable -> L70
                int r12 = (r9 > r0 ? 1 : (r9 == r0 ? 0 : -1))
                if (r12 != 0) goto L72
                r12 = 1
                goto L73
            L70:
                r12 = move-exception
                goto L8f
            L72:
                r12 = 0
            L73:
                r7 = r12
                okhttp3.internal.http2.Http2Stream r12 = okhttp3.internal.http2.Http2Stream.this     // Catch: java.lang.Throwable -> L70
                okhttp3.internal.http2.Http2Connection r5 = r12.getConnection()     // Catch: java.lang.Throwable -> L70
                okhttp3.internal.http2.Http2Stream r12 = okhttp3.internal.http2.Http2Stream.this     // Catch: java.lang.Throwable -> L70
                int r6 = r12.getId()     // Catch: java.lang.Throwable -> L70
                d.f r8 = r11.sendBuffer     // Catch: java.lang.Throwable -> L70
                r5.writeData(r6, r7, r8, r9)     // Catch: java.lang.Throwable -> L70
                okhttp3.internal.http2.Http2Stream r12 = okhttp3.internal.http2.Http2Stream.this
                okhttp3.internal.http2.Http2Stream$StreamTimeout r12 = r12.getWriteTimeout$okhttp()
                r12.exitAndThrowIfTimedOut()
                return
            L8f:
                okhttp3.internal.http2.Http2Stream r0 = okhttp3.internal.http2.Http2Stream.this
                okhttp3.internal.http2.Http2Stream$StreamTimeout r0 = r0.getWriteTimeout$okhttp()
                r0.exitAndThrowIfTimedOut()
                throw r12
            L99:
                r12 = move-exception
                okhttp3.internal.http2.Http2Stream r1 = okhttp3.internal.http2.Http2Stream.this     // Catch: java.lang.Throwable -> La4
                okhttp3.internal.http2.Http2Stream$StreamTimeout r1 = r1.getWriteTimeout$okhttp()     // Catch: java.lang.Throwable -> La4
                r1.exitAndThrowIfTimedOut()     // Catch: java.lang.Throwable -> La4
                throw r12     // Catch: java.lang.Throwable -> La4
            La4:
                r12 = move-exception
                monitor-exit(r0)
                throw r12
            */
            throw new UnsupportedOperationException("Method not decompiled: okhttp3.internal.http2.Http2Stream.FramingSink.emitFrame(boolean):void");
        }

        @Override // d.y, java.io.Closeable, java.lang.AutoCloseable
        public void close() throws IOException {
            boolean z = !Thread.holdsLock(Http2Stream.this);
            if (k.f1418a && !z) {
                throw new AssertionError("Assertion failed");
            }
            synchronized (Http2Stream.this) {
                if (this.closed) {
                    return;
                }
                j jVar = j.f1417a;
                if (!Http2Stream.this.getSink$okhttp().finished) {
                    boolean z2 = this.sendBuffer.b0() > 0;
                    if (this.trailers != null) {
                        while (this.sendBuffer.b0() > 0) {
                            emitFrame(false);
                        }
                        Http2Connection connection = Http2Stream.this.getConnection();
                        int id = Http2Stream.this.getId();
                        Headers headers = this.trailers;
                        if (headers == null) {
                            c.q.d.j.g();
                            throw null;
                        }
                        connection.writeHeaders$okhttp(id, true, Util.toHeaderList(headers));
                    } else if (z2) {
                        while (this.sendBuffer.b0() > 0) {
                            emitFrame(true);
                        }
                    } else {
                        Http2Stream.this.getConnection().writeData(Http2Stream.this.getId(), true, null, 0L);
                    }
                }
                synchronized (Http2Stream.this) {
                    this.closed = true;
                    j jVar2 = j.f1417a;
                }
                Http2Stream.this.getConnection().flush();
                Http2Stream.this.cancelStreamIfNecessary$okhttp();
            }
        }

        @Override // d.y, java.io.Flushable
        public void flush() throws IOException {
            boolean z = !Thread.holdsLock(Http2Stream.this);
            if (k.f1418a && !z) {
                throw new AssertionError("Assertion failed");
            }
            synchronized (Http2Stream.this) {
                Http2Stream.this.checkOutNotClosed$okhttp();
                j jVar = j.f1417a;
            }
            while (this.sendBuffer.b0() > 0) {
                emitFrame(false);
                Http2Stream.this.getConnection().flush();
            }
        }

        public final boolean getClosed() {
            return this.closed;
        }

        public final boolean getFinished() {
            return this.finished;
        }

        public final Headers getTrailers() {
            return this.trailers;
        }

        public final void setClosed(boolean z) {
            this.closed = z;
        }

        public final void setFinished(boolean z) {
            this.finished = z;
        }

        public final void setTrailers(Headers headers) {
            this.trailers = headers;
        }

        @Override // d.y
        public b0 timeout() {
            return Http2Stream.this.getWriteTimeout$okhttp();
        }

        @Override // d.y
        public void write(f fVar, long j) throws IOException {
            c.q.d.j.c(fVar, "source");
            boolean z = !Thread.holdsLock(Http2Stream.this);
            if (k.f1418a && !z) {
                throw new AssertionError("Assertion failed");
            }
            this.sendBuffer.write(fVar, j);
            while (this.sendBuffer.b0() >= Http2Stream.EMIT_BUFFER_SIZE) {
                emitFrame(false);
            }
        }

        public /* synthetic */ FramingSink(Http2Stream http2Stream, boolean z, int i, g gVar) {
            this((i & 1) != 0 ? false : z);
        }
    }
}
