package 㿎.ሽ.㿎;

import android.support.v4.media.session.PlaybackStateCompat;
import java.io.EOFException;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.List;
import okhttp3.internal.http2.ErrorCode;
import okhttp3.internal.http2.StreamResetException;
import ඓ.K.K.K.C1293;
import ᨨ.AbstractC1473;
import ᨨ.AbstractC1494;
import ᨨ.AbstractC1496;
import ᨨ.C1470;
import ᨨ.C1477;
import ᨨ.C1492;

/* renamed from: 㿎.ሽ.㿎.㿎  reason: contains not printable characters */
/* compiled from: Http2Stream */
public final class C2621 {

    /* renamed from: ʬ  reason: contains not printable characters */
    public final int f8851;

    /* renamed from: ඓ  reason: contains not printable characters */
    public List<C2614> f8852;

    /* renamed from: ᕣ  reason: contains not printable characters */
    public ErrorCode f8853 = null;

    /* renamed from: ᨨ  reason: contains not printable characters */
    public final C2622 f8854 = new C2622();

    /* renamed from: ῲ  reason: contains not printable characters */
    public long f8855;

    /* renamed from: ⁱ  reason: contains not printable characters */
    public final C2623 f8856;

    /* renamed from: K  reason: contains not printable characters */
    public long f8857 = 0;

    /* renamed from: 㳳  reason: contains not printable characters */
    public final C2622 f8858 = new C2622();

    /* renamed from: 㴹  reason: contains not printable characters */
    public boolean f8859;

    /* renamed from: 㸾  reason: contains not printable characters */
    public final C2591 f8860;

    /* renamed from: 㿎  reason: contains not printable characters */
    public final C2624 f8861;

    /* renamed from: 㿎.ሽ.㿎.㿎$ʬ  reason: contains not printable characters */
    /* compiled from: Http2Stream */
    public class C2622 extends C1470 {
        public C2622() {
        }

        @Override // ᨨ.C1470
        /* renamed from: K  reason: contains not printable characters */
        public IOException m6911(IOException iOException) {
            SocketTimeoutException socketTimeoutException = new SocketTimeoutException("timeout");
            if (iOException != null) {
                socketTimeoutException.initCause(iOException);
            }
            return socketTimeoutException;
        }

        /* renamed from: 㳳  reason: contains not printable characters */
        public void m6912() throws IOException {
            if (m3686()) {
                throw m6911((IOException) null);
            }
        }

        @Override // ᨨ.C1470
        /* renamed from: 㿎  reason: contains not printable characters */
        public void m6913() {
            C2621.this.m6899(ErrorCode.CANCEL);
        }
    }

    /* renamed from: 㿎.ሽ.㿎.㿎$ῲ  reason: contains not printable characters */
    /* compiled from: Http2Stream */
    public final class C2623 implements AbstractC1494 {

        /* renamed from: ʬ  reason: contains not printable characters */
        public final C1492 f8863 = new C1492();

        /* renamed from: ඓ  reason: contains not printable characters */
        public boolean f8864;

        /* renamed from: ῲ  reason: contains not printable characters */
        public final C1492 f8865 = new C1492();

        /* renamed from: 㴹  reason: contains not printable characters */
        public boolean f8867;

        /* renamed from: 㸾  reason: contains not printable characters */
        public final long f8868;

        public C2623(long j) {
            this.f8868 = j;
        }

        @Override // ᨨ.AbstractC1494, java.io.Closeable, java.lang.AutoCloseable
        public void close() throws IOException {
            long j;
            synchronized (C2621.this) {
                this.f8864 = true;
                j = this.f8863.f5529;
                this.f8863.m3773();
                C2621.this.notifyAll();
            }
            if (j > 0) {
                C2621.this.f8860.m6811(j);
            }
            C2621.this.m6904();
        }

