package okhttp3;

import androidx.navigation.C1134i;
import java.io.EOFException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import kotlin.jvm.internal.o;
import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.internal.Util;
import p5.C2740j;
import p5.C2743m;
import p5.InterfaceC2741k;

/* JADX INFO: loaded from: classes3.dex */
public final class MultipartBody extends RequestBody {

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public static final Companion f22049f = new Companion(0);

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public static final MediaType f22050g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public static final MediaType f22051h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public static final byte[] f22052i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public static final byte[] f22053j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public static final byte[] f22054k;

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

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final List f22056c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final MediaType f22057d;

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

    public static final class Builder {

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

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

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        public final ArrayList f22061c;

        public Builder() {
            this(0);
        }

        public final void a(String name, String value) {
            o.h(name, "name");
            o.h(value, "value");
            Part.f22062c.getClass();
            RequestBody.f22136a.getClass();
            RequestBody$Companion$toRequestBody$2 requestBody$Companion$toRequestBody$2A = RequestBody.Companion.a(value, null);
            StringBuilder sb = new StringBuilder("form-data; name=\"");
            MultipartBody.f22049f.getClass();
            int length = name.length();
            for (int i6 = 0; i6 < length; i6++) {
                char cCharAt = name.charAt(i6);
                if (cCharAt == '\n') {
                    sb.append("%0A");
                } else if (cCharAt == '\r') {
                    sb.append("%0D");
                } else if (cCharAt == '\"') {
                    sb.append("%22");
                } else {
                    sb.append(cCharAt);
                }
            }
            sb.append('\"');
            String string = sb.toString();
            o.g(string, "StringBuilder().apply(builderAction).toString()");
            Headers.Builder builder = new Headers.Builder();
            builder.d("Content-Disposition", string);
            Headers headersE = builder.e();
            if (headersE.g("Content-Type") != null) {
                throw new IllegalArgumentException("Unexpected header: Content-Type");
            }
            if (headersE.g("Content-Length") != null) {
                throw new IllegalArgumentException("Unexpected header: Content-Length");
            }
            this.f22061c.add(new Part(headersE, requestBody$Companion$toRequestBody$2A));
        }

        public final MultipartBody b() {
            ArrayList arrayList = this.f22061c;
            if (arrayList.isEmpty()) {
                throw new IllegalStateException("Multipart body must have at least one part.");
            }
            return new MultipartBody(this.f22059a, this.f22060b, Util.y(arrayList));
        }

        public final void c(MediaType type) {
            o.h(type, "type");
            if (type.f22047b.equals("multipart")) {
                this.f22060b = type;
            } else {
                throw new IllegalArgumentException(("multipart != " + type).toString());
            }
        }

        public Builder(int i6) {
            String string = UUID.randomUUID().toString();
            o.g(string, "randomUUID().toString()");
            C2743m c2743m = C2743m.f23101d;
            this.f22059a = C1134i.n(string);
            this.f22060b = MultipartBody.f22050g;
            this.f22061c = new ArrayList();
        }
    }

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

        private Companion() {
        }
    }

    public static final class Part {

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        public static final Companion f22062c = new Companion(0);

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

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        public final RequestBody$Companion$toRequestBody$2 f22064b;

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

            private Companion() {
            }
        }

        public Part(Headers headers, RequestBody$Companion$toRequestBody$2 requestBody$Companion$toRequestBody$2) {
            this.f22063a = headers;
            this.f22064b = requestBody$Companion$toRequestBody$2;
        }
    }

    static {
        MediaType.f22043d.getClass();
        f22050g = MediaType.Companion.a("multipart/mixed");
        MediaType.Companion.a("multipart/alternative");
        MediaType.Companion.a("multipart/digest");
        MediaType.Companion.a("multipart/parallel");
        f22051h = MediaType.Companion.a("multipart/form-data");
        f22052i = new byte[]{58, 32};
        f22053j = new byte[]{13, 10};
        f22054k = new byte[]{45, 45};
    }

    public MultipartBody(C2743m boundaryByteString, MediaType type, List list) {
        o.h(boundaryByteString, "boundaryByteString");
        o.h(type, "type");
        this.f22055b = boundaryByteString;
        this.f22056c = list;
        MediaType.Companion companion = MediaType.f22043d;
        String str = type + "; boundary=" + boundaryByteString.t();
        companion.getClass();
        this.f22057d = MediaType.Companion.a(str);
        this.f22058e = -1L;
    }

    @Override // okhttp3.RequestBody
    public final long a() throws EOFException {
        long j4 = this.f22058e;
        if (j4 != -1) {
            return j4;
        }
        long jD = d(null, true);
        this.f22058e = jD;
        return jD;
    }

    @Override // okhttp3.RequestBody
    public final MediaType b() {
        return this.f22057d;
    }

    @Override // okhttp3.RequestBody
    public final void c(InterfaceC2741k interfaceC2741k) throws EOFException {
        d(interfaceC2741k, false);
    }

    /* JADX WARN: Multi-variable type inference failed */
    public final long d(InterfaceC2741k interfaceC2741k, boolean z6) throws EOFException {
        C2740j c2740j;
        InterfaceC2741k c2740j2;
        if (z6) {
            c2740j2 = new C2740j();
            c2740j = c2740j2;
        } else {
            c2740j = 0;
            c2740j2 = interfaceC2741k;
        }
        List list = this.f22056c;
        int size = list.size();
        long j4 = 0;
        int i6 = 0;
        while (true) {
            C2743m c2743m = this.f22055b;
            byte[] bArr = f22054k;
            byte[] bArr2 = f22053j;
            if (i6 >= size) {
                o.e(c2740j2);
                c2740j2.u(bArr);
                c2740j2.j(c2743m);
                c2740j2.u(bArr);
                c2740j2.u(bArr2);
                if (!z6) {
                    return j4;
                }
                o.e(c2740j);
                long j6 = j4 + c2740j.f23100b;
                c2740j.a();
                return j6;
            }
            Part part = (Part) list.get(i6);
            Headers headers = part.f22063a;
            o.e(c2740j2);
            c2740j2.u(bArr);
            c2740j2.j(c2743m);
            c2740j2.u(bArr2);
            int size2 = headers.size();
            for (int i7 = 0; i7 < size2; i7++) {
                c2740j2.n(headers.h(i7)).u(f22052i).n(headers.l(i7)).u(bArr2);
            }
            RequestBody$Companion$toRequestBody$2 requestBody$Companion$toRequestBody$2 = part.f22064b;
            MediaType mediaType = requestBody$Companion$toRequestBody$2.f22137b;
            if (mediaType != null) {
                c2740j2.n("Content-Type: ").n(mediaType.f22046a).u(bArr2);
            }
            int i8 = requestBody$Companion$toRequestBody$2.f22138c;
            long j7 = i8;
            if (j7 != -1) {
                c2740j2.n("Content-Length: ").w(j7).u(bArr2);
            } else if (z6) {
                o.e(c2740j);
                c2740j.a();
                return -1L;
            }
            c2740j2.u(bArr2);
            if (z6) {
                j4 += j7;
            } else {
                c2740j2.l(requestBody$Companion$toRequestBody$2.f22139d, i8);
            }
            c2740j2.u(bArr2);
            i6++;
        }
    }
}
