package ඓ.ⁱ.K.ʬ;

import com.facebook.GraphRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import okio.ByteString;
import ᨨ.AbstractC1483;
import ᨨ.C1492;
import 㿎.AbstractC2651;
import 㿎.C2628;
import 㿎.ሽ.C2540;

/* renamed from: ඓ.ⁱ.K.ʬ.ᨨ  reason: contains not printable characters */
/* compiled from: MultipartBody */
public final class C1232 extends AbstractC2651 {

    /* renamed from: ඓ  reason: contains not printable characters */
    public static final C2628 f4863 = C2628.m6936("multipart/mixed");

    /* renamed from: ⁱ  reason: contains not printable characters */
    public static final byte[] f4864 = {13, 10};

    /* renamed from: 㴹  reason: contains not printable characters */
    public static final byte[] f4865 = {58, 32};

    /* renamed from: 㿎  reason: contains not printable characters */
    public static final byte[] f4866 = {45, 45};

    /* renamed from: ʬ  reason: contains not printable characters */
    public final List<C1233> f4867;

    /* renamed from: ῲ  reason: contains not printable characters */
    public final C2628 f4868;

    /* renamed from: K  reason: contains not printable characters */
    public final ByteString f4869;

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

    /* renamed from: ඓ.ⁱ.K.ʬ.ᨨ$ῲ  reason: contains not printable characters */
    /* compiled from: MultipartBody */
    public static final class C1233 {

        /* renamed from: ῲ  reason: contains not printable characters */
        public final AbstractC2651 f4871;

        /* renamed from: K  reason: contains not printable characters */
        public final C1247 f4872;

        public C1233(C1247 r1, AbstractC2651 r2) {
            this.f4872 = r1;
            this.f4871 = r2;
        }

        /* renamed from: K  reason: contains not printable characters */
        public static C1233 m3307(String str, String str2, AbstractC2651 r7) {
            if (str != null) {
                StringBuilder sb = new StringBuilder("form-data; name=");
                C1232.m3305(sb, str);
                if (str2 != null) {
                    sb.append("; filename=");
                    C1232.m3305(sb, str2);
                }
                String[] strArr = {"Content-Disposition", sb.toString()};
                if (strArr.length % 2 == 0) {
                    String[] strArr2 = (String[]) strArr.clone();
                    for (int i = 0; i < strArr2.length; i++) {
                        if (strArr2[i] != null) {
                            strArr2[i] = strArr2[i].trim();
                        } else {
                            throw new IllegalArgumentException("Headers cannot be null");
                        }
                    }
                    for (int i2 = 0; i2 < strArr2.length; i2 += 2) {
                        String str3 = strArr2[i2];
                        String str4 = strArr2[i2 + 1];
                        if (str3.length() == 0 || str3.indexOf(0) != -1 || str4.indexOf(0) != -1) {
                            throw new IllegalArgumentException("Unexpected header: " + str3 + ": " + str4);
                        }
                    }
                    C1247 r6 = new C1247(strArr2);
                    if (r7 == null) {
                        throw new NullPointerException("body == null");
                    } else if (r6.m3318(GraphRequest.CONTENT_TYPE_HEADER) != null) {
                        throw new IllegalArgumentException("Unexpected header: Content-Type");
                    } else if (r6.m3318("Content-Length") == null) {
                        return new C1233(r6, r7);
                    } else {
                        throw new IllegalArgumentException("Unexpected header: Content-Length");
                    }
                } else {
                    throw new IllegalArgumentException("Expected alternating header names and values");
                }
            } else {
                throw new NullPointerException("name == null");
            }
        }
    }

    static {
        C2628.m6936("multipart/alternative");
        C2628.m6936("multipart/digest");
        C2628.m6936("multipart/parallel");
        C2628.m6936("multipart/form-data");
    }

    public C1232(ByteString byteString, C2628 r4, List<C1233> list) {
        this.f4869 = byteString;
        this.f4868 = C2628.m6937(r4 + "; boundary=" + byteString.utf8());
        this.f4867 = C2540.m6646(list);
    }