        @Override // ᨨ.AbstractC1494
        public long read(C1492 r11, long j) throws IOException {
            ErrorCode errorCode;
            long j2;
            if (j >= 0) {
                synchronized (C2621.this) {
                    m6914();
                    if (!this.f8864) {
                        errorCode = C2621.this.f8853;
                        if (this.f8863.f5529 > 0) {
                            j2 = this.f8863.read(r11, Math.min(j, this.f8863.f5529));
                            C2621.this.f8857 += j2;
                        } else {
                            j2 = -1;
                        }
                        if (errorCode == null && C2621.this.f8857 >= ((long) (C2621.this.f8860.f8728.m6884() / 2))) {
                            C2621.this.f8860.m6814(C2621.this.f8851, C2621.this.f8857);
                            C2621.this.f8857 = 0;
                        }
                    } else {
                        throw new IOException("stream closed");
                    }
                }
                if (j2 != -1) {
                    C2621.this.f8860.m6811(j2);
                    return j2;
                } else if (errorCode == null) {
                    return -1;
                } else {
                    throw new StreamResetException(errorCode);
                }
            } else {
                throw new IllegalArgumentException(C1293.m3409("byteCount < 0: ", j));
            }
        }

        @Override // ᨨ.AbstractC1494
        public C1477 timeout() {
            return C2621.this.f8854;
        }

        /* renamed from: ᨨ  reason: contains not printable characters */
        public final void m6914() throws IOException {
            C2621.this.f8854.m3689();
            while (this.f8863.f5529 == 0 && !this.f8867 && !this.f8864 && C2621.this.f8853 == null) {
                try {
                    C2621.this.m6910();
                } finally {
                    C2621.this.f8854.m6912();
                }
            }
        }

        /* renamed from: K  reason: contains not printable characters */
        public void m6915(AbstractC1496 r12, long j) throws IOException {
            boolean z;
            boolean z2;
            boolean z3;
            while (j > 0) {
                synchronized (C2621.this) {
                    z = this.f8867;
                    z2 = true;
                    z3 = this.f8863.f5529 + j > this.f8868;
                }
                if (z3) {
                    r12.skip(j);
                    C2621.this.m6899(ErrorCode.FLOW_CONTROL_ERROR);
                    return;
                } else if (z) {
                    r12.skip(j);
                    return;
                } else {
                    long read = r12.read(this.f8865, j);
                    if (read != -1) {
                        j -= read;
                        synchronized (C2621.this) {
                            if (this.f8863.f5529 != 0) {
                                z2 = false;
                            }
                            this.f8863.m3783((AbstractC1494) this.f8865);
                            if (z2) {
                                C2621.this.notifyAll();
                            }
                        }
                    } else {
                        throw new EOFException();
                    }
                }
            }
        }
    }

    public C2621(int i, C2591 r4, boolean z, boolean z2, List<C2614> list) {
        if (r4 == null) {
            throw new NullPointerException("connection == null");
        } else if (list != null) {
            this.f8851 = i;
            this.f8860 = r4;
            this.f8855 = (long) r4.f8719.m6884();
            this.f8856 = new C2623((long) r4.f8728.m6884());
            this.f8861 = new C2624();
            this.f8856.f8867 = z2;
            this.f8861.f8872 = z;
        } else {
            throw new NullPointerException("requestHeaders == null");
        }
    }

    /* renamed from: ʬ  reason: contains not printable characters */
    public AbstractC1473 m6898() {
        synchronized (this) {
            if (!this.f8859) {
                if (!m6909()) {
                    throw new IllegalStateException("reply before requesting the sink");
                }
            }
        }
        return this.f8861;
    }

    /* renamed from: ඓ  reason: contains not printable characters */
    public synchronized boolean m6900() {
        if (this.f8853 != null) {
            return false;
        }
        if ((this.f8856.f8867 || this.f8856.f8864) && ((this.f8861.f8872 || this.f8861.f8869) && this.f8859)) {
            return false;
        }
        return true;
    }

    /* renamed from: ῲ  reason: contains not printable characters */
    public final boolean m6902(ErrorCode errorCode) {
        synchronized (this) {
            if (this.f8853 != null) {
                return false;
            }
            if (this.f8856.f8867 && this.f8861.f8872) {
                return false;
            }
            this.f8853 = errorCode;
            notifyAll();
            this.f8860.m6805(this.f8851);
            return true;
        }
    }

