262 lines
8.5 KiB
Java
262 lines
8.5 KiB
Java
package io.grpc.okhttp.internal;
|
|
|
|
import java.io.Closeable;
|
|
import java.io.IOException;
|
|
import java.io.InterruptedIOException;
|
|
import java.io.UnsupportedEncodingException;
|
|
import java.lang.reflect.Array;
|
|
import java.net.ServerSocket;
|
|
import java.net.Socket;
|
|
import java.nio.charset.Charset;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.security.MessageDigest;
|
|
import java.security.NoSuchAlgorithmException;
|
|
import java.util.ArrayList;
|
|
import java.util.Arrays;
|
|
import java.util.Collections;
|
|
import java.util.LinkedHashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.concurrent.ThreadFactory;
|
|
import java.util.concurrent.TimeUnit;
|
|
import o.C15111ghZ;
|
|
import o.C15155gij;
|
|
import o.gdP;
|
|
import o.giD;
|
|
import org.bouncycastle.pqc.jcajce.spec.McElieceCCA2KeyGenParameterSpec;
|
|
|
|
/* loaded from: classes6.dex */
|
|
public final class Util {
|
|
public static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
|
|
public static final String[] EMPTY_STRING_ARRAY = new String[0];
|
|
public static final Charset UTF_8 = StandardCharsets.UTF_8;
|
|
|
|
private Util() {
|
|
}
|
|
|
|
public static void checkOffsetAndCount(long j, long j2, long j3) {
|
|
if ((j2 | j3) < 0 || j2 > j || j - j2 < j3) {
|
|
throw new ArrayIndexOutOfBoundsException();
|
|
}
|
|
}
|
|
|
|
public static boolean equal(Object obj, Object obj2) {
|
|
return obj == obj2 || (obj != null && obj.equals(obj2));
|
|
}
|
|
|
|
public static void closeQuietly(Closeable closeable) {
|
|
if (closeable != null) {
|
|
try {
|
|
closeable.close();
|
|
} catch (RuntimeException e) {
|
|
throw e;
|
|
} catch (Exception unused) {
|
|
}
|
|
}
|
|
}
|
|
|
|
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) {
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void closeAll(Closeable closeable, Closeable closeable2) throws IOException {
|
|
try {
|
|
closeable.close();
|
|
th = null;
|
|
} catch (Throwable th) {
|
|
th = th;
|
|
}
|
|
try {
|
|
closeable2.close();
|
|
} catch (Throwable th2) {
|
|
if (th == null) {
|
|
th = th2;
|
|
}
|
|
}
|
|
if (th == null) {
|
|
return;
|
|
}
|
|
if (th instanceof IOException) {
|
|
throw ((IOException) th);
|
|
}
|
|
if (th instanceof RuntimeException) {
|
|
throw ((RuntimeException) th);
|
|
}
|
|
if (!(th instanceof Error)) {
|
|
throw new AssertionError(th);
|
|
}
|
|
throw ((Error) th);
|
|
}
|
|
|
|
public static boolean discard(giD gid, int i, TimeUnit timeUnit) {
|
|
try {
|
|
return skipAll(gid, i, timeUnit);
|
|
} catch (IOException unused) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static boolean skipAll(giD gid, int i, TimeUnit timeUnit) throws IOException {
|
|
long nanoTime = System.nanoTime();
|
|
long deadlineNanoTime = gid.timeout().hasDeadline() ? gid.timeout().deadlineNanoTime() - nanoTime : Long.MAX_VALUE;
|
|
gid.timeout().deadlineNanoTime(Math.min(deadlineNanoTime, timeUnit.toNanos(i)) + nanoTime);
|
|
try {
|
|
C15111ghZ c15111ghZ = new C15111ghZ();
|
|
while (gid.read(c15111ghZ, 2048L) != -1) {
|
|
c15111ghZ.j(c15111ghZ.c);
|
|
}
|
|
if (deadlineNanoTime == Long.MAX_VALUE) {
|
|
gid.timeout().clearDeadline();
|
|
return true;
|
|
}
|
|
gid.timeout().deadlineNanoTime(nanoTime + deadlineNanoTime);
|
|
return true;
|
|
} catch (InterruptedIOException unused) {
|
|
if (deadlineNanoTime == Long.MAX_VALUE) {
|
|
gid.timeout().clearDeadline();
|
|
return false;
|
|
}
|
|
gid.timeout().deadlineNanoTime(nanoTime + deadlineNanoTime);
|
|
return false;
|
|
} catch (Throwable th) {
|
|
if (deadlineNanoTime == Long.MAX_VALUE) {
|
|
gid.timeout().clearDeadline();
|
|
} else {
|
|
gid.timeout().deadlineNanoTime(nanoTime + deadlineNanoTime);
|
|
}
|
|
throw th;
|
|
}
|
|
}
|
|
|
|
public static String md5Hex(String str) {
|
|
try {
|
|
return C15155gij.e(MessageDigest.getInstance("MD5").digest(str.getBytes("UTF-8"))).c();
|
|
} catch (UnsupportedEncodingException e) {
|
|
throw new AssertionError(e);
|
|
} catch (NoSuchAlgorithmException e2) {
|
|
throw new AssertionError(e2);
|
|
}
|
|
}
|
|
|
|
public static String shaBase64(String str) {
|
|
try {
|
|
return C15155gij.e(MessageDigest.getInstance(McElieceCCA2KeyGenParameterSpec.SHA1).digest(str.getBytes("UTF-8"))).e();
|
|
} catch (UnsupportedEncodingException e) {
|
|
throw new AssertionError(e);
|
|
} catch (NoSuchAlgorithmException e2) {
|
|
throw new AssertionError(e2);
|
|
}
|
|
}
|
|
|
|
public static C15155gij sha1(C15155gij c15155gij) {
|
|
try {
|
|
return C15155gij.e(MessageDigest.getInstance(McElieceCCA2KeyGenParameterSpec.SHA1).digest(c15155gij.j()));
|
|
} catch (NoSuchAlgorithmException e) {
|
|
throw new AssertionError(e);
|
|
}
|
|
}
|
|
|
|
public static <T> List<T> immutableList(List<T> list) {
|
|
return Collections.unmodifiableList(new ArrayList(list));
|
|
}
|
|
|
|
public static <T> List<T> immutableList(T[] tArr) {
|
|
return Collections.unmodifiableList(Arrays.asList((Object[]) tArr.clone()));
|
|
}
|
|
|
|
public static <K, V> Map<K, V> immutableMap(Map<K, V> map) {
|
|
return Collections.unmodifiableMap(new LinkedHashMap(map));
|
|
}
|
|
|
|
public static ThreadFactory threadFactory(String str, boolean z) {
|
|
return new ThreadFactory(str, z) { // from class: io.grpc.okhttp.internal.Util.1
|
|
final boolean val$daemon;
|
|
final String val$name;
|
|
|
|
{
|
|
this.val$name = str;
|
|
this.val$daemon = z;
|
|
}
|
|
|
|
@Override // java.util.concurrent.ThreadFactory
|
|
public Thread newThread(Runnable runnable) {
|
|
Thread thread = new Thread(runnable, this.val$name);
|
|
thread.setDaemon(this.val$daemon);
|
|
return thread;
|
|
}
|
|
};
|
|
}
|
|
|
|
/* JADX WARN: Multi-variable type inference failed */
|
|
public static <T> T[] intersect(Class<T> cls, T[] tArr, T[] tArr2) {
|
|
List intersect = intersect(tArr, tArr2);
|
|
return (T[]) intersect.toArray((Object[]) Array.newInstance((Class<?>) cls, intersect.size()));
|
|
}
|
|
|
|
private static <T> List<T> intersect(T[] tArr, T[] tArr2) {
|
|
ArrayList arrayList = new ArrayList();
|
|
for (T t : tArr) {
|
|
int length = tArr2.length;
|
|
int i = 0;
|
|
while (true) {
|
|
if (i < length) {
|
|
T t2 = tArr2[i];
|
|
if (t.equals(t2)) {
|
|
arrayList.add(t2);
|
|
break;
|
|
}
|
|
i++;
|
|
}
|
|
}
|
|
}
|
|
return arrayList;
|
|
}
|
|
|
|
public static String toHumanReadableAscii(String str) {
|
|
int length = str.length();
|
|
int i = 0;
|
|
while (i < length) {
|
|
int codePointAt = str.codePointAt(i);
|
|
if (codePointAt > 31 && codePointAt < 127) {
|
|
i += Character.charCount(codePointAt);
|
|
} else {
|
|
C15111ghZ c15111ghZ = new C15111ghZ();
|
|
c15111ghZ.c(str, 0, i);
|
|
while (i < length) {
|
|
int codePointAt2 = str.codePointAt(i);
|
|
c15111ghZ.i((codePointAt2 <= 31 || codePointAt2 >= 127) ? 63 : codePointAt2);
|
|
i += Character.charCount(codePointAt2);
|
|
}
|
|
return c15111ghZ.b(c15111ghZ.c, gdP.a);
|
|
}
|
|
}
|
|
return str;
|
|
}
|
|
|
|
public static boolean isAndroidGetsocknameError(AssertionError assertionError) {
|
|
return (assertionError.getCause() == null || assertionError.getMessage() == null || !assertionError.getMessage().contains("getsockname failed")) ? false : true;
|
|
}
|
|
}
|