package org.tensorflow.lite; import java.lang.reflect.Array; import java.nio.ByteBuffer; import java.util.Arrays; import o.EnumC15518gsn; /* loaded from: classes6.dex */ public final class Tensor { public int[] b; public long d; private final EnumC15518gsn e; public static native ByteBuffer buffer(long j); public static native long create(long j, int i); public static native void delete(long j); public static native int dtype(long j); public static native int numBytes(long j); public static native void readMultiDimensionalArray(long j, Object obj); public static native int[] shape(long j); public static native void writeDirectBuffer(long j, ByteBuffer byteBuffer); public static native void writeMultiDimensionalArray(long j, Object obj); public static Tensor b(long j, int i) { return new Tensor(create(j, i)); } private static EnumC15518gsn c(Object obj) { if (obj != null) { Class cls = obj.getClass(); while (cls.isArray()) { cls = cls.getComponentType(); } if (Float.TYPE.equals(cls)) { return EnumC15518gsn.FLOAT32; } if (Integer.TYPE.equals(cls)) { return EnumC15518gsn.INT32; } if (Byte.TYPE.equals(cls)) { return EnumC15518gsn.UINT8; } if (Long.TYPE.equals(cls)) { return EnumC15518gsn.INT64; } } StringBuilder sb = new StringBuilder("DataType error: cannot resolve DataType of "); sb.append(obj.getClass().getName()); throw new IllegalArgumentException(sb.toString()); } public static int[] d(Object obj) { int[] iArr = new int[b(obj)]; d(obj, 0, iArr); return iArr; } private static int b(Object obj) { if (obj == null || !obj.getClass().isArray()) { return 0; } if (Array.getLength(obj) == 0) { throw new IllegalArgumentException("Array lengths cannot be 0."); } return b(Array.get(obj, 0)) + 1; } private static void d(Object obj, int i, int[] iArr) { if (iArr == null || i == iArr.length) { return; } int length = Array.getLength(obj); int i2 = iArr[i]; if (i2 == 0) { iArr[i] = length; } else if (i2 != length) { throw new IllegalArgumentException(String.format("Mismatched lengths (%d and %d) in dimension %d", Integer.valueOf(i2), Integer.valueOf(length), Integer.valueOf(i))); } for (int i3 = 0; i3 < length; i3++) { d(Array.get(obj, i3), i + 1, iArr); } } public static boolean a(Object obj) { return obj instanceof ByteBuffer; } private Tensor(long j) { this.d = j; this.e = EnumC15518gsn.e(dtype(j)); this.b = shape(j); } static { TensorFlowLite.a(); } public final void e(Object obj) { if (obj instanceof ByteBuffer) { ByteBuffer byteBuffer = (ByteBuffer) obj; if (byteBuffer.capacity() == numBytes(this.d)) { return; } throw new IllegalArgumentException(String.format("Cannot convert between a TensorFlowLite buffer with %d bytes and a ByteBuffer with %d bytes.", Integer.valueOf(numBytes(this.d)), Integer.valueOf(byteBuffer.capacity()))); } EnumC15518gsn c = c(obj); EnumC15518gsn enumC15518gsn = this.e; if (c != enumC15518gsn) { throw new IllegalArgumentException(String.format("Cannot convert between a TensorFlowLite tensor with type %s and a Java object of type %s (which is compatible with the TensorFlowLite type %s).", enumC15518gsn, obj.getClass().getName(), c)); } int[] iArr = new int[b(obj)]; d(obj, 0, iArr); if (!Arrays.equals(iArr, this.b)) { throw new IllegalArgumentException(String.format("Cannot copy between a TensorFlowLite tensor with shape %s and a Java object with shape %s.", Arrays.toString(this.b), Arrays.toString(iArr))); } } }