    /* JADX INFO: finally extract failed */
    /* renamed from: ⁱ  reason: contains not printable characters */
    public synchronized List<C2614> m6903() throws IOException {
        List<C2614> list;
        if (m6909()) {
            this.f8854.m3689();
            while (this.f8852 == null && this.f8853 == null) {
                try {
                    m6910();
                } catch (Throwable th) {
                    this.f8854.m6912();
                    throw th;
                }
            }
            this.f8854.m6912();
            list = this.f8852;
            if (list != null) {
                this.f8852 = null;
            } else {
                throw new StreamResetException(this.f8853);
            }
        } else {
            throw new IllegalStateException("servers cannot read response headers");
        }
        return list;
    }

    /* renamed from: K  reason: contains not printable characters */
    public void m6906(ErrorCode errorCode) throws IOException {
        if (m6902(errorCode)) {
            C2591 r0 = this.f8860;
            r0.f8720.m6855(this.f8851, errorCode);
        }
    }

    /* renamed from: 㴹  reason: contains not printable characters */
    public void m6907() {
        boolean r0;
        synchronized (this) {
            this.f8856.f8867 = true;
            r0 = m6900();
            notifyAll();
        }
        if (!r0) {
            this.f8860.m6805(this.f8851);
        }
    }

    /* renamed from: 㸾  reason: contains not printable characters */
    public boolean m6909() {
        if (this.f8860.f8725 == ((this.f8851 & 1) == 1)) {
            return true;
        }
        return false;
    }

    /* renamed from: 㿎  reason: contains not printable characters */
    public void m6910() throws InterruptedIOException {
        try {
            wait();
        } catch (InterruptedException unused) {
            Thread.currentThread().interrupt();
            throw new InterruptedIOException();
        }
    }

    /* renamed from: 㸾  reason: contains not printable characters */
    public synchronized void m6908(ErrorCode errorCode) {
        if (this.f8853 == null) {
            this.f8853 = errorCode;
            notifyAll();
        }
    }

    /* renamed from: 㿎.ሽ.㿎.㿎$K  reason: contains not printable characters */
    /* compiled from: Http2Stream */
    public final class C2624 implements AbstractC1473 {

        /* renamed from: ʬ  reason: contains not printable characters */
        public boolean f8869;

        /* renamed from: ῲ  reason: contains not printable characters */
        public final C1492 f8871 = new C1492();

        /* renamed from: 㸾  reason: contains not printable characters */
        public boolean f8872;

        public C2624() {
        }

        /* JADX WARNING: Code restructure failed: missing block: B:11:0x001b, code lost:
            if (r13.f8871.f5529 <= 0) goto L_0x0029;
         */
        /* JADX WARNING: Code restructure failed: missing block: B:13:0x0023, code lost:
            if (r13.f8871.f5529 <= 0) goto L_0x0034;
         */
        /* JADX WARNING: Code restructure failed: missing block: B:14:0x0025, code lost:
            m6917(true);
         */
        /* JADX WARNING: Code restructure failed: missing block: B:15:0x0029, code lost:
            r0.f8860.m6818(r0.f8851, true, (ᨨ.C1492) null, 0L);
         */
        /* JADX WARNING: Code restructure failed: missing block: B:16:0x0034, code lost:
            r1 = r13.f8870;
         */
        /* JADX WARNING: Code restructure failed: missing block: B:17:0x0036, code lost:
            monitor-enter(r1);
         */
        /* JADX WARNING: Code restructure failed: missing block: B:19:?, code lost:
            r13.f8869 = true;
         */
        /* JADX WARNING: Code restructure failed: missing block: B:20:0x0039, code lost:
            monitor-exit(r1);
         */
        /* JADX WARNING: Code restructure failed: missing block: B:21:0x003a, code lost:
            r13.f8870.f8860.f8720.flush();
            r13.f8870.m6904();
         */
        /* JADX WARNING: Code restructure failed: missing block: B:22:0x0048, code lost:
            return;
         */
        /* JADX WARNING: Code restructure failed: missing block: B:8:0x000a, code lost:
            r0 = r13.f8870;
         */
        /* JADX WARNING: Code restructure failed: missing block: B:9:0x0011, code lost:
            if (r0.f8861.f8872 != false) goto L_0x0034;
         */
        @Override // ᨨ.AbstractC1473, java.io.Closeable, java.lang.AutoCloseable
        public void close() throws IOException {
            synchronized (C2621.this) {
                if (this.f8869) {
                }
            }
        }

