package com.google.common.primitives; 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 java.util.Objects; import sun.misc.Unsafe; @ElementTypesAreNonnullByDefault /* loaded from: classes3.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 & 255; } 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; } throw new NumberFormatException(new StringBuilder(25).append("out of range: ").append(parseInt).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).append(toString(bArr[i])); } return sb.toString(); } public static Comparator lexicographicalComparator() { return LexicographicalComparatorHolder.BEST_COMPARATOR; } static Comparator lexicographicalComparatorJavaImpl() { return LexicographicalComparatorHolder.PureJavaComparator.INSTANCE; } /* loaded from: classes3.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: classes3.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; @Override // java.lang.Enum public String toString() { return "UnsignedBytes.lexicographicalComparator() (sun.misc.Unsafe version)"; } 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 int compare(byte[] bArr, byte[] bArr2) { int min = Math.min(bArr.length, bArr2.length); int i = min & (-8); int i2 = 0; while (i2 < i) { Unsafe unsafe = theUnsafe; int i3 = BYTE_ARRAY_BASE_OFFSET; long j = i2; long j2 = unsafe.getLong(bArr, i3 + j); long j3 = unsafe.getLong(bArr2, i3 + 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) (255 & (j3 >>> numberOfTrailingZeros))); } i2 += 8; } while (i2 < min) { int compare = UnsignedBytes.compare(bArr[i2], bArr2[i2]); if (compare != 0) { return compare; } i2++; } return bArr.length - bArr2.length; } } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes3.dex */ public enum PureJavaComparator implements Comparator { INSTANCE; @Override // java.lang.Enum public String toString() { return "UnsignedBytes.lexicographicalComparator() (pure Java version)"; } @Override // java.util.Comparator public 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; } } static Comparator getBestComparator() { try { return (Comparator) ((Object[]) Objects.requireNonNull(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] ^ Byte.MAX_VALUE); } Arrays.sort(bArr, i, i2); while (i < i2) { bArr[i] = (byte) (bArr[i] ^ Byte.MAX_VALUE); i++; } } }