package com.google.common.primitives; import com.google.common.base.Ascii; import com.google.common.base.Preconditions; import java.lang.reflect.Field; import java.nio.ByteOrder; import java.security.AccessController; import java.security.PrivilegedActionException; import java.security.PrivilegedExceptionAction; import java.util.Arrays; import java.util.Comparator; import sun.misc.Unsafe; /* loaded from: classes2.dex */ public final class UnsignedBytes { public static final byte MAX_POWER_OF_TWO = Byte.MIN_VALUE; public static final byte MAX_VALUE = -1; private static final int UNSIGNED_MASK = 255; private static byte flip(byte b) { return (byte) (b ^ 128); } public static int toInt(byte b) { return b & MAX_VALUE; } private UnsignedBytes() { } public static byte checkedCast(long j) { Preconditions.checkArgument((j >> 8) == 0, "out of range: %s", j); return (byte) j; } public static byte saturatedCast(long j) { if (j > toInt((byte) -1)) { return (byte) -1; } if (j < 0) { return (byte) 0; } return (byte) j; } public static int compare(byte b, byte b2) { return toInt(b) - toInt(b2); } public static byte min(byte... bArr) { Preconditions.checkArgument(bArr.length > 0); int i = toInt(bArr[0]); for (int i2 = 1; i2 < bArr.length; i2++) { int i3 = toInt(bArr[i2]); if (i3 < i) { i = i3; } } return (byte) i; } public static byte max(byte... bArr) { Preconditions.checkArgument(bArr.length > 0); int i = toInt(bArr[0]); for (int i2 = 1; i2 < bArr.length; i2++) { int i3 = toInt(bArr[i2]); if (i3 > i) { i = i3; } } return (byte) i; } public static String toString(byte b) { return toString(b, 10); } public static String toString(byte b, int i) { Preconditions.checkArgument(i >= 2 && i <= 36, "radix (%s) must be between Character.MIN_RADIX and Character.MAX_RADIX", i); return Integer.toString(toInt(b), i); } public static byte parseUnsignedByte(String str) { return parseUnsignedByte(str, 10); } public static byte parseUnsignedByte(String str, int i) { int parseInt = Integer.parseInt((String) Preconditions.checkNotNull(str), i); if ((parseInt >> 8) == 0) { return (byte) parseInt; } StringBuilder sb = new StringBuilder(25); sb.append("out of range: "); sb.append(parseInt); throw new NumberFormatException(sb.toString()); } public static String join(String str, byte... bArr) { Preconditions.checkNotNull(str); if (bArr.length == 0) { return ""; } StringBuilder sb = new StringBuilder(bArr.length * (str.length() + 3)); sb.append(toInt(bArr[0])); for (int i = 1; i < bArr.length; i++) { sb.append(str); sb.append(toString(bArr[i])); } return sb.toString(); } public static Comparator lexicographicalComparator() { return LexicographicalComparatorHolder.BEST_COMPARATOR; } static Comparator lexicographicalComparatorJavaImpl() { return LexicographicalComparatorHolder.PureJavaComparator.INSTANCE; } /* loaded from: classes2.dex */ static class LexicographicalComparatorHolder { static final String UNSAFE_COMPARATOR_NAME = String.valueOf(LexicographicalComparatorHolder.class.getName()).concat("$UnsafeComparator"); static final Comparator BEST_COMPARATOR = getBestComparator(); LexicographicalComparatorHolder() { } /* loaded from: classes2.dex */ enum UnsafeComparator implements Comparator { INSTANCE; static final boolean BIG_ENDIAN = ByteOrder.nativeOrder().equals(ByteOrder.BIG_ENDIAN); static final int BYTE_ARRAY_BASE_OFFSET; static final Unsafe theUnsafe; static { Unsafe unsafe = getUnsafe(); theUnsafe = unsafe; int arrayBaseOffset = unsafe.arrayBaseOffset(byte[].class); BYTE_ARRAY_BASE_OFFSET = arrayBaseOffset; if (!"64".equals(System.getProperty("sun.arch.data.model")) || arrayBaseOffset % 8 != 0 || unsafe.arrayIndexScale(byte[].class) != 1) { throw new Error(); } } private static Unsafe getUnsafe() { try { try { return Unsafe.getUnsafe(); } catch (PrivilegedActionException e) { throw new RuntimeException("Could not initialize intrinsics", e.getCause()); } } catch (SecurityException unused) { return (Unsafe) AccessController.doPrivileged(new PrivilegedExceptionAction() { // from class: com.google.common.primitives.UnsignedBytes.LexicographicalComparatorHolder.UnsafeComparator.1 @Override // java.security.PrivilegedExceptionAction public Unsafe run() throws Exception { for (Field field : Unsafe.class.getDeclaredFields()) { field.setAccessible(true); Object obj = field.get(null); if (Unsafe.class.isInstance(obj)) { return (Unsafe) Unsafe.class.cast(obj); } } throw new NoSuchFieldError("the Unsafe"); } }); } } @Override // java.util.Comparator public final int compare(byte[] bArr, byte[] bArr2) { int min = Math.min(bArr.length, bArr2.length); int i = 0; while (i < (min & (-8))) { Unsafe unsafe = theUnsafe; long j = BYTE_ARRAY_BASE_OFFSET + i; long j2 = unsafe.getLong(bArr, j); long j3 = unsafe.getLong(bArr2, j); if (j2 != j3) { if (BIG_ENDIAN) { return UnsignedLongs.compare(j2, j3); } int numberOfTrailingZeros = Long.numberOfTrailingZeros(j2 ^ j3) & (-8); return ((int) ((j2 >>> numberOfTrailingZeros) & 255)) - ((int) ((j3 >>> numberOfTrailingZeros) & 255)); } i += 8; } while (i < min) { int compare = UnsignedBytes.compare(bArr[i], bArr2[i]); if (compare != 0) { return compare; } i++; } return bArr.length - bArr2.length; } @Override // java.lang.Enum public final String toString() { return "UnsignedBytes.lexicographicalComparator() (sun.misc.Unsafe version)"; } } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes2.dex */ public enum PureJavaComparator implements Comparator { INSTANCE; @Override // java.util.Comparator public final int compare(byte[] bArr, byte[] bArr2) { int min = Math.min(bArr.length, bArr2.length); for (int i = 0; i < min; i++) { int compare = UnsignedBytes.compare(bArr[i], bArr2[i]); if (compare != 0) { return compare; } } return bArr.length - bArr2.length; } @Override // java.lang.Enum public final String toString() { return "UnsignedBytes.lexicographicalComparator() (pure Java version)"; } } static Comparator getBestComparator() { try { return (Comparator) Class.forName(UNSAFE_COMPARATOR_NAME).getEnumConstants()[0]; } catch (Throwable unused) { return UnsignedBytes.lexicographicalComparatorJavaImpl(); } } } public static void sort(byte[] bArr) { Preconditions.checkNotNull(bArr); sort(bArr, 0, bArr.length); } public static void sort(byte[] bArr, int i, int i2) { Preconditions.checkNotNull(bArr); Preconditions.checkPositionIndexes(i, i2, bArr.length); for (int i3 = i; i3 < i2; i3++) { bArr[i3] = flip(bArr[i3]); } Arrays.sort(bArr, i, i2); while (i < i2) { bArr[i] = flip(bArr[i]); i++; } } public static void sortDescending(byte[] bArr) { Preconditions.checkNotNull(bArr); sortDescending(bArr, 0, bArr.length); } public static void sortDescending(byte[] bArr, int i, int i2) { Preconditions.checkNotNull(bArr); Preconditions.checkPositionIndexes(i, i2, bArr.length); for (int i3 = i; i3 < i2; i3++) { bArr[i3] = (byte) (bArr[i3] ^ Ascii.DEL); } Arrays.sort(bArr, i, i2); while (i < i2) { bArr[i] = (byte) (bArr[i] ^ Ascii.DEL); i++; } } }