package okhttp3; import d0.g0.w; import d0.z.d.m; import f0.f0.c; import g0.e; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.UUID; import kotlin.TypeCastException; import kotlin.jvm.internal.DefaultConstructorMarker; import okhttp3.MediaType; import okio.BufferedSink; import okio.ByteString; /* compiled from: MultipartBody.kt */ public final class MultipartBody extends RequestBody { public static final MediaType a = MediaType.a.a("multipart/mixed"); public static final MediaType b = MediaType.a.a("multipart/form-data"); /* renamed from: c reason: collision with root package name */ public static final byte[] f2870c = {(byte) 58, (byte) 32}; public static final byte[] d = {(byte) 13, (byte) 10}; public static final byte[] e; public static final b f = new b(null); public final MediaType g; public long h = -1; public final ByteString i; public final MediaType j; public final List k; /* compiled from: MultipartBody.kt */ public static final class Part { public final Headers a; public final RequestBody b; public Part(Headers headers, RequestBody requestBody, DefaultConstructorMarker defaultConstructorMarker) { this.a = headers; this.b = requestBody; } public static final Part a(Headers headers, RequestBody requestBody) { m.checkParameterIsNotNull(requestBody, "body"); boolean z2 = false; if (headers.c("Content-Type") == null) { if (headers.c("Content-Length") == null) { z2 = true; } if (z2) { return new Part(headers, requestBody, null); } throw new IllegalArgumentException("Unexpected header: Content-Length".toString()); } throw new IllegalArgumentException("Unexpected header: Content-Type".toString()); } public static final Part b(String str, String str2, RequestBody requestBody) { m.checkParameterIsNotNull(str, "name"); m.checkParameterIsNotNull(requestBody, "body"); StringBuilder sb = new StringBuilder(); sb.append("form-data; name="); b bVar = MultipartBody.f; bVar.a(sb, str); if (str2 != null) { sb.append("; filename="); bVar.a(sb, str2); } String sb2 = sb.toString(); m.checkExpressionValueIsNotNull(sb2, "StringBuilder().apply(builderAction).toString()"); ArrayList arrayList = new ArrayList(20); m.checkParameterIsNotNull("Content-Disposition", "name"); m.checkParameterIsNotNull(sb2, "value"); if (1 != 0) { for (int i = 0; i < 19; i++) { char charAt = "Content-Disposition".charAt(i); if (!('!' <= charAt && '~' >= charAt)) { throw new IllegalArgumentException(c.j("Unexpected char %#04x at %d in header name: %s", Integer.valueOf(charAt), Integer.valueOf(i), "Content-Disposition").toString()); } } m.checkParameterIsNotNull("Content-Disposition", "name"); m.checkParameterIsNotNull(sb2, "value"); arrayList.add("Content-Disposition"); arrayList.add(w.trim(sb2).toString()); Object[] array = arrayList.toArray(new String[0]); if (array != null) { return a(new Headers((String[]) array, null), requestBody); } throw new TypeCastException("null cannot be cast to non-null type kotlin.Array"); } throw new IllegalArgumentException("name is empty".toString()); } } /* compiled from: MultipartBody.kt */ public static final class a { public final ByteString a; public MediaType b = MultipartBody.a; /* renamed from: c reason: collision with root package name */ public final List f2871c = new ArrayList(); public a() { String uuid = UUID.randomUUID().toString(); m.checkExpressionValueIsNotNull(uuid, "UUID.randomUUID().toString()"); m.checkParameterIsNotNull(uuid, "boundary"); this.a = ByteString.j.b(uuid); } public final a a(Part part) { m.checkParameterIsNotNull(part, "part"); this.f2871c.add(part); return this; } public final MultipartBody b() { if (!this.f2871c.isEmpty()) { return new MultipartBody(this.a, this.b, c.z(this.f2871c)); } throw new IllegalStateException("Multipart body must have at least one part.".toString()); } public final a c(MediaType mediaType) { m.checkParameterIsNotNull(mediaType, "type"); if (m.areEqual(mediaType.e, "multipart")) { this.b = mediaType; return this; } throw new IllegalArgumentException(("multipart != " + mediaType).toString()); } } /* compiled from: MultipartBody.kt */ public static final class b { public b(DefaultConstructorMarker defaultConstructorMarker) { } public final void a(StringBuilder sb, String str) { m.checkParameterIsNotNull(sb, "$this$appendQuotedString"); m.checkParameterIsNotNull(str, "key"); 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("%22"); } else { sb.append(charAt); } } sb.append('\"'); } } static { MediaType.a aVar = MediaType.f2869c; MediaType.a.a("multipart/alternative"); MediaType.a.a("multipart/digest"); MediaType.a.a("multipart/parallel"); byte b2 = (byte) 45; e = new byte[]{b2, b2}; } public MultipartBody(ByteString byteString, MediaType mediaType, List list) { m.checkParameterIsNotNull(byteString, "boundaryByteString"); m.checkParameterIsNotNull(mediaType, "type"); m.checkParameterIsNotNull(list, "parts"); this.i = byteString; this.j = mediaType; this.k = list; MediaType.a aVar = MediaType.f2869c; this.g = MediaType.a.a(mediaType + "; boundary=" + byteString.q()); } /* JADX DEBUG: Multi-variable search result rejected for r0v0, resolved type: g0.e */ /* JADX DEBUG: Multi-variable search result rejected for r0v1, resolved type: g0.e */ /* JADX DEBUG: Multi-variable search result rejected for r0v2, resolved type: g0.e */ /* JADX WARN: Multi-variable type inference failed */ public final long a(BufferedSink bufferedSink, boolean z2) throws IOException { e eVar; if (z2) { bufferedSink = new e(); eVar = bufferedSink; } else { eVar = 0; } int size = this.k.size(); long j = 0; for (int i = 0; i < size; i++) { Part part = this.k.get(i); Headers headers = part.a; RequestBody requestBody = part.b; if (bufferedSink == null) { m.throwNpe(); } bufferedSink.write(e); bufferedSink.d0(this.i); bufferedSink.write(d); if (headers != null) { int size2 = headers.size(); for (int i2 = 0; i2 < size2; i2++) { bufferedSink.J(headers.d(i2)).write(f2870c).J(headers.g(i2)).write(d); } } MediaType contentType = requestBody.contentType(); if (contentType != null) { bufferedSink.J("Content-Type: ").J(contentType.d).write(d); } long contentLength = requestBody.contentLength(); if (contentLength != -1) { bufferedSink.J("Content-Length: ").p0(contentLength).write(d); } else if (z2) { if (eVar == 0) { m.throwNpe(); } eVar.skip(eVar.j); return -1; } byte[] bArr = d; bufferedSink.write(bArr); if (z2) { j += contentLength; } else { requestBody.writeTo(bufferedSink); } bufferedSink.write(bArr); } if (bufferedSink == null) { m.throwNpe(); } byte[] bArr2 = e; bufferedSink.write(bArr2); bufferedSink.d0(this.i); bufferedSink.write(bArr2); bufferedSink.write(d); if (!z2) { return j; } if (eVar == 0) { m.throwNpe(); } long j2 = eVar.j; long j3 = j + j2; eVar.skip(j2); return j3; } @Override // okhttp3.RequestBody public long contentLength() throws IOException { long j = this.h; if (j != -1) { return j; } long a2 = a(null, true); this.h = a2; return a2; } @Override // okhttp3.RequestBody public MediaType contentType() { return this.g; } @Override // okhttp3.RequestBody public void writeTo(BufferedSink bufferedSink) throws IOException { m.checkParameterIsNotNull(bufferedSink, "sink"); a(bufferedSink, false); } }