mirror of
https://github.com/VibedByKaKi/alexandroid.git
synced 2026-11-28 08:43:22 +01:00
177 lines
6.5 KiB
Java
177 lines
6.5 KiB
Java
package org.apache.http.util;
|
|
|
|
import androidx.collection.SieveCacheKt;
|
|
import io.ktor.http.auth.HttpAuthHeader;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.io.InputStreamReader;
|
|
import java.io.UnsupportedEncodingException;
|
|
import java.nio.charset.Charset;
|
|
import java.nio.charset.UnsupportedCharsetException;
|
|
import org.apache.http.HeaderElement;
|
|
import org.apache.http.HttpEntity;
|
|
import org.apache.http.HttpResponse;
|
|
import org.apache.http.NameValuePair;
|
|
import org.apache.http.ParseException;
|
|
import org.apache.http.entity.ContentType;
|
|
import org.apache.http.protocol.HTTP;
|
|
|
|
/* compiled from: r8-map-id-b9432e0f623b4a4bff4ba50a8a24c4aafad2c1e11092747b9fb5e6a22af5a940 */
|
|
/* loaded from: classes11.dex */
|
|
public final class EntityUtils {
|
|
private static final int DEFAULT_BUFFER_SIZE = 4096;
|
|
|
|
private EntityUtils() {
|
|
}
|
|
|
|
public static void consume(HttpEntity httpEntity) throws IOException {
|
|
InputStream content;
|
|
if (httpEntity == null || !httpEntity.isStreaming() || (content = httpEntity.getContent()) == null) {
|
|
return;
|
|
}
|
|
content.close();
|
|
}
|
|
|
|
public static void consumeQuietly(HttpEntity httpEntity) {
|
|
try {
|
|
consume(httpEntity);
|
|
} catch (IOException unused) {
|
|
}
|
|
}
|
|
|
|
@Deprecated
|
|
public static String getContentCharSet(HttpEntity httpEntity) throws ParseException {
|
|
NameValuePair parameterByName;
|
|
Args.notNull(httpEntity, "Entity");
|
|
if (httpEntity.getContentType() == null) {
|
|
return null;
|
|
}
|
|
HeaderElement[] elements = httpEntity.getContentType().getElements();
|
|
if (elements.length <= 0 || (parameterByName = elements[0].getParameterByName(HttpAuthHeader.Parameters.Charset)) == null) {
|
|
return null;
|
|
}
|
|
return parameterByName.getValue();
|
|
}
|
|
|
|
@Deprecated
|
|
public static String getContentMimeType(HttpEntity httpEntity) throws ParseException {
|
|
Args.notNull(httpEntity, "Entity");
|
|
if (httpEntity.getContentType() == null) {
|
|
return null;
|
|
}
|
|
HeaderElement[] elements = httpEntity.getContentType().getElements();
|
|
if (elements.length > 0) {
|
|
return elements[0].getName();
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public static byte[] toByteArray(HttpEntity httpEntity) throws IOException {
|
|
Args.notNull(httpEntity, "Entity");
|
|
InputStream content = httpEntity.getContent();
|
|
if (content == null) {
|
|
return null;
|
|
}
|
|
try {
|
|
Args.check(httpEntity.getContentLength() <= SieveCacheKt.NodeLinkMask, "HTTP entity too large to be buffered in memory");
|
|
int contentLength = (int) httpEntity.getContentLength();
|
|
if (contentLength < 0) {
|
|
contentLength = 4096;
|
|
}
|
|
ByteArrayBuffer byteArrayBuffer = new ByteArrayBuffer(contentLength);
|
|
byte[] bArr = new byte[4096];
|
|
while (true) {
|
|
int read = content.read(bArr);
|
|
if (read == -1) {
|
|
byte[] byteArray = byteArrayBuffer.toByteArray();
|
|
content.close();
|
|
return byteArray;
|
|
}
|
|
byteArrayBuffer.append(bArr, 0, read);
|
|
}
|
|
} catch (Throwable th) {
|
|
content.close();
|
|
throw th;
|
|
}
|
|
}
|
|
|
|
private static String toString(HttpEntity httpEntity, ContentType contentType) throws IOException {
|
|
InputStream content = httpEntity.getContent();
|
|
Charset charset = null;
|
|
if (content == null) {
|
|
return null;
|
|
}
|
|
try {
|
|
Args.check(httpEntity.getContentLength() <= SieveCacheKt.NodeLinkMask, "HTTP entity too large to be buffered in memory");
|
|
int contentLength = (int) httpEntity.getContentLength();
|
|
if (contentLength < 0) {
|
|
contentLength = 4096;
|
|
}
|
|
if (contentType != null) {
|
|
Charset charset2 = contentType.getCharset();
|
|
if (charset2 == null) {
|
|
ContentType byMimeType = ContentType.getByMimeType(contentType.getMimeType());
|
|
if (byMimeType != null) {
|
|
charset = byMimeType.getCharset();
|
|
}
|
|
} else {
|
|
charset = charset2;
|
|
}
|
|
}
|
|
if (charset == null) {
|
|
charset = HTTP.DEF_CONTENT_CHARSET;
|
|
}
|
|
InputStreamReader inputStreamReader = new InputStreamReader(content, charset);
|
|
CharArrayBuffer charArrayBuffer = new CharArrayBuffer(contentLength);
|
|
char[] cArr = new char[1024];
|
|
while (true) {
|
|
int read = inputStreamReader.read(cArr);
|
|
if (read == -1) {
|
|
String charArrayBuffer2 = charArrayBuffer.toString();
|
|
content.close();
|
|
return charArrayBuffer2;
|
|
}
|
|
charArrayBuffer.append(cArr, 0, read);
|
|
}
|
|
} catch (Throwable th) {
|
|
content.close();
|
|
throw th;
|
|
}
|
|
}
|
|
|
|
public static void updateEntity(HttpResponse httpResponse, HttpEntity httpEntity) throws IOException {
|
|
Args.notNull(httpResponse, "Response");
|
|
consume(httpResponse.getEntity());
|
|
httpResponse.setEntity(httpEntity);
|
|
}
|
|
|
|
public static String toString(HttpEntity httpEntity, Charset charset) throws IOException, ParseException {
|
|
ContentType contentType;
|
|
Args.notNull(httpEntity, "Entity");
|
|
try {
|
|
contentType = ContentType.get(httpEntity);
|
|
} catch (UnsupportedCharsetException e) {
|
|
if (charset == null) {
|
|
throw new UnsupportedEncodingException(e.getMessage());
|
|
}
|
|
contentType = null;
|
|
}
|
|
if (contentType != null) {
|
|
if (contentType.getCharset() == null) {
|
|
contentType = contentType.withCharset(charset);
|
|
}
|
|
} else {
|
|
contentType = ContentType.DEFAULT_TEXT.withCharset(charset);
|
|
}
|
|
return toString(httpEntity, contentType);
|
|
}
|
|
|
|
public static String toString(HttpEntity httpEntity, String str) throws IOException, ParseException {
|
|
return toString(httpEntity, str != null ? Charset.forName(str) : null);
|
|
}
|
|
|
|
public static String toString(HttpEntity httpEntity) throws IOException, ParseException {
|
|
Args.notNull(httpEntity, "Entity");
|
|
return toString(httpEntity, ContentType.get(httpEntity));
|
|
}
|
|
}
|