105 lines
3.5 KiB
Java
105 lines
3.5 KiB
Java
package okhttp3;
|
|
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.nio.charset.Charset;
|
|
import okhttp3.internal.Util;
|
|
import okio.BufferedSink;
|
|
import okio.ByteString;
|
|
import okio.Okio;
|
|
import okio.Source;
|
|
|
|
/* loaded from: classes2.dex */
|
|
public abstract class RequestBody {
|
|
public static RequestBody create(MediaType mediaType, String str) {
|
|
Charset charset = Util.UTF_8;
|
|
if (mediaType != null && (charset = mediaType.charset()) == null) {
|
|
charset = Util.UTF_8;
|
|
mediaType = MediaType.parse(mediaType + "; charset=utf-8");
|
|
}
|
|
return create(mediaType, str.getBytes(charset));
|
|
}
|
|
|
|
public long contentLength() throws IOException {
|
|
return -1L;
|
|
}
|
|
|
|
public abstract MediaType contentType();
|
|
|
|
public abstract void writeTo(BufferedSink bufferedSink) throws IOException;
|
|
|
|
public static RequestBody create(final MediaType mediaType, final ByteString byteString) {
|
|
return new RequestBody() { // from class: okhttp3.RequestBody.1
|
|
@Override // okhttp3.RequestBody
|
|
public long contentLength() throws IOException {
|
|
return byteString.size();
|
|
}
|
|
|
|
@Override // okhttp3.RequestBody
|
|
public MediaType contentType() {
|
|
return MediaType.this;
|
|
}
|
|
|
|
@Override // okhttp3.RequestBody
|
|
public void writeTo(BufferedSink bufferedSink) throws IOException {
|
|
bufferedSink.write(byteString);
|
|
}
|
|
};
|
|
}
|
|
|
|
public static RequestBody create(MediaType mediaType, byte[] bArr) {
|
|
return create(mediaType, bArr, 0, bArr.length);
|
|
}
|
|
|
|
public static RequestBody create(final MediaType mediaType, final byte[] bArr, final int i, final int i2) {
|
|
if (bArr != null) {
|
|
Util.checkOffsetAndCount(bArr.length, i, i2);
|
|
return new RequestBody() { // from class: okhttp3.RequestBody.2
|
|
@Override // okhttp3.RequestBody
|
|
public long contentLength() {
|
|
return i2;
|
|
}
|
|
|
|
@Override // okhttp3.RequestBody
|
|
public MediaType contentType() {
|
|
return MediaType.this;
|
|
}
|
|
|
|
@Override // okhttp3.RequestBody
|
|
public void writeTo(BufferedSink bufferedSink) throws IOException {
|
|
bufferedSink.write(bArr, i, i2);
|
|
}
|
|
};
|
|
}
|
|
throw new NullPointerException("content == null");
|
|
}
|
|
|
|
public static RequestBody create(final MediaType mediaType, final File file) {
|
|
if (file != null) {
|
|
return new RequestBody() { // from class: okhttp3.RequestBody.3
|
|
@Override // okhttp3.RequestBody
|
|
public long contentLength() {
|
|
return file.length();
|
|
}
|
|
|
|
@Override // okhttp3.RequestBody
|
|
public MediaType contentType() {
|
|
return MediaType.this;
|
|
}
|
|
|
|
@Override // okhttp3.RequestBody
|
|
public void writeTo(BufferedSink bufferedSink) throws IOException {
|
|
Source source = null;
|
|
try {
|
|
source = Okio.source(file);
|
|
bufferedSink.writeAll(source);
|
|
} finally {
|
|
Util.closeQuietly(source);
|
|
}
|
|
}
|
|
};
|
|
}
|
|
throw new NullPointerException("content == null");
|
|
}
|
|
}
|