package 㿎.ሽ.ⁱ;

import android.support.v4.media.session.PlaybackStateCompat;
import com.facebook.GraphRequest;
import java.io.EOFException;
import java.io.IOException;
import java.net.ProtocolException;
import java.net.Proxy;
import java.util.concurrent.TimeUnit;
import ඓ.ⁱ.K.ඓ.C1252;
import ඓ.K.K.K.C1293;
import ᨨ.AbstractC1473;
import ᨨ.AbstractC1483;
import ᨨ.AbstractC1494;
import ᨨ.AbstractC1496;
import ᨨ.C1477;
import ᨨ.C1480;
import ᨨ.C1486;
import ᨨ.C1492;
import 㿎.AbstractC2533;
import 㿎.AbstractC2537;
import 㿎.AbstractC2632;
import 㿎.C2531;
import 㿎.C2625;
import 㿎.C2644;
import 㿎.C2667;
import 㿎.C2671;
import 㿎.ሽ.AbstractC2570;
import 㿎.ሽ.C2540;
import 㿎.ሽ.ඓ.C2543;
import 㿎.ሽ.ඓ.C2548;
import 㿎.ሽ.㴹.AbstractC2576;
import 㿎.ሽ.㴹.C2577;
import 㿎.ሽ.㴹.C2578;
import 㿎.ሽ.㴹.C2581;

/* renamed from: 㿎.ሽ.ⁱ.K  reason: contains not printable characters */
/* compiled from: Http1Codec */
public final class C2562 implements AbstractC2576 {

    /* renamed from: ʬ  reason: contains not printable characters */
    public final AbstractC1496 f8653;

    /* renamed from: ඓ  reason: contains not printable characters */
    public int f8654 = 0;

    /* renamed from: ῲ  reason: contains not printable characters */
    public final C2548 f8655;

    /* renamed from: K  reason: contains not printable characters */
    public final C2625 f8656;

    /* renamed from: 㴹  reason: contains not printable characters */
    public long f8657 = PlaybackStateCompat.ACTION_SET_REPEAT_MODE;

    /* renamed from: 㸾  reason: contains not printable characters */
    public final AbstractC1483 f8658;

    /* renamed from: 㿎.ሽ.ⁱ.K$ʬ  reason: contains not printable characters */
    /* compiled from: Http1Codec */
    public final class C2563 implements AbstractC1473 {

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

        /* renamed from: ῲ  reason: contains not printable characters */
        public final C1480 f8660 = new C1480(C2562.this.f8658.timeout());

        public C2563() {
        }

        @Override // ᨨ.AbstractC1473, java.io.Closeable, java.lang.AutoCloseable
        public synchronized void close() throws IOException {
            if (!this.f8659) {
                this.f8659 = true;
                C2562.this.f8658.m3729("0\r\n\r\n");
                C2562.this.m6749(this.f8660);
                C2562.this.f8654 = 3;
            }
        }

        @Override // ᨨ.AbstractC1473, java.io.Flushable
        public synchronized void flush() throws IOException {
            if (!this.f8659) {
                C2562.this.f8658.flush();
            }
        }

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

        @Override // ᨨ.AbstractC1473
        /* renamed from: K  reason: contains not printable characters */
        public void m6753(C1492 r4, long j) throws IOException {
            if (this.f8659) {
                throw new IllegalStateException("closed");
            } else if (j != 0) {
                C2562.this.f8658.m3728(j);
                C2562.this.f8658.m3729("\r\n");
                C2562.this.f8658.m3692(r4, j);
                C2562.this.f8658.m3729("\r\n");
            }
        }
    }

    /* renamed from: 㿎.ሽ.ⁱ.K$ඓ  reason: contains not printable characters */
    /* compiled from: Http1Codec */
    public final class C2564 implements AbstractC1473 {

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

        /* renamed from: ῲ  reason: contains not printable characters */
        public final C1480 f8664 = new C1480(C2562.this.f8658.timeout());

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