    /* renamed from: K  reason: contains not printable characters */
    public static StringBuilder m3305(StringBuilder sb, String str) {
        sb.append('\"');
        int length = str.length();
        for (int i = 0; i < length; i++) {
            char charAt = str.charAt(i);
            if (charAt == '\n') {
                sb.append("%0A");
            } else if (charAt == '\r') {
                sb.append("%0D");
            } else if (charAt != '\"') {
                sb.append(charAt);
            } else {
                sb.append("%22");
            }
        }
        sb.append('\"');
        return sb;
    }

    @Override // 㿎.AbstractC2651
    public long contentLength() throws IOException {
        long j = this.f4870;
        if (j != -1) {
            return j;
        }
        long r0 = m3306((AbstractC1483) null, true);
        this.f4870 = r0;
        return r0;
    }

    @Override // 㿎.AbstractC2651
    public C2628 contentType() {
        return this.f4868;
    }

    @Override // 㿎.AbstractC2651
    public void writeTo(AbstractC1483 r2) throws IOException {
        m3306(r2, false);
    }

    /* renamed from: ඓ.ⁱ.K.ʬ.ᨨ$K  reason: contains not printable characters */
    /* compiled from: MultipartBody */
    public static final class C1234 {

        /* renamed from: ʬ  reason: contains not printable characters */
        public C2628 f4873 = C1232.f4863;

        /* renamed from: ῲ  reason: contains not printable characters */
        public final List<C1233> f4874 = new ArrayList();

        /* renamed from: K  reason: contains not printable characters */
        public final ByteString f4875;

        public C1234() {
            String uuid = UUID.randomUUID().toString();
            this.f4875 = ByteString.encodeUtf8(uuid);
        }

        /* renamed from: K  reason: contains not printable characters */
        public C1234 m3308(String str, String str2) {
            this.f4874.add(C1233.m3307(str, null, AbstractC2651.create((C2628) null, str2)));
            return this;
        }

        /* renamed from: K  reason: contains not printable characters */
        public C1234 m3309(C1233 r2) {
            if (r2 != null) {
                this.f4874.add(r2);
                return this;
            }
            throw new NullPointerException("part == null");
        }
    }

    /* renamed from: K  reason: contains not printable characters */
    public final long m3306(AbstractC1483 r16, boolean z) throws IOException {
        C1492 r2;
        C1492 r1;
        if (z) {
            r1 = new C1492();
            r2 = r1;
        } else {
            r1 = null;
            r2 = r16;
        }
        int size = this.f4867.size();
        long j = 0;
        for (int i = 0; i < size; i++) {
            C1233 r5 = this.f4867.get(i);
            C1247 r9 = r5.f4872;
            AbstractC2651 r52 = r5.f4871;
            r2.write(f4866);
            r2.m3730(this.f4869);
            r2.write(f4864);
            if (r9 != null) {
                int length = r9.f4896.length / 2;
                for (int i2 = 0; i2 < length; i2++) {
                    int i3 = i2 * 2;
                    r2.m3729(r9.f4896[i3]).write(f4865).m3729(r9.f4896[i3 + 1]).write(f4864);
                }
            }
            C2628 contentType = r52.contentType();
            if (contentType != null) {
                r2.m3729("Content-Type: ").m3729(contentType.f8933).write(f4864);
            }
            long contentLength = r52.contentLength();
            if (contentLength != -1) {
                r2.m3729("Content-Length: ").m3726(contentLength).write(f4864);
            } else if (z) {
                r1.m3773();
                return -1;
            }
            r2.write(f4864);
            if (z) {
                j += contentLength;
            } else {
                r52.writeTo(r2);
            }
            r2.write(f4864);
        }
        r2.write(f4866);
        r2.m3730(this.f4869);
        r2.write(f4866);
        r2.write(f4864);
        if (!z) {
            return j;
        }
        long j2 = j + r1.f5529;
        r1.m3773();
        return j2;
    }
}
