package okhttp3.internal.http2;

import A0.a;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.SocketTimeoutException;
import java.util.ArrayDeque;
import kotlin.jvm.internal.o;
import okhttp3.Headers;
import okhttp3.internal.Util;
import okhttp3.internal.concurrent.Task;
import okhttp3.internal.concurrent.TaskQueue;
import p5.C2736f;
import p5.C2740j;
import p5.N;
import p5.P;
import p5.S;

/* JADX INFO: loaded from: classes3.dex */
public final class Http2Stream {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final int f22553a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final Http2Connection f22554b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public long f22555c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public long f22556d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public long f22557e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public long f22558f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final ArrayDeque f22559g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public boolean f22560h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public final FramingSource f22561i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public final FramingSink f22562j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public final StreamTimeout f22563k;
    public final StreamTimeout l;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public ErrorCode f22564m;

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    public IOException f22565n;

    public static final class Companion {
        public /* synthetic */ Companion(int i6) {
            this();
        }

        private Companion() {
        }
    }

    public final class FramingSink implements N {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        public final boolean f22566a;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        public final C2740j f22567b = new C2740j();

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        public boolean f22568c;

        public FramingSink(boolean z6) {
            this.f22566a = z6;
        }

        /* JADX WARN: Finally extract failed */
        public final void a(boolean z6) throws SocketTimeoutException {
            long jMin;
            boolean z7;
            Http2Stream http2Stream = Http2Stream.this;
            synchronized (http2Stream) {
                http2Stream.l.h();
                while (http2Stream.f22557e >= http2Stream.f22558f && !this.f22566a && !this.f22568c) {
                    try {
                        synchronized (http2Stream) {
                            ErrorCode errorCode = http2Stream.f22564m;
                            if (errorCode != null) {
                                break;
                            } else {
                                http2Stream.k();
                            }
                        }
                    } catch (Throwable th) {
                        http2Stream.l.k();
                        throw th;
                    }
                }
                http2Stream.l.k();
                http2Stream.b();
                jMin = Math.min(http2Stream.f22558f - http2Stream.f22557e, this.f22567b.f23100b);
                http2Stream.f22557e += jMin;
                z7 = z6 && jMin == this.f22567b.f23100b;
            }
            Http2Stream.this.l.h();
            try {
                Http2Stream http2Stream2 = Http2Stream.this;
                http2Stream2.f22554b.i(http2Stream2.f22553a, z7, this.f22567b, jMin);
            } finally {
                Http2Stream.this.l.k();
            }
        }

        @Override // p5.N, java.io.Closeable, java.lang.AutoCloseable
        public final void close() {
            boolean z6;
            Http2Stream http2Stream = Http2Stream.this;
            byte[] bArr = Util.f22179a;
            synchronized (http2Stream) {
                if (this.f22568c) {
                    return;
                }
                synchronized (http2Stream) {
                    z6 = http2Stream.f22564m == null;
                }
                Http2Stream http2Stream2 = Http2Stream.this;
                if (!http2Stream2.f22562j.f22566a) {
                    if (this.f22567b.f23100b > 0) {
                        while (this.f22567b.f23100b > 0) {
                            a(true);
                        }
                    } else if (z6) {
                        http2Stream2.f22554b.i(http2Stream2.f22553a, true, null, 0L);
                    }
                }
                synchronized (Http2Stream.this) {
                    this.f22568c = true;
                }
                Http2Stream.this.f22554b.f22470A.flush();
                Http2Stream.this.a();
            }
        }

        @Override // p5.N, java.io.Flushable
        public final void flush() throws SocketTimeoutException {
            Http2Stream http2Stream = Http2Stream.this;
            byte[] bArr = Util.f22179a;
            synchronized (http2Stream) {
                http2Stream.b();
            }
            while (this.f22567b.f23100b > 0) {
                a(false);
                Http2Stream.this.f22554b.f22470A.flush();
            }
        }

        @Override // p5.N
        public final S timeout() {
            return Http2Stream.this.l;
        }

        @Override // p5.N
        public final void write(C2740j source, long j4) throws SocketTimeoutException {
            o.h(source, "source");
            byte[] bArr = Util.f22179a;
            C2740j c2740j = this.f22567b;
            c2740j.write(source, j4);
            while (c2740j.f23100b >= 16384) {
                a(false);
            }
        }
    }

    public final class FramingSource implements P {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        public final long f22570a;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        public boolean f22571b;

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        public final C2740j f22572c = new C2740j();

        /* JADX INFO: renamed from: d, reason: collision with root package name */
        public final C2740j f22573d = new C2740j();

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        public boolean f22574e;