        public C2564(long j) {
            this.f8665 = j;
        }

        @Override // ᨨ.AbstractC1473, java.io.Closeable, java.lang.AutoCloseable
        public void close() throws IOException {
            if (!this.f8662) {
                this.f8662 = true;
                if (this.f8665 <= 0) {
                    C2562.this.m6749(this.f8664);
                    C2562.this.f8654 = 3;
                    return;
                }
                throw new ProtocolException("unexpected end of stream");
            }
        }

        @Override // ᨨ.AbstractC1473, java.io.Flushable
        public void flush() throws IOException {
            if (!this.f8662) {
                C2562.this.f8658.flush();
            }
        }

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

        @Override // ᨨ.AbstractC1473
        /* renamed from: K  reason: contains not printable characters */
        public void m6754(C1492 r8, long j) throws IOException {
            if (!this.f8662) {
                C2540.m6650(r8.f5529, 0, j);
                if (j <= this.f8665) {
                    C2562.this.f8658.m3692(r8, j);
                    this.f8665 -= j;
                    return;
                }
                StringBuilder r0 = C1293.m3416("expected ");
                r0.append(this.f8665);
                r0.append(" bytes but received ");
                r0.append(j);
                throw new ProtocolException(r0.toString());
            }
            throw new IllegalStateException("closed");
        }
    }

    /* renamed from: 㿎.ሽ.ⁱ.K$ῲ  reason: contains not printable characters */
    /* compiled from: Http1Codec */
    public abstract class AbstractC2565 implements AbstractC1494 {

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

        /* renamed from: ῲ  reason: contains not printable characters */
        public final C1480 f8668 = new C1480(C2562.this.f8653.timeout());

        /* renamed from: 㸾  reason: contains not printable characters */
        public long f8669 = 0;

        public /* synthetic */ AbstractC2565(C2567 k) {
        }

        @Override // ᨨ.AbstractC1494
        public long read(C1492 r3, long j) throws IOException {
            try {
                long read = C2562.this.f8653.read(r3, j);
                if (read > 0) {
                    this.f8669 += read;
                }
                return read;
            } catch (IOException e) {
                m6755(false, e);
                throw e;
            }
        }

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

        /* renamed from: K  reason: contains not printable characters */
        public final void m6755(boolean z, IOException iOException) throws IOException {
            C2562 k = C2562.this;
            int i = k.f8654;
            if (i != 6) {
                if (i == 5) {
                    k.m6749(this.f8668);
                    C2562 k2 = C2562.this;
                    k2.f8654 = 6;
                    C2548 r3 = k2.f8655;
                    if (r3 != null) {
                        r3.m6686(!z, k2, this.f8669, iOException);
                        return;
                    }
                    return;
                }
                StringBuilder r11 = C1293.m3416("state: ");
                r11.append(C2562.this.f8654);
                throw new IllegalStateException(r11.toString());
            }
        }
    }

    /* renamed from: 㿎.ሽ.ⁱ.K$ⁱ  reason: contains not printable characters */
    /* compiled from: Http1Codec */
    public class C2566 extends AbstractC2565 {

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

        public C2566(C2562 k) {
            super(null);
        }

        @Override // ᨨ.AbstractC1494, java.io.Closeable, java.lang.AutoCloseable
        public void close() throws IOException {
            if (!this.f8666) {
                if (!this.f8670) {
                    m6755(false, null);
                }
                this.f8666 = true;
            }
        }

        @Override // 㿎.ሽ.ⁱ.C2562.AbstractC2565, ᨨ.AbstractC1494
        public long read(C1492 r4, long j) throws IOException {
            if (j < 0) {
                throw new IllegalArgumentException(C1293.m3409("byteCount < 0: ", j));
            } else if (this.f8666) {
                throw new IllegalStateException("closed");
            } else if (this.f8670) {
                return -1;
            } else {
                long read = super.read(r4, j);
                if (read != -1) {
                    return read;
                }
                this.f8670 = true;
                m6755(true, null);
                return -1;
            }
        }
    }

