package okhttp3.repackaged; import com.google.common.net.HttpHeaders; import java.io.EOFException; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.UUID; import o.C15183gjd; import o.giM; import o.giO; import o.giP; import okhttp3.repackaged.internal.Util; /* loaded from: classes6.dex */ public final class MultipartBody extends RequestBody { private final giP aid; private final MediaType aie; private final MediaType aif; private final List aig; private long contentLength = -1; public static final MediaType MIXED = MediaType.parse("multipart/mixed"); public static final MediaType ALTERNATIVE = MediaType.parse("multipart/alternative"); public static final MediaType DIGEST = MediaType.parse("multipart/digest"); public static final MediaType PARALLEL = MediaType.parse("multipart/parallel"); public static final MediaType FORM = MediaType.parse("multipart/form-data"); private static final byte[] aia = {58, 32}; private static final byte[] aib = {13, 10}; private static final byte[] aic = {45, 45}; MultipartBody(giP gip, MediaType mediaType, List list) { this.aid = gip; this.aie = mediaType; StringBuilder sb = new StringBuilder(); sb.append(mediaType); sb.append("; boundary="); String str = gip.c; if (str == null) { str = new String(gip.d, C15183gjd.e); gip.c = str; } sb.append(str); this.aif = MediaType.parse(sb.toString()); this.aig = Util.immutableList(list); } public final String boundary() { giP gip = this.aid; String str = gip.c; if (str != null) { return str; } String str2 = new String(gip.d, C15183gjd.e); gip.c = str2; return str2; } public final int size() { return this.aig.size(); } public final Part part(int i) { return this.aig.get(i); } @Override // okhttp3.repackaged.RequestBody public final long contentLength() throws IOException { long j = this.contentLength; if (j != -1) { return j; } long a = a(null, true); this.contentLength = a; return a; } @Override // okhttp3.repackaged.RequestBody public final void writeTo(giO gio) throws IOException { a(gio, false); } private long a(giO gio, boolean z) throws IOException { giO gio2; giM gim; if (z) { gim = new giM(); gio2 = gim; } else { gio2 = gio; gim = null; } int size = this.aig.size(); long j = 0; for (int i = 0; i < size; i++) { Part part = this.aig.get(i); Headers headers = part.aii; RequestBody requestBody = part.aij; gio2.a(aic); gio2.e(this.aid); gio2.a(aib); if (headers != null) { int size2 = headers.size(); for (int i2 = 0; i2 < size2; i2++) { gio2.c(headers.name(i2)).a(aia).c(headers.value(i2)).a(aib); } } MediaType contentType = requestBody.contentType(); if (contentType != null) { gio2.c("Content-Type: ").c(contentType.toString()).a(aib); } long contentLength = requestBody.contentLength(); if (contentLength != -1) { gio2.c("Content-Length: ").i(contentLength).a(aib); } else if (z) { try { gim.f(gim.a); return -1L; } catch (EOFException e) { throw new AssertionError(e); } } byte[] bArr = aib; gio2.a(bArr); if (z) { j += contentLength; } else { requestBody.writeTo(gio2); } gio2.a(bArr); } byte[] bArr2 = aic; gio2.a(bArr2); gio2.e(this.aid); gio2.a(bArr2); gio2.a(aib); if (z) { j += gim.a; try { gim.f(gim.a); } catch (EOFException e2) { throw new AssertionError(e2); } } return j; } static StringBuilder appendQuotedString(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("%22"); } else { sb.append(charAt); } } sb.append('\"'); return sb; } /* loaded from: classes6.dex */ public static final class Part { private final Headers aii; private final RequestBody aij; public static Part create(RequestBody requestBody) { return create(null, requestBody); } public static Part create(Headers headers, RequestBody requestBody) { if (requestBody == null) { throw new NullPointerException("body == null"); } if (headers != null && headers.get(HttpHeaders.CONTENT_TYPE) != null) { throw new IllegalArgumentException("Unexpected header: Content-Type"); } if (headers != null && headers.get(HttpHeaders.CONTENT_LENGTH) != null) { throw new IllegalArgumentException("Unexpected header: Content-Length"); } return new Part(headers, requestBody); } public static Part createFormData(String str, String str2) { return createFormData(str, null, RequestBody.create((MediaType) null, str2)); } public static Part createFormData(String str, String str2, RequestBody requestBody) { if (str == null) { throw new NullPointerException("name == null"); } StringBuilder sb = new StringBuilder("form-data; name="); MultipartBody.appendQuotedString(sb, str); if (str2 != null) { sb.append("; filename="); MultipartBody.appendQuotedString(sb, str2); } return create(Headers.of(HttpHeaders.CONTENT_DISPOSITION, sb.toString()), requestBody); } private Part(Headers headers, RequestBody requestBody) { this.aii = headers; this.aij = requestBody; } } /* loaded from: classes6.dex */ public static final class Builder { private final giP aid; private final List aig; private MediaType aih; public Builder() { this(UUID.randomUUID().toString()); } public Builder(String str) { this.aih = MultipartBody.MIXED; this.aig = new ArrayList(); this.aid = giP.b(str); } public final Builder setType(MediaType mediaType) { if (mediaType == null) { throw new NullPointerException("type == null"); } if (!mediaType.type().equals("multipart")) { throw new IllegalArgumentException("multipart != ".concat(String.valueOf(mediaType))); } this.aih = mediaType; return this; } public final Builder addPart(RequestBody requestBody) { return addPart(Part.create(requestBody)); } public final Builder addPart(Headers headers, RequestBody requestBody) { return addPart(Part.create(headers, requestBody)); } public final Builder addFormDataPart(String str, String str2) { return addPart(Part.createFormData(str, str2)); } public final Builder addFormDataPart(String str, String str2, RequestBody requestBody) { return addPart(Part.createFormData(str, str2, requestBody)); } public final Builder addPart(Part part) { if (part == null) { throw new NullPointerException("part == null"); } this.aig.add(part); return this; } public final MultipartBody build() { if (this.aig.isEmpty()) { throw new IllegalStateException("Multipart body must have at least one part."); } return new MultipartBody(this.aid, this.aih, this.aig); } } public final MediaType type() { return this.aie; } public final List parts() { return this.aig; } @Override // okhttp3.repackaged.RequestBody public final MediaType contentType() { return this.aif; } }