package okhttp3; import com.google.common.net.HttpHeaders; import java.io.IOException; import java.util.ArrayList; import java.util.List; import o.C14953gcr; import o.C14957gcv; import o.C15111ghZ; import o.C15155gij; import o.InterfaceC15148gic; import o.gdP; import okhttp3.Headers; import okhttp3.MediaType; import okhttp3.RequestBody; import okhttp3.internal.Util; /* loaded from: classes.dex */ public final class MultipartBody extends RequestBody { private final C15155gij boundaryByteString; private long contentLength; private final MediaType contentType; private final List parts; private final MediaType type; public static final Companion Companion = new Companion(null); public static final MediaType MIXED = MediaType.Companion.get("multipart/mixed"); public static final MediaType ALTERNATIVE = MediaType.Companion.get("multipart/alternative"); public static final MediaType DIGEST = MediaType.Companion.get("multipart/digest"); public static final MediaType PARALLEL = MediaType.Companion.get("multipart/parallel"); public static final MediaType FORM = MediaType.Companion.get("multipart/form-data"); private static final byte[] COLONSPACE = {58, 32}; private static final byte[] CRLF = {13, 10}; private static final byte[] DASHDASH = {45, 45}; public MultipartBody(C15155gij c15155gij, MediaType mediaType, List list) { C14957gcv.e(c15155gij, ""); C14957gcv.e(mediaType, ""); C14957gcv.e(list, ""); this.boundaryByteString = c15155gij; this.type = mediaType; this.parts = list; MediaType.Companion companion = MediaType.Companion; StringBuilder sb = new StringBuilder(); sb.append(mediaType); sb.append("; boundary="); sb.append(boundary()); this.contentType = companion.get(sb.toString()); this.contentLength = -1L; } public final int size() { return this.parts.size(); } public final Part part(int i) { return this.parts.get(i); } /* renamed from: -deprecated_boundary, reason: not valid java name */ public final String m435deprecated_boundary() { return boundary(); } /* renamed from: -deprecated_size, reason: not valid java name */ public final int m437deprecated_size() { return size(); } @Override // okhttp3.RequestBody public final long contentLength() throws IOException { long j = this.contentLength; if (j != -1) { return j; } long writeOrCountBytes = writeOrCountBytes(null, true); this.contentLength = writeOrCountBytes; return writeOrCountBytes; } @Override // okhttp3.RequestBody public final void writeTo(InterfaceC15148gic interfaceC15148gic) throws IOException { C14957gcv.e(interfaceC15148gic, ""); writeOrCountBytes(interfaceC15148gic, false); } private final long writeOrCountBytes(InterfaceC15148gic interfaceC15148gic, boolean z) throws IOException { C15111ghZ c15111ghZ; C15111ghZ c15111ghZ2; if (z) { c15111ghZ2 = new C15111ghZ(); c15111ghZ = c15111ghZ2; } else { c15111ghZ = interfaceC15148gic; c15111ghZ2 = null; } int size = this.parts.size(); long j = 0; for (int i = 0; i < size; i++) { Part part = this.parts.get(i); Headers headers = part.headers(); RequestBody body = part.body(); C14957gcv.e(c15111ghZ); c15111ghZ.a(DASHDASH); c15111ghZ.b(this.boundaryByteString); c15111ghZ.a(CRLF); if (headers != null) { int size2 = headers.size(); for (int i2 = 0; i2 < size2; i2++) { c15111ghZ.d(headers.name(i2)).a(COLONSPACE).d(headers.value(i2)).a(CRLF); } } MediaType contentType = body.contentType(); if (contentType != null) { c15111ghZ.d("Content-Type: ").d(contentType.toString()).a(CRLF); } long contentLength = body.contentLength(); if (contentLength != -1) { c15111ghZ.d("Content-Length: ").h(contentLength).a(CRLF); } else if (z) { C14957gcv.e(c15111ghZ2); c15111ghZ2.j(c15111ghZ2.c); return -1L; } byte[] bArr = CRLF; c15111ghZ.a(bArr); if (z) { j += contentLength; } else { body.writeTo(c15111ghZ); } c15111ghZ.a(bArr); } C14957gcv.e(c15111ghZ); byte[] bArr2 = DASHDASH; c15111ghZ.a(bArr2); c15111ghZ.b(this.boundaryByteString); c15111ghZ.a(bArr2); c15111ghZ.a(CRLF); if (!z) { return j; } C14957gcv.e(c15111ghZ2); long j2 = j + c15111ghZ2.c; c15111ghZ2.j(c15111ghZ2.c); return j2; } /* loaded from: classes.dex */ public static final class Part { public static final Companion Companion = new Companion(null); private final RequestBody body; private final Headers headers; private Part(Headers headers, RequestBody requestBody) { this.headers = headers; this.body = requestBody; } /* loaded from: classes.dex */ public static final class Companion { private Companion() { } public final Part create(RequestBody requestBody) { C14957gcv.e(requestBody, ""); return create(null, requestBody); } public final Part create(Headers headers, RequestBody requestBody) { C14957gcv.e(requestBody, ""); C14953gcr c14953gcr = null; if ((headers == null ? null : headers.get(HttpHeaders.CONTENT_TYPE)) != null) { throw new IllegalArgumentException("Unexpected header: Content-Type".toString()); } if ((headers == null ? null : headers.get(HttpHeaders.CONTENT_LENGTH)) != null) { throw new IllegalArgumentException("Unexpected header: Content-Length".toString()); } return new Part(headers, requestBody, c14953gcr); } public final Part createFormData(String str, String str2) { C14957gcv.e(str, ""); C14957gcv.e(str2, ""); return createFormData(str, null, RequestBody.Companion.create$default(RequestBody.Companion, str2, (MediaType) null, 1, (Object) null)); } public final Part createFormData(String str, String str2, RequestBody requestBody) { C14957gcv.e(str, ""); C14957gcv.e(requestBody, ""); StringBuilder sb = new StringBuilder(); sb.append("form-data; name="); MultipartBody.Companion.appendQuotedString$okhttp(sb, str); if (str2 != null) { sb.append("; filename="); MultipartBody.Companion.appendQuotedString$okhttp(sb, str2); } String obj = sb.toString(); C14957gcv.c((Object) obj, ""); return create(new Headers.Builder().addUnsafeNonAscii(HttpHeaders.CONTENT_DISPOSITION, obj).build(), requestBody); } public /* synthetic */ Companion(C14953gcr c14953gcr) { this(); } } public final Headers headers() { return this.headers; } public final RequestBody body() { return this.body; } /* renamed from: -deprecated_headers, reason: not valid java name */ public final Headers m440deprecated_headers() { return this.headers; } /* renamed from: -deprecated_body, reason: not valid java name */ public final RequestBody m439deprecated_body() { return this.body; } public static final Part createFormData(String str, String str2, RequestBody requestBody) { return Companion.createFormData(str, str2, requestBody); } public static final Part createFormData(String str, String str2) { return Companion.createFormData(str, str2); } public static final Part create(RequestBody requestBody) { return Companion.create(requestBody); } public static final Part create(Headers headers, RequestBody requestBody) { return Companion.create(headers, requestBody); } public /* synthetic */ Part(Headers headers, RequestBody requestBody, C14953gcr c14953gcr) { this(headers, requestBody); } } /* loaded from: classes.dex */ public static final class Builder { private final C15155gij boundary; private final List parts; private MediaType type; public Builder(String str) { C14957gcv.e(str, ""); C15155gij.LWm lWm = C15155gij.c; C14957gcv.e(str, ""); C14957gcv.e(str, ""); byte[] bytes = str.getBytes(gdP.a); C14957gcv.c(bytes, ""); C15155gij c15155gij = new C15155gij(bytes); c15155gij.d = str; this.boundary = c15155gij; this.type = MultipartBody.MIXED; this.parts = new ArrayList(); } /* JADX WARN: Illegal instructions before constructor call */ /* Code decompiled incorrectly, please refer to instructions dump. To view partially-correct add '--show-bad-code' argument */ public /* synthetic */ Builder(java.lang.String r1, int r2, o.C14953gcr r3) { /* r0 = this; r2 = r2 & 1 if (r2 == 0) goto L11 java.util.UUID r1 = java.util.UUID.randomUUID() java.lang.String r1 = r1.toString() java.lang.String r2 = "" o.C14957gcv.c(r1, r2) L11: r0.(r1) return */ throw new UnsupportedOperationException("Method not decompiled: okhttp3.MultipartBody.Builder.(java.lang.String, int, o.gcr):void"); } public final Builder setType(MediaType mediaType) { C14957gcv.e(mediaType, ""); if (!C14957gcv.b((Object) mediaType.type(), (Object) "multipart")) { throw new IllegalArgumentException(C14957gcv.c("multipart != ", mediaType).toString()); } this.type = mediaType; return this; } public final Builder addPart(RequestBody requestBody) { C14957gcv.e(requestBody, ""); addPart(Part.Companion.create(requestBody)); return this; } public final Builder addPart(Headers headers, RequestBody requestBody) { C14957gcv.e(requestBody, ""); addPart(Part.Companion.create(headers, requestBody)); return this; } public final Builder addFormDataPart(String str, String str2) { C14957gcv.e(str, ""); C14957gcv.e(str2, ""); addPart(Part.Companion.createFormData(str, str2)); return this; } public final Builder addFormDataPart(String str, String str2, RequestBody requestBody) { C14957gcv.e(str, ""); C14957gcv.e(requestBody, ""); addPart(Part.Companion.createFormData(str, str2, requestBody)); return this; } public final Builder addPart(Part part) { C14957gcv.e(part, ""); this.parts.add(part); return this; } public final MultipartBody build() { if (!(!this.parts.isEmpty())) { throw new IllegalStateException("Multipart body must have at least one part.".toString()); } return new MultipartBody(this.boundary, this.type, Util.toImmutableList(this.parts)); } /* JADX WARN: Multi-variable type inference failed */ public Builder() { this(null, 1, 0 == true ? 1 : 0); } } /* loaded from: classes.dex */ public static final class Companion { private Companion() { } public final void appendQuotedString$okhttp(StringBuilder sb, String str) { C14957gcv.e(sb, ""); C14957gcv.e(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("%22"); } else { sb.append(charAt); } } sb.append('\"'); } public /* synthetic */ Companion(C14953gcr c14953gcr) { this(); } } public final String boundary() { C15155gij c15155gij = this.boundaryByteString; String str = c15155gij.d; if (str != null) { return str; } byte[] b = c15155gij.b(); C14957gcv.e(b, ""); String str2 = new String(b, gdP.a); c15155gij.d = str2; return str2; } public final MediaType type() { return this.type; } public final List parts() { return this.parts; } @Override // okhttp3.RequestBody public final MediaType contentType() { return this.contentType; } /* renamed from: -deprecated_type, reason: not valid java name */ public final MediaType m438deprecated_type() { return this.type; } /* renamed from: -deprecated_parts, reason: not valid java name */ public final List m436deprecated_parts() { return this.parts; } }