    /* renamed from: 㿎.ሽ.ⁱ.K$㴹  reason: contains not printable characters */
    /* compiled from: Http1Codec */
    public class C2568 extends AbstractC2565 {

        /* renamed from: 㴹  reason: contains not printable characters */
        public long f8671;

        public C2568(C2562 k, long j) throws IOException {
            super(null);
            this.f8671 = j;
            if (this.f8671 == 0) {
                m6755(true, null);
            }
        }

        @Override // ᨨ.AbstractC1494, java.io.Closeable, java.lang.AutoCloseable
        public void close() throws IOException {
            if (!this.f8666) {
                if (this.f8671 != 0 && !C2540.m6655(this, 100, TimeUnit.MILLISECONDS)) {
                    m6755(false, null);
                }
                this.f8666 = true;
            }
        }

        @Override // 㿎.ሽ.ⁱ.C2562.AbstractC2565, ᨨ.AbstractC1494
        public long read(C1492 r8, long j) throws IOException {
            if (j < 0) {
                throw new IllegalArgumentException(C1293.m3409("byteCount < 0: ", j));
            } else if (!this.f8666) {
                long j2 = this.f8671;
                if (j2 == 0) {
                    return -1;
                }
                long read = super.read(r8, Math.min(j2, j));
                if (read != -1) {
                    this.f8671 -= read;
                    if (this.f8671 == 0) {
                        m6755(true, null);
                    }
                    return read;
                }
                ProtocolException protocolException = new ProtocolException("unexpected end of stream");
                m6755(false, protocolException);
                throw protocolException;
            } else {
                throw new IllegalStateException("closed");
            }
        }
    }

    /* renamed from: 㿎.ሽ.ⁱ.K$㸾  reason: contains not printable characters */
    /* compiled from: Http1Codec */
    public class C2569 extends AbstractC2565 {

        /* renamed from: ⁱ  reason: contains not printable characters */
        public long f8673 = -1;

        /* renamed from: 㴹  reason: contains not printable characters */
        public final C2531 f8674;

        /* renamed from: 㿎  reason: contains not printable characters */
        public boolean f8675 = true;

        public C2569(C2531 r4) {
            super(null);
            this.f8674 = r4;
        }

        @Override // ᨨ.AbstractC1494, java.io.Closeable, java.lang.AutoCloseable
        public void close() throws IOException {
            if (!this.f8666) {
                if (this.f8675 && !C2540.m6655(this, 100, TimeUnit.MILLISECONDS)) {
                    m6755(false, null);
                }
                this.f8666 = true;
            }
        }

        @Override // 㿎.ሽ.ⁱ.C2562.AbstractC2565, ᨨ.AbstractC1494
        public long read(C1492 r10, long j) throws IOException {
            if (j < 0) {
                throw new IllegalArgumentException(C1293.m3409("byteCount < 0: ", j));
            } else if (this.f8666) {
                throw new IllegalStateException("closed");
            } else if (!this.f8675) {
                return -1;
            } else {
                long j2 = this.f8673;
                if (j2 == 0 || j2 == -1) {
                    if (this.f8673 != -1) {
                        C2562.this.f8653.m3810();
                    }
                    try {
                        this.f8673 = C2562.this.f8653.m3815();
                        String trim = C2562.this.f8653.m3810().trim();
                        if (this.f8673 < 0 || (!trim.isEmpty() && !trim.startsWith(";"))) {
                            throw new ProtocolException("expected chunk size and optional extensions but was \"" + this.f8673 + trim + "\"");
                        }
                        if (this.f8673 == 0) {
                            this.f8675 = false;
                            C2577.m6784(C2562.this.f8656.m6921(), this.f8674, C2562.this.m6752());
                            m6755(true, null);
                        }
                        if (!this.f8675) {
                            return -1;
                        }
                    } catch (NumberFormatException e) {
                        throw new ProtocolException(e.getMessage());
                    }
                }
                long read = super.read(r10, Math.min(j, this.f8673));
                if (read != -1) {
                    this.f8673 -= read;
                    return read;
                }
                ProtocolException protocolException = new ProtocolException("unexpected end of stream");
                m6755(false, protocolException);
                throw protocolException;
            }
        }
    }