        public FramingSource(long j4, boolean z6) {
            this.f22570a = j4;
            this.f22571b = z6;
        }

        @Override // java.io.Closeable, java.lang.AutoCloseable
        public final void close() {
            long j4;
            Http2Stream http2Stream = Http2Stream.this;
            synchronized (http2Stream) {
                this.f22574e = true;
                C2740j c2740j = this.f22573d;
                j4 = c2740j.f23100b;
                c2740j.a();
                http2Stream.notifyAll();
            }
            if (j4 > 0) {
                byte[] bArr = Util.f22179a;
                Http2Stream.this.f22554b.g(j4);
            }
            Http2Stream.this.a();
        }

        /* JADX WARN: Removed duplicated region for block: B:45:0x0087 A[LOOP:0: B:4:0x000e->B:45:0x0087, LOOP_END] */
        /* JADX WARN: Removed duplicated region for block: B:72:0x008a A[SYNTHETIC] */
        @Override // p5.P
        /*
            Code decompiled incorrectly, please refer to instructions dump.
        */
        public final long read(C2740j sink, long j4) throws Throwable {
            ErrorCode errorCode;
            Throwable streamResetException;
            boolean z6;
            long j6;
            o.h(sink, "sink");
            long j7 = 0;
            if (j4 < 0) {
                throw new IllegalArgumentException(a.m("byteCount < 0: ", j4).toString());
            }
            while (true) {
                Http2Stream http2Stream = Http2Stream.this;
                synchronized (http2Stream) {
                    http2Stream.f22563k.h();
                    try {
                        synchronized (http2Stream) {
                            errorCode = http2Stream.f22564m;
                        }
                        if (z6) {
                            if (j6 != -1) {
                                return j6;
                            }
                            if (streamResetException == null) {
                                return -1L;
                            }
                            throw streamResetException;
                        }
                        j7 = 0;
                    } finally {
                        http2Stream.f22563k.k();
                    }
                }
                if (errorCode == null || this.f22571b) {
                    streamResetException = null;
                } else {
                    streamResetException = http2Stream.f22565n;
                    if (streamResetException == null) {
                        synchronized (http2Stream) {
                            ErrorCode errorCode2 = http2Stream.f22564m;
                            o.e(errorCode2);
                            streamResetException = new StreamResetException(errorCode2);
                        }
                    }
                }
                if (this.f22574e) {
                    throw new IOException("stream closed");
                }
                C2740j c2740j = this.f22573d;
                long j8 = c2740j.f23100b;
                z6 = false;
                if (j8 > j7) {
                    j6 = c2740j.read(sink, Math.min(j4, j8));
                    long j9 = http2Stream.f22555c + j6;
                    http2Stream.f22555c = j9;
                    long j10 = j9 - http2Stream.f22556d;
                    if (streamResetException == null && j10 >= http2Stream.f22554b.f22488t.a() / 2) {
                        http2Stream.f22554b.y(http2Stream.f22553a, j10);
                        http2Stream.f22556d = http2Stream.f22555c;
                    }
                } else {
                    if (!this.f22571b && streamResetException == null) {
                        http2Stream.k();
                        z6 = true;
                    }
                    j6 = -1;
                }
                if (z6) {
                }
            }
        }

        @Override // p5.P
        public final S timeout() {
            return Http2Stream.this.f22563k;
        }
    }

    public final class StreamTimeout extends C2736f {
        public StreamTimeout() {
        }

        @Override // p5.C2736f
        public final void j() {
            Http2Stream.this.e(ErrorCode.CANCEL);
            final Http2Connection http2Connection = Http2Stream.this.f22554b;
            synchronized (http2Connection) {
                long j4 = http2Connection.f22486r;
                long j6 = http2Connection.f22485q;
                if (j4 < j6) {
                    return;
                }
                http2Connection.f22485q = j6 + 1;
                http2Connection.f22487s = System.nanoTime() + ((long) 1000000000);
                TaskQueue taskQueue = http2Connection.f22480k;
                final String strN = com.google.android.recaptcha.internal.a.n(new StringBuilder(), http2Connection.f22475c, " ping");
                taskQueue.c(new Task(strN) { // from class: okhttp3.internal.http2.Http2Connection$sendDegradedPingLater$$inlined$execute$default$1
                    @Override // okhttp3.internal.concurrent.Task
                    public final long a() {
                        Http2Connection http2Connection2 = http2Connection;
                        http2Connection2.getClass();
                        try {
                            http2Connection2.f22470A.g(2, 0, false);
                            return -1L;
                        } catch (IOException e6) {
                            http2Connection2.b(e6);
                            return -1L;
                        }
                    }
                }, 0L);
            }
        }

