what-the-bank/sources/okhttp3/repackaged/ResponseBody.java

151 lines
4.9 KiB
Java
Raw Permalink Normal View History

2024-07-27 18:17:47 +07:00
package okhttp3.repackaged;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.Charset;
import o.C15183gjd;
import o.InterfaceC15142giR;
import o.giM;
import okhttp3.repackaged.internal.Util;
/* loaded from: classes6.dex */
public abstract class ResponseBody implements Closeable {
private Reader aiI;
public abstract long contentLength();
public abstract MediaType contentType();
public abstract InterfaceC15142giR source();
public final InputStream byteStream() {
return source().g();
}
public final byte[] bytes() throws IOException {
long contentLength = contentLength();
if (contentLength > 2147483647L) {
throw new IOException("Cannot buffer entire body for content length: ".concat(String.valueOf(contentLength)));
}
InterfaceC15142giR source = source();
try {
byte[] j = source.j();
Util.closeQuietly(source);
if (contentLength == -1 || contentLength == j.length) {
return j;
}
throw new IOException("Content-Length and stream length disagree");
} catch (Throwable th) {
Util.closeQuietly(source);
throw th;
}
}
public final Reader charStream() {
Reader reader = this.aiI;
if (reader != null) {
return reader;
}
InputStreamReader inputStreamReader = new InputStreamReader(byteStream(), charset());
this.aiI = inputStreamReader;
return inputStreamReader;
}
public final String string() throws IOException {
return new String(bytes(), charset().name());
}
private Charset charset() {
MediaType contentType = contentType();
return contentType != null ? contentType.charset(Util.UTF_8) : Util.UTF_8;
}
@Override // java.io.Closeable, java.lang.AutoCloseable
public void close() {
Util.closeQuietly(source());
}
public static ResponseBody create(MediaType mediaType, String str) {
giM a;
Charset charset = Util.UTF_8;
if (mediaType != null && (charset = mediaType.charset()) == null) {
charset = Util.UTF_8;
StringBuilder sb = new StringBuilder();
sb.append(mediaType);
sb.append("; charset=utf-8");
mediaType = MediaType.parse(sb.toString());
}
giM gim = new giM();
int length = str.length();
if (str == null) {
throw new IllegalArgumentException("string == null");
}
if (length < 0) {
StringBuilder sb2 = new StringBuilder("endIndex < beginIndex: ");
sb2.append(length);
sb2.append(" < 0");
throw new IllegalArgumentException(sb2.toString());
}
if (length > str.length()) {
StringBuilder sb3 = new StringBuilder("endIndex > string.length: ");
sb3.append(length);
sb3.append(" > ");
sb3.append(str.length());
throw new IllegalArgumentException(sb3.toString());
}
if (charset == null) {
throw new IllegalArgumentException("charset == null");
}
if (!charset.equals(C15183gjd.e)) {
byte[] bytes = str.substring(0, length).getBytes(charset);
a = gim.a(bytes, 0, bytes.length);
} else {
a = gim.b(str, 0, str.length());
}
return create(mediaType, a.a, a);
}
public static ResponseBody create(MediaType mediaType, byte[] bArr) {
giM gim = new giM();
if (bArr == null) {
throw new IllegalArgumentException("source == null");
}
return create(mediaType, bArr.length, gim.a(bArr, 0, bArr.length));
}
public static ResponseBody create(MediaType mediaType, long j, InterfaceC15142giR interfaceC15142giR) {
if (interfaceC15142giR == null) {
throw new NullPointerException("source == null");
}
return new ResponseBody(mediaType, j, interfaceC15142giR) { // from class: okhttp3.repackaged.ResponseBody.1
final MediaType aiD;
final InterfaceC15142giR aiJ;
final long val$contentLength;
{
this.aiD = mediaType;
this.val$contentLength = j;
this.aiJ = interfaceC15142giR;
}
@Override // okhttp3.repackaged.ResponseBody
public final InterfaceC15142giR source() {
return this.aiJ;
}
@Override // okhttp3.repackaged.ResponseBody
public final MediaType contentType() {
return this.aiD;
}
@Override // okhttp3.repackaged.ResponseBody
public final long contentLength() {
return this.val$contentLength;
}
};
}
}