    public C2562(C2625 r3, C2548 r4, AbstractC1496 r5, AbstractC1483 r6) {
        this.f8656 = r3;
        this.f8655 = r4;
        this.f8653 = r5;
        this.f8658 = r6;
    }

    @Override // 㿎.ሽ.㴹.AbstractC2576
    public void cancel() {
        C2543 r0 = this.f8655.m6676();
        if (r0 != null) {
            C2540.m6652(r0.f8590);
        }
    }

    /* renamed from: ʬ  reason: contains not printable characters */
    public final String m6742() throws IOException {
        String r0 = this.f8653.m3811(this.f8657);
        this.f8657 -= (long) r0.length();
        return r0;
    }

    @Override // 㿎.ሽ.㴹.AbstractC2576
    /* renamed from: ῲ  reason: contains not printable characters */
    public void m6743() throws IOException {
        this.f8658.flush();
    }

    /* renamed from: K  reason: contains not printable characters */
    public void m6749(C1480 r3) {
        C1477 r0 = r3.f5509;
        C1477 r1 = C1477.f5502;
        if (r1 != null) {
            r3.f5509 = r1;
            r0.m3703();
            r0.m3702();
            return;
        }
        throw new IllegalArgumentException("delegate == null");
    }

    /* renamed from: 㸾  reason: contains not printable characters */
    public C2667 m6752() throws IOException {
        C2667.C2668 k = new C2667.C2668();
        while (true) {
            String r1 = m6742();
            if (r1.length() == 0) {
                return new C2667(k);
            }
            AbstractC2570.f8676.m6762(k, r1);
        }
    }

    @Override // 㿎.ሽ.㴹.AbstractC2576
    /* renamed from: K  reason: contains not printable characters */
    public AbstractC1473 m6744(C2644 r6, long j) {
        if ("chunked".equalsIgnoreCase(r6.f8982.m6996("Transfer-Encoding"))) {
            if (this.f8654 == 1) {
                this.f8654 = 2;
                return new C2563();
            }
            StringBuilder r7 = C1293.m3416("state: ");
            r7.append(this.f8654);
            throw new IllegalStateException(r7.toString());
        } else if (j == -1) {
            throw new IllegalStateException("Cannot stream a request body without chunked encoding or a known content length!");
        } else if (this.f8654 == 1) {
            this.f8654 = 2;
            return new C2564(j);
        } else {
            StringBuilder r72 = C1293.m3416("state: ");
            r72.append(this.f8654);
            throw new IllegalStateException(r72.toString());
        }
    }

    @Override // 㿎.ሽ.㴹.AbstractC2576
    /* renamed from: K  reason: contains not printable characters */
    public void m6750(C2644 r4) throws IOException {
        Proxy.Type type = this.f8655.m6676().f8579.f8558.type();
        StringBuilder sb = new StringBuilder();
        sb.append(r4.f8984);
        sb.append(' ');
        if (!r4.m6962() && type == Proxy.Type.HTTP) {
            sb.append(r4.f8985);
        } else {
            sb.append(C1252.m3342(r4.f8985));
        }
        sb.append(" HTTP/1.1");
        m6751(r4.f8982, sb.toString());
    }