        @Override // ᨨ.AbstractC1473, java.io.Flushable
        public void flush() throws IOException {
            synchronized (C2621.this) {
                C2621.this.m6901();
            }
            while (this.f8871.f5529 > 0) {
                m6917(false);
                C2621.this.f8860.flush();
            }
        }

        @Override // ᨨ.AbstractC1473
        public C1477 timeout() {
            return C2621.this.f8858;
        }

        @Override // ᨨ.AbstractC1473
        /* renamed from: K  reason: contains not printable characters */
        public void m6916(C1492 r3, long j) throws IOException {
            this.f8871.m3797(r3, j);
            while (this.f8871.f5529 >= PlaybackStateCompat.ACTION_PREPARE) {
                m6917(false);
            }
        }

        /* JADX INFO: finally extract failed */
        /* renamed from: K  reason: contains not printable characters */
        public final void m6917(boolean z) throws IOException {
            long min;
            synchronized (C2621.this) {
                C2621.this.f8858.m3689();
                while (C2621.this.f8855 <= 0 && !this.f8872 && !this.f8869 && C2621.this.f8853 == null) {
                    try {
                        C2621.this.m6910();
                    } catch (Throwable th) {
                        C2621.this.f8858.m6912();
                        throw th;
                    }
                }
                C2621.this.f8858.m6912();
                C2621.this.m6901();
                min = Math.min(C2621.this.f8855, this.f8871.f5529);
                C2621.this.f8855 -= min;
            }
            C2621.this.f8858.m3689();
            try {
                C2621.this.f8860.m6818(C2621.this.f8851, z && min == this.f8871.f5529, this.f8871, min);
            } finally {
                C2621.this.f8858.m6912();
            }
        }
    }

    /* renamed from: K  reason: contains not printable characters */
    public void m6905(List<C2614> list) {
        boolean z;
        synchronized (this) {
            z = true;
            this.f8859 = true;
            if (this.f8852 == null) {
                this.f8852 = list;
                z = m6900();
                notifyAll();
            } else {
                ArrayList arrayList = new ArrayList();
                arrayList.addAll(this.f8852);
                arrayList.add(null);
                arrayList.addAll(list);
                this.f8852 = arrayList;
            }
        }
        if (!z) {
            this.f8860.m6805(this.f8851);
        }
    }

    /* renamed from: ʬ  reason: contains not printable characters */
    public void m6899(ErrorCode errorCode) {
        if (m6902(errorCode)) {
            this.f8860.m6809(this.f8851, errorCode);
        }
    }

    /* renamed from: ῲ  reason: contains not printable characters */
    public void m6901() throws IOException {
        C2624 k = this.f8861;
        if (k.f8869) {
            throw new IOException("stream closed");
        } else if (!k.f8872) {
            ErrorCode errorCode = this.f8853;
            if (errorCode != null) {
                throw new StreamResetException(errorCode);
            }
        } else {
            throw new IOException("stream finished");
        }
    }

    /* renamed from: K  reason: contains not printable characters */
    public void m6904() throws IOException {
        boolean z;
        boolean r1;
        synchronized (this) {
            z = !this.f8856.f8867 && this.f8856.f8864 && (this.f8861.f8872 || this.f8861.f8869);
            r1 = m6900();
        }
        if (z) {
            m6906(ErrorCode.CANCEL);
        } else if (!r1) {
            this.f8860.m6805(this.f8851);
        }
    }
}