        public final void k() throws SocketTimeoutException {
            if (i()) {
                throw new SocketTimeoutException("timeout");
            }
        }
    }

    static {
        new Companion(0);
    }

    public Http2Stream(int i6, Http2Connection connection, boolean z6, boolean z7, Headers headers) {
        o.h(connection, "connection");
        this.f22553a = i6;
        this.f22554b = connection;
        this.f22558f = connection.f22489u.a();
        ArrayDeque arrayDeque = new ArrayDeque();
        this.f22559g = arrayDeque;
        this.f22561i = new FramingSource(connection.f22488t.a(), z7);
        this.f22562j = new FramingSink(z6);
        this.f22563k = new StreamTimeout();
        this.l = new StreamTimeout();
        if (headers == null) {
            if (!g()) {
                throw new IllegalStateException("remotely-initiated streams should have headers");
            }
        } else {
            if (g()) {
                throw new IllegalStateException("locally-initiated streams shouldn't have headers yet");
            }
            arrayDeque.add(headers);
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:15:0x001b  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void a() {
        boolean z6;
        boolean zH;
        byte[] bArr = Util.f22179a;
        synchronized (this) {
            try {
                FramingSource framingSource = this.f22561i;
                if (framingSource.f22571b || !framingSource.f22574e) {
                    z6 = false;
                } else {
                    FramingSink framingSink = this.f22562j;
                    if (framingSink.f22566a || framingSink.f22568c) {
                        z6 = true;
                    }
                }
                zH = h();
            } catch (Throwable th) {
                throw th;
            }
        }
        if (z6) {
            c(ErrorCode.CANCEL, null);
        } else {
            if (zH) {
                return;
            }
            this.f22554b.d(this.f22553a);
        }
    }

    public final void b() throws IOException {
        FramingSink framingSink = this.f22562j;
        if (framingSink.f22568c) {
            throw new IOException("stream closed");
        }
        if (framingSink.f22566a) {
            throw new IOException("stream finished");
        }
        if (this.f22564m != null) {
            IOException iOException = this.f22565n;
            if (iOException != null) {
                throw iOException;
            }
            ErrorCode errorCode = this.f22564m;
            o.e(errorCode);
            throw new StreamResetException(errorCode);
        }
    }

    public final void c(ErrorCode errorCode, IOException iOException) {
        if (d(errorCode, iOException)) {
            this.f22554b.f22470A.i(this.f22553a, errorCode);
        }
    }

    public final boolean d(ErrorCode errorCode, IOException iOException) {
        byte[] bArr = Util.f22179a;
        synchronized (this) {
            if (this.f22564m != null) {
                return false;
            }
            this.f22564m = errorCode;
            this.f22565n = iOException;
            notifyAll();
            if (this.f22561i.f22571b) {
                if (this.f22562j.f22566a) {
                    return false;
                }
            }
            this.f22554b.d(this.f22553a);
            return true;
        }
    }

    public final void e(ErrorCode errorCode) {
        if (d(errorCode, null)) {
            this.f22554b.t(this.f22553a, errorCode);
        }
    }

    public final FramingSink f() {
        synchronized (this) {
            if (!this.f22560h && !g()) {
                throw new IllegalStateException("reply before requesting the sink");
            }
        }
        return this.f22562j;
    }

    public final boolean g() {
        boolean z6 = (this.f22553a & 1) == 1;
        this.f22554b.getClass();
        return true == z6;
    }

    public final synchronized boolean h() {
        if (this.f22564m != null) {
            return false;
        }
        FramingSource framingSource = this.f22561i;
        if (framingSource.f22571b || framingSource.f22574e) {
            FramingSink framingSink = this.f22562j;
            if (framingSink.f22566a || framingSink.f22568c) {
                if (this.f22560h) {
                    return false;
                }
            }
        }
        return true;
    }

    public final void i(Headers headers, boolean z6) {
        boolean zH;
        o.h(headers, "headers");
        byte[] bArr = Util.f22179a;
        synchronized (this) {
            try {
                if (this.f22560h && z6) {
                    this.f22561i.getClass();
                } else {
                    this.f22560h = true;
                    this.f22559g.add(headers);
                }
                if (z6) {
                    this.f22561i.f22571b = true;
                }
                zH = h();
                notifyAll();
            } catch (Throwable th) {
                throw th;
            }
        }
        if (zH) {
            return;
        }
        this.f22554b.d(this.f22553a);
    }

    public final synchronized void j(ErrorCode errorCode) {
        if (this.f22564m == null) {
            this.f22564m = errorCode;
            notifyAll();
        }
    }

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