260 lines
8.5 KiB
Java
260 lines
8.5 KiB
Java
package okhttp3;
|
|
|
|
import java.io.IOException;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.UUID;
|
|
import okhttp3.internal.Util;
|
|
import okio.Buffer;
|
|
import okio.BufferedSink;
|
|
import okio.ByteString;
|
|
|
|
/* loaded from: classes2.dex */
|
|
public final class MultipartBody extends RequestBody {
|
|
private final ByteString boundary;
|
|
private long contentLength = -1;
|
|
private final MediaType contentType;
|
|
private final MediaType originalType;
|
|
private final List<Part> parts;
|
|
public static final MediaType MIXED = MediaType.get("multipart/mixed");
|
|
public static final MediaType ALTERNATIVE = MediaType.get("multipart/alternative");
|
|
public static final MediaType DIGEST = MediaType.get("multipart/digest");
|
|
public static final MediaType PARALLEL = MediaType.get("multipart/parallel");
|
|
public static final MediaType FORM = MediaType.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 static final class Builder {
|
|
private final ByteString boundary;
|
|
private final List<Part> parts;
|
|
private MediaType type;
|
|
|
|
public Builder() {
|
|
this(UUID.randomUUID().toString());
|
|
}
|
|
|
|
public Builder addFormDataPart(String str, String str2) {
|
|
return addPart(Part.createFormData(str, str2));
|
|
}
|
|
|
|
public Builder addPart(RequestBody requestBody) {
|
|
return addPart(Part.create(requestBody));
|
|
}
|
|
|
|
public MultipartBody build() {
|
|
if (this.parts.isEmpty()) {
|
|
throw new IllegalStateException("Multipart body must have at least one part.");
|
|
}
|
|
return new MultipartBody(this.boundary, this.type, this.parts);
|
|
}
|
|
|
|
public Builder setType(MediaType mediaType) {
|
|
if (mediaType == null) {
|
|
throw new NullPointerException("type == null");
|
|
}
|
|
if (mediaType.type().equals("multipart")) {
|
|
this.type = mediaType;
|
|
return this;
|
|
}
|
|
throw new IllegalArgumentException("multipart != " + mediaType);
|
|
}
|
|
|
|
public Builder(String str) {
|
|
this.type = MultipartBody.MIXED;
|
|
this.parts = new ArrayList();
|
|
this.boundary = ByteString.encodeUtf8(str);
|
|
}
|
|
|
|
public Builder addFormDataPart(String str, String str2, RequestBody requestBody) {
|
|
return addPart(Part.createFormData(str, str2, requestBody));
|
|
}
|
|
|
|
public Builder addPart(Headers headers, RequestBody requestBody) {
|
|
return addPart(Part.create(headers, requestBody));
|
|
}
|
|
|
|
public Builder addPart(Part part) {
|
|
if (part != null) {
|
|
this.parts.add(part);
|
|
return this;
|
|
}
|
|
throw new NullPointerException("part == null");
|
|
}
|
|
}
|
|
|
|
public static final class Part {
|
|
final RequestBody body;
|
|
final Headers headers;
|
|
|
|
private Part(Headers headers, RequestBody requestBody) {
|
|
this.headers = headers;
|
|
this.body = requestBody;
|
|
}
|
|
|
|
public static Part create(RequestBody requestBody) {
|
|
return create(null, requestBody);
|
|
}
|
|
|
|
public static Part createFormData(String str, String str2) {
|
|
return createFormData(str, null, RequestBody.create((MediaType) null, str2));
|
|
}
|
|
|
|
public RequestBody body() {
|
|
return this.body;
|
|
}
|
|
|
|
public Headers headers() {
|
|
return this.headers;
|
|
}
|
|
|
|
public static Part create(Headers headers, RequestBody requestBody) {
|
|
if (requestBody == null) {
|
|
throw new NullPointerException("body == null");
|
|
}
|
|
if (headers != null && headers.get("Content-Type") != null) {
|
|
throw new IllegalArgumentException("Unexpected header: Content-Type");
|
|
}
|
|
if (headers == null || headers.get("Content-Length") == null) {
|
|
return new Part(headers, requestBody);
|
|
}
|
|
throw new IllegalArgumentException("Unexpected header: Content-Length");
|
|
}
|
|
|
|
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("Content-Disposition", sb.toString()), requestBody);
|
|
}
|
|
}
|
|
|
|
MultipartBody(ByteString byteString, MediaType mediaType, List<Part> list) {
|
|
this.boundary = byteString;
|
|
this.originalType = mediaType;
|
|
this.contentType = MediaType.get(mediaType + "; boundary=" + byteString.utf8());
|
|
this.parts = Util.immutableList(list);
|
|
}
|
|
|
|
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(charAt);
|
|
} else {
|
|
sb.append("%22");
|
|
}
|
|
}
|
|
sb.append('\"');
|
|
return sb;
|
|
}
|
|
|
|
/* JADX WARN: Multi-variable type inference failed */
|
|
private long writeOrCountBytes(BufferedSink bufferedSink, boolean z) throws IOException {
|
|
Buffer buffer;
|
|
if (z) {
|
|
bufferedSink = new Buffer();
|
|
buffer = bufferedSink;
|
|
} else {
|
|
buffer = 0;
|
|
}
|
|
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 requestBody = part.body;
|
|
bufferedSink.write(DASHDASH);
|
|
bufferedSink.write(this.boundary);
|
|
bufferedSink.write(CRLF);
|
|
if (headers != null) {
|
|
int size2 = headers.size();
|
|
for (int i2 = 0; i2 < size2; i2++) {
|
|
bufferedSink.writeUtf8(headers.name(i2)).write(COLONSPACE).writeUtf8(headers.value(i2)).write(CRLF);
|
|
}
|
|
}
|
|
MediaType contentType = requestBody.contentType();
|
|
if (contentType != null) {
|
|
bufferedSink.writeUtf8("Content-Type: ").writeUtf8(contentType.toString()).write(CRLF);
|
|
}
|
|
long contentLength = requestBody.contentLength();
|
|
if (contentLength != -1) {
|
|
bufferedSink.writeUtf8("Content-Length: ").writeDecimalLong(contentLength).write(CRLF);
|
|
} else if (z) {
|
|
buffer.clear();
|
|
return -1L;
|
|
}
|
|
bufferedSink.write(CRLF);
|
|
if (z) {
|
|
j += contentLength;
|
|
} else {
|
|
requestBody.writeTo(bufferedSink);
|
|
}
|
|
bufferedSink.write(CRLF);
|
|
}
|
|
bufferedSink.write(DASHDASH);
|
|
bufferedSink.write(this.boundary);
|
|
bufferedSink.write(DASHDASH);
|
|
bufferedSink.write(CRLF);
|
|
if (!z) {
|
|
return j;
|
|
}
|
|
long size3 = j + buffer.size();
|
|
buffer.clear();
|
|
return size3;
|
|
}
|
|
|
|
public String boundary() {
|
|
return this.boundary.utf8();
|
|
}
|
|
|
|
@Override // okhttp3.RequestBody
|
|
public 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 MediaType contentType() {
|
|
return this.contentType;
|
|
}
|
|
|
|
public Part part(int i) {
|
|
return this.parts.get(i);
|
|
}
|
|
|
|
public List<Part> parts() {
|
|
return this.parts;
|
|
}
|
|
|
|
public int size() {
|
|
return this.parts.size();
|
|
}
|
|
|
|
public MediaType type() {
|
|
return this.originalType;
|
|
}
|
|
|
|
@Override // okhttp3.RequestBody
|
|
public void writeTo(BufferedSink bufferedSink) throws IOException {
|
|
writeOrCountBytes(bufferedSink, false);
|
|
}
|
|
}
|