    @Override // 㿎.ሽ.㴹.AbstractC2576
    /* renamed from: K  reason: contains not printable characters */
    public AbstractC2632 m6746(C2671 r10) throws IOException {
        C2548 r0 = this.f8655;
        AbstractC2533 r1 = r0.f8618;
        AbstractC2537 r02 = r0.f8610;
        r1.m6610();
        String r03 = r10.f9088.m6996(GraphRequest.CONTENT_TYPE_HEADER);
        String str = null;
        if (r03 == null) {
            r03 = null;
        }
        if (!C2577.m6780(r10)) {
            return new C2581(r03, 0, C1486.m3741(m6745(0)));
        }
        String r2 = r10.f9088.m6996("Transfer-Encoding");
        if (r2 != null) {
            str = r2;
        }
        if ("chunked".equalsIgnoreCase(str)) {
            C2531 r102 = r10.f9087.f8985;
            if (this.f8654 == 4) {
                this.f8654 = 5;
                return new C2581(r03, -1, C1486.m3741(new C2569(r102)));
            }
            StringBuilder r04 = C1293.m3416("state: ");
            r04.append(this.f8654);
            throw new IllegalStateException(r04.toString());
        }
        long r7 = C2577.m6783(r10);
        if (r7 != -1) {
            return new C2581(r03, r7, C1486.m3741(m6745(r7)));
        }
        if (this.f8654 == 4) {
            C2548 r12 = this.f8655;
            if (r12 != null) {
                this.f8654 = 5;
                r12.m6687();
                return new C2581(r03, -1, C1486.m3741(new C2566(this)));
            }
            throw new IllegalStateException("streamAllocation == null");
        }
        StringBuilder r05 = C1293.m3416("state: ");
        r05.append(this.f8654);
        throw new IllegalStateException(r05.toString());
    }

    @Override // 㿎.ሽ.㴹.AbstractC2576
    /* renamed from: K  reason: contains not printable characters */
    public void m6748() throws IOException {
        this.f8658.flush();
    }

    /* renamed from: K  reason: contains not printable characters */
    public void m6751(C2667 r5, String str) throws IOException {
        if (this.f8654 == 0) {
            this.f8658.m3729(str).m3729("\r\n");
            int r1 = r5.m6993();
            for (int i = 0; i < r1; i++) {
                this.f8658.m3729(r5.m6995(i)).m3729(": ").m3729(r5.m6994(i)).m3729("\r\n");
            }
            this.f8658.m3729("\r\n");
            this.f8654 = 1;
            return;
        }
        StringBuilder r6 = C1293.m3416("state: ");
        r6.append(this.f8654);
        throw new IllegalStateException(r6.toString());
    }

    @Override // 㿎.ሽ.㴹.AbstractC2576
    /* renamed from: K  reason: contains not printable characters */
    public C2671.C2672 m6747(boolean z) throws IOException {
        int i = this.f8654;
        if (i == 1 || i == 3) {
            try {
                C2578 r0 = C2578.m6785(m6742());
                C2671.C2672 k = new C2671.C2672();
                k.f9099 = r0.f8682;
                k.f9095 = r0.f8681;
                k.f9105 = r0.f8680;
                k.m7008(m6752());
                if (z && r0.f8681 == 100) {
                    return null;
                }
                if (r0.f8681 == 100) {
                    this.f8654 = 3;
                    return k;
                }
                this.f8654 = 4;
                return k;
            } catch (EOFException e) {
                StringBuilder r1 = C1293.m3416("unexpected end of stream on ");
                r1.append(this.f8655);
                IOException iOException = new IOException(r1.toString());
                iOException.initCause(e);
                throw iOException;
            }
        } else {
            StringBuilder r02 = C1293.m3416("state: ");
            r02.append(this.f8654);
            throw new IllegalStateException(r02.toString());
        }
    }

    /* renamed from: K  reason: contains not printable characters */
    public AbstractC1494 m6745(long j) throws IOException {
        if (this.f8654 == 4) {
            this.f8654 = 5;
            return new C2568(this, j);
        }
        StringBuilder r4 = C1293.m3416("state: ");
        r4.append(this.f8654);
        throw new IllegalStateException(r4.toString());
    }
}
