jimu-decompiled/sources/okhttp3/internal/Util.java
2025-05-13 19:24:51 +02:00

637 lines
21 KiB
Java

package okhttp3.internal;
import android.support.v4.media.session.PlaybackStateCompat;
import java.io.Closeable;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.IDN;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.Charset;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;
import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import okio.Buffer;
import okio.BufferedSource;
import okio.ByteString;
import okio.Source;
/* loaded from: classes2.dex */
public final class Util {
private static final Pattern VERIFY_AS_IP_ADDRESS;
private static final Method addSuppressedExceptionMethod;
public static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
public static final String[] EMPTY_STRING_ARRAY = new String[0];
public static final ResponseBody EMPTY_RESPONSE = ResponseBody.create((MediaType) null, EMPTY_BYTE_ARRAY);
public static final RequestBody EMPTY_REQUEST = RequestBody.create((MediaType) null, EMPTY_BYTE_ARRAY);
private static final ByteString UTF_8_BOM = ByteString.decodeHex("efbbbf");
private static final ByteString UTF_16_BE_BOM = ByteString.decodeHex("feff");
private static final ByteString UTF_16_LE_BOM = ByteString.decodeHex("fffe");
private static final ByteString UTF_32_BE_BOM = ByteString.decodeHex("0000ffff");
private static final ByteString UTF_32_LE_BOM = ByteString.decodeHex("ffff0000");
public static final Charset UTF_8 = Charset.forName("UTF-8");
public static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1");
private static final Charset UTF_16_BE = Charset.forName("UTF-16BE");
private static final Charset UTF_16_LE = Charset.forName("UTF-16LE");
private static final Charset UTF_32_BE = Charset.forName("UTF-32BE");
private static final Charset UTF_32_LE = Charset.forName("UTF-32LE");
public static final TimeZone UTC = TimeZone.getTimeZone("GMT");
public static final Comparator<String> NATURAL_ORDER = new Comparator<String>() { // from class: okhttp3.internal.Util.1
@Override // java.util.Comparator
public int compare(String str, String str2) {
return str.compareTo(str2);
}
};
static {
Method method = null;
try {
method = Throwable.class.getDeclaredMethod("addSuppressed", Throwable.class);
} catch (Exception unused) {
}
addSuppressedExceptionMethod = method;
VERIFY_AS_IP_ADDRESS = Pattern.compile("([0-9a-fA-F]*:[0-9a-fA-F:.]*)|([\\d.]+)");
}
private Util() {
}
public static void addSuppressedIfPossible(Throwable th, Throwable th2) {
Method method = addSuppressedExceptionMethod;
if (method != null) {
try {
method.invoke(th, th2);
} catch (IllegalAccessException | InvocationTargetException unused) {
}
}
}
public static AssertionError assertionError(String str, Exception exc) {
AssertionError assertionError = new AssertionError(str);
try {
assertionError.initCause(exc);
} catch (IllegalStateException unused) {
}
return assertionError;
}
public static Charset bomAwareCharset(BufferedSource bufferedSource, Charset charset) throws IOException {
if (bufferedSource.rangeEquals(0L, UTF_8_BOM)) {
bufferedSource.skip(UTF_8_BOM.size());
return UTF_8;
}
if (bufferedSource.rangeEquals(0L, UTF_16_BE_BOM)) {
bufferedSource.skip(UTF_16_BE_BOM.size());
return UTF_16_BE;
}
if (bufferedSource.rangeEquals(0L, UTF_16_LE_BOM)) {
bufferedSource.skip(UTF_16_LE_BOM.size());
return UTF_16_LE;
}
if (bufferedSource.rangeEquals(0L, UTF_32_BE_BOM)) {
bufferedSource.skip(UTF_32_BE_BOM.size());
return UTF_32_BE;
}
if (!bufferedSource.rangeEquals(0L, UTF_32_LE_BOM)) {
return charset;
}
bufferedSource.skip(UTF_32_LE_BOM.size());
return UTF_32_LE;
}
public static String canonicalizeHost(String str) {
if (!str.contains(":")) {
try {
String lowerCase = IDN.toASCII(str).toLowerCase(Locale.US);
if (lowerCase.isEmpty()) {
return null;
}
if (containsInvalidHostnameAsciiCodes(lowerCase)) {
return null;
}
return lowerCase;
} catch (IllegalArgumentException unused) {
return null;
}
}
InetAddress decodeIpv6 = (str.startsWith("[") && str.endsWith("]")) ? decodeIpv6(str, 1, str.length() - 1) : decodeIpv6(str, 0, str.length());
if (decodeIpv6 == null) {
return null;
}
byte[] address = decodeIpv6.getAddress();
if (address.length == 16) {
return inet6AddressToAscii(address);
}
throw new AssertionError("Invalid IPv6 address: '" + str + "'");
}
public static int checkDuration(String str, long j, TimeUnit timeUnit) {
if (j < 0) {
throw new IllegalArgumentException(str + " < 0");
}
if (timeUnit == null) {
throw new NullPointerException("unit == null");
}
long millis = timeUnit.toMillis(j);
if (millis > 2147483647L) {
throw new IllegalArgumentException(str + " too large.");
}
if (millis != 0 || j <= 0) {
return (int) millis;
}
throw new IllegalArgumentException(str + " too small.");
}
public static void checkOffsetAndCount(long j, long j2, long j3) {
if ((j2 | j3) < 0 || j2 > j || j - j2 < j3) {
throw new ArrayIndexOutOfBoundsException();
}
}
public static void closeQuietly(Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (RuntimeException e) {
throw e;
} catch (Exception unused) {
}
}
}
public static String[] concat(String[] strArr, String str) {
String[] strArr2 = new String[strArr.length + 1];
System.arraycopy(strArr, 0, strArr2, 0, strArr.length);
strArr2[strArr2.length - 1] = str;
return strArr2;
}
private static boolean containsInvalidHostnameAsciiCodes(String str) {
for (int i = 0; i < str.length(); i++) {
char charAt = str.charAt(i);
if (charAt <= 31 || charAt >= 127 || " #%/:?@[\\]".indexOf(charAt) != -1) {
return true;
}
}
return false;
}
public static int decodeHexDigit(char c) {
if (c >= '0' && c <= '9') {
return c - '0';
}
char c2 = 'a';
if (c < 'a' || c > 'f') {
c2 = 'A';
if (c < 'A' || c > 'F') {
return -1;
}
}
return (c - c2) + 10;
}
private static boolean decodeIpv4Suffix(String str, int i, int i2, byte[] bArr, int i3) {
int i4 = i3;
while (i < i2) {
if (i4 == bArr.length) {
return false;
}
if (i4 != i3) {
if (str.charAt(i) != '.') {
return false;
}
i++;
}
int i5 = i;
int i6 = 0;
while (i5 < i2) {
char charAt = str.charAt(i5);
if (charAt < '0' || charAt > '9') {
break;
}
if ((i6 == 0 && i != i5) || (i6 = ((i6 * 10) + charAt) - 48) > 255) {
return false;
}
i5++;
}
if (i5 - i == 0) {
return false;
}
bArr[i4] = (byte) i6;
i4++;
i = i5;
}
return i4 == i3 + 4;
}
/* JADX WARN: Code restructure failed: missing block: B:26:0x007a, code lost:
return null;
*/
/* JADX WARN: Removed duplicated region for block: B:16:0x0050 */
/*
Code decompiled incorrectly, please refer to instructions dump.
To view partially-correct code enable 'Show inconsistent code' option in preferences
*/
private static java.net.InetAddress decodeIpv6(java.lang.String r10, int r11, int r12) {
/*
r0 = 16
byte[] r0 = new byte[r0]
r1 = -1
r2 = 0
r3 = 0
r4 = -1
r5 = -1
L9:
r6 = 0
if (r11 >= r12) goto L7b
int r7 = r0.length
if (r3 != r7) goto L10
return r6
L10:
int r7 = r11 + 2
if (r7 > r12) goto L28
r8 = 2
java.lang.String r9 = "::"
boolean r8 = r10.regionMatches(r11, r9, r2, r8)
if (r8 == 0) goto L28
if (r4 == r1) goto L20
return r6
L20:
int r3 = r3 + 2
r4 = r3
if (r7 != r12) goto L26
goto L7b
L26:
r5 = r7
goto L4c
L28:
if (r3 == 0) goto L4b
r7 = 1
java.lang.String r8 = ":"
boolean r8 = r10.regionMatches(r11, r8, r2, r7)
if (r8 == 0) goto L36
int r11 = r11 + 1
goto L4b
L36:
java.lang.String r8 = "."
boolean r11 = r10.regionMatches(r11, r8, r2, r7)
if (r11 == 0) goto L4a
int r11 = r3 + (-2)
boolean r10 = decodeIpv4Suffix(r10, r5, r12, r0, r11)
if (r10 != 0) goto L47
return r6
L47:
int r3 = r3 + 2
goto L7b
L4a:
return r6
L4b:
r5 = r11
L4c:
r11 = r5
r7 = 0
L4e:
if (r11 >= r12) goto L61
char r8 = r10.charAt(r11)
int r8 = decodeHexDigit(r8)
if (r8 != r1) goto L5b
goto L61
L5b:
int r7 = r7 << 4
int r7 = r7 + r8
int r11 = r11 + 1
goto L4e
L61:
int r8 = r11 - r5
if (r8 == 0) goto L7a
r9 = 4
if (r8 <= r9) goto L69
goto L7a
L69:
int r6 = r3 + 1
int r8 = r7 >>> 8
r8 = r8 & 255(0xff, float:3.57E-43)
byte r8 = (byte) r8
r0[r3] = r8
int r3 = r6 + 1
r7 = r7 & 255(0xff, float:3.57E-43)
byte r7 = (byte) r7
r0[r6] = r7
goto L9
L7a:
return r6
L7b:
int r10 = r0.length
if (r3 == r10) goto L8e
if (r4 != r1) goto L81
return r6
L81:
int r10 = r0.length
int r11 = r3 - r4
int r10 = r10 - r11
java.lang.System.arraycopy(r0, r4, r0, r10, r11)
int r10 = r0.length
int r10 = r10 - r3
int r10 = r10 + r4
java.util.Arrays.fill(r0, r4, r10, r2)
L8e:
java.net.InetAddress r10 = java.net.InetAddress.getByAddress(r0) // Catch: java.net.UnknownHostException -> L93
return r10
L93:
java.lang.AssertionError r10 = new java.lang.AssertionError
r10.<init>()
throw r10
*/
throw new UnsupportedOperationException("Method not decompiled: okhttp3.internal.Util.decodeIpv6(java.lang.String, int, int):java.net.InetAddress");
}
public static int delimiterOffset(String str, int i, int i2, String str2) {
while (i < i2) {
if (str2.indexOf(str.charAt(i)) != -1) {
return i;
}
i++;
}
return i2;
}
public static boolean discard(Source source, int i, TimeUnit timeUnit) {
try {
return skipAll(source, i, timeUnit);
} catch (IOException unused) {
return false;
}
}
public static boolean equal(Object obj, Object obj2) {
return obj == obj2 || (obj != null && obj.equals(obj2));
}
public static String format(String str, Object... objArr) {
return String.format(Locale.US, str, objArr);
}
public static String hostHeader(HttpUrl httpUrl, boolean z) {
String host;
if (httpUrl.host().contains(":")) {
host = "[" + httpUrl.host() + "]";
} else {
host = httpUrl.host();
}
if (!z && httpUrl.port() == HttpUrl.defaultPort(httpUrl.scheme())) {
return host;
}
return host + ":" + httpUrl.port();
}
public static <T> List<T> immutableList(List<T> list) {
return Collections.unmodifiableList(new ArrayList(list));
}
public static <K, V> Map<K, V> immutableMap(Map<K, V> map) {
return map.isEmpty() ? Collections.emptyMap() : Collections.unmodifiableMap(new LinkedHashMap(map));
}
public static int indexOf(Comparator<String> comparator, String[] strArr, String str) {
int length = strArr.length;
for (int i = 0; i < length; i++) {
if (comparator.compare(strArr[i], str) == 0) {
return i;
}
}
return -1;
}
public static int indexOfControlOrNonAscii(String str) {
int length = str.length();
for (int i = 0; i < length; i++) {
char charAt = str.charAt(i);
if (charAt <= 31 || charAt >= 127) {
return i;
}
}
return -1;
}
private static String inet6AddressToAscii(byte[] bArr) {
int i = 0;
int i2 = 0;
int i3 = -1;
int i4 = 0;
while (i2 < bArr.length) {
int i5 = i2;
while (i5 < 16 && bArr[i5] == 0 && bArr[i5 + 1] == 0) {
i5 += 2;
}
int i6 = i5 - i2;
if (i6 > i4 && i6 >= 4) {
i3 = i2;
i4 = i6;
}
i2 = i5 + 2;
}
Buffer buffer = new Buffer();
while (i < bArr.length) {
if (i == i3) {
buffer.writeByte(58);
i += i4;
if (i == 16) {
buffer.writeByte(58);
}
} else {
if (i > 0) {
buffer.writeByte(58);
}
buffer.writeHexadecimalUnsignedLong(((bArr[i] & 255) << 8) | (bArr[i + 1] & 255));
i += 2;
}
}
return buffer.readUtf8();
}
public static String[] intersect(Comparator<? super String> comparator, String[] strArr, String[] strArr2) {
ArrayList arrayList = new ArrayList();
for (String str : strArr) {
int length = strArr2.length;
int i = 0;
while (true) {
if (i >= length) {
break;
}
if (comparator.compare(str, strArr2[i]) == 0) {
arrayList.add(str);
break;
}
i++;
}
}
return (String[]) arrayList.toArray(new String[arrayList.size()]);
}
public static boolean isAndroidGetsocknameError(AssertionError assertionError) {
return (assertionError.getCause() == null || assertionError.getMessage() == null || !assertionError.getMessage().contains("getsockname failed")) ? false : true;
}
public static boolean nonEmptyIntersection(Comparator<String> comparator, String[] strArr, String[] strArr2) {
if (strArr != null && strArr2 != null && strArr.length != 0 && strArr2.length != 0) {
for (String str : strArr) {
for (String str2 : strArr2) {
if (comparator.compare(str, str2) == 0) {
return true;
}
}
}
}
return false;
}
public static X509TrustManager platformTrustManager() {
try {
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init((KeyStore) null);
TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
if (trustManagers.length == 1 && (trustManagers[0] instanceof X509TrustManager)) {
return (X509TrustManager) trustManagers[0];
}
throw new IllegalStateException("Unexpected default trust managers:" + Arrays.toString(trustManagers));
} catch (GeneralSecurityException e) {
throw assertionError("No System TLS", e);
}
}
public static boolean skipAll(Source source, int i, TimeUnit timeUnit) throws IOException {
long nanoTime = System.nanoTime();
long deadlineNanoTime = source.timeout().hasDeadline() ? source.timeout().deadlineNanoTime() - nanoTime : Long.MAX_VALUE;
source.timeout().deadlineNanoTime(Math.min(deadlineNanoTime, timeUnit.toNanos(i)) + nanoTime);
try {
Buffer buffer = new Buffer();
while (source.read(buffer, PlaybackStateCompat.ACTION_PLAY_FROM_URI) != -1) {
buffer.clear();
}
if (deadlineNanoTime == Long.MAX_VALUE) {
source.timeout().clearDeadline();
} else {
source.timeout().deadlineNanoTime(nanoTime + deadlineNanoTime);
}
return true;
} catch (InterruptedIOException unused) {
if (deadlineNanoTime == Long.MAX_VALUE) {
source.timeout().clearDeadline();
} else {
source.timeout().deadlineNanoTime(nanoTime + deadlineNanoTime);
}
return false;
} catch (Throwable th) {
if (deadlineNanoTime == Long.MAX_VALUE) {
source.timeout().clearDeadline();
} else {
source.timeout().deadlineNanoTime(nanoTime + deadlineNanoTime);
}
throw th;
}
}
public static int skipLeadingAsciiWhitespace(String str, int i, int i2) {
while (i < i2) {
char charAt = str.charAt(i);
if (charAt != '\t' && charAt != '\n' && charAt != '\f' && charAt != '\r' && charAt != ' ') {
return i;
}
i++;
}
return i2;
}
public static int skipTrailingAsciiWhitespace(String str, int i, int i2) {
for (int i3 = i2 - 1; i3 >= i; i3--) {
char charAt = str.charAt(i3);
if (charAt != '\t' && charAt != '\n' && charAt != '\f' && charAt != '\r' && charAt != ' ') {
return i3 + 1;
}
}
return i;
}
public static ThreadFactory threadFactory(final String str, final boolean z) {
return new ThreadFactory() { // from class: okhttp3.internal.Util.2
@Override // java.util.concurrent.ThreadFactory
public Thread newThread(Runnable runnable) {
Thread thread = new Thread(runnable, str);
thread.setDaemon(z);
return thread;
}
};
}
public static String trimSubstring(String str, int i, int i2) {
int skipLeadingAsciiWhitespace = skipLeadingAsciiWhitespace(str, i, i2);
return str.substring(skipLeadingAsciiWhitespace, skipTrailingAsciiWhitespace(str, skipLeadingAsciiWhitespace, i2));
}
public static boolean verifyAsIpAddress(String str) {
return VERIFY_AS_IP_ADDRESS.matcher(str).matches();
}
public static int delimiterOffset(String str, int i, int i2, char c) {
while (i < i2) {
if (str.charAt(i) == c) {
return i;
}
i++;
}
return i2;
}
public static <T> List<T> immutableList(T... tArr) {
return Collections.unmodifiableList(Arrays.asList((Object[]) tArr.clone()));
}
public static void closeQuietly(Socket socket) {
if (socket != null) {
try {
socket.close();
} catch (AssertionError e) {
if (!isAndroidGetsocknameError(e)) {
throw e;
}
} catch (RuntimeException e2) {
throw e2;
} catch (Exception unused) {
}
}
}
public static void closeQuietly(ServerSocket serverSocket) {
if (serverSocket != null) {
try {
serverSocket.close();
} catch (RuntimeException e) {
throw e;
} catch (Exception unused) {
}
}
}
}