608 lines
21 KiB
Java
608 lines
21 KiB
Java
package androidx.work;
|
|
|
|
import android.annotation.SuppressLint;
|
|
import android.util.Log;
|
|
import androidx.annotation.NonNull;
|
|
import androidx.annotation.Nullable;
|
|
import androidx.annotation.RestrictTo;
|
|
import androidx.annotation.VisibleForTesting;
|
|
import androidx.room.TypeConverter;
|
|
import java.io.ByteArrayInputStream;
|
|
import java.io.ByteArrayOutputStream;
|
|
import java.io.IOException;
|
|
import java.io.ObjectInputStream;
|
|
import java.io.ObjectOutputStream;
|
|
import java.util.Arrays;
|
|
import java.util.Collections;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.Set;
|
|
public final class Data {
|
|
public static final Data EMPTY = new Builder().build();
|
|
@SuppressLint({"MinMaxConstant"})
|
|
public static final int MAX_DATA_BYTES = 10240;
|
|
private static final String TAG = Logger.tagWithPrefix("Data");
|
|
public Map<String, Object> mValues;
|
|
|
|
public static final class Builder {
|
|
private Map<String, Object> mValues = new HashMap();
|
|
|
|
@NonNull
|
|
public Data build() {
|
|
Data data = new Data(this.mValues);
|
|
Data.toByteArrayInternal(data);
|
|
return data;
|
|
}
|
|
|
|
@NonNull
|
|
@RestrictTo({RestrictTo.Scope.LIBRARY_GROUP})
|
|
public Builder put(@NonNull String str, @Nullable Object obj) {
|
|
if (obj == null) {
|
|
this.mValues.put(str, null);
|
|
} else {
|
|
Class<?> cls = obj.getClass();
|
|
if (cls == Boolean.class || cls == Byte.class || cls == Integer.class || cls == Long.class || cls == Float.class || cls == Double.class || cls == String.class || cls == Boolean[].class || cls == Byte[].class || cls == Integer[].class || cls == Long[].class || cls == Float[].class || cls == Double[].class || cls == String[].class) {
|
|
this.mValues.put(str, obj);
|
|
} else if (cls == boolean[].class) {
|
|
this.mValues.put(str, Data.convertPrimitiveBooleanArray((boolean[]) obj));
|
|
} else if (cls == byte[].class) {
|
|
this.mValues.put(str, Data.convertPrimitiveByteArray((byte[]) obj));
|
|
} else if (cls == int[].class) {
|
|
this.mValues.put(str, Data.convertPrimitiveIntArray((int[]) obj));
|
|
} else if (cls == long[].class) {
|
|
this.mValues.put(str, Data.convertPrimitiveLongArray((long[]) obj));
|
|
} else if (cls == float[].class) {
|
|
this.mValues.put(str, Data.convertPrimitiveFloatArray((float[]) obj));
|
|
} else if (cls == double[].class) {
|
|
this.mValues.put(str, Data.convertPrimitiveDoubleArray((double[]) obj));
|
|
} else {
|
|
throw new IllegalArgumentException(String.format("Key %s has invalid type %s", str, cls));
|
|
}
|
|
}
|
|
return this;
|
|
}
|
|
|
|
@NonNull
|
|
public Builder putAll(@NonNull Data data) {
|
|
putAll(data.mValues);
|
|
return this;
|
|
}
|
|
|
|
@NonNull
|
|
public Builder putAll(@NonNull Map<String, Object> map) {
|
|
for (Map.Entry<String, Object> entry : map.entrySet()) {
|
|
put(entry.getKey(), entry.getValue());
|
|
}
|
|
return this;
|
|
}
|
|
|
|
@NonNull
|
|
public Builder putBoolean(@NonNull String str, boolean z2) {
|
|
this.mValues.put(str, Boolean.valueOf(z2));
|
|
return this;
|
|
}
|
|
|
|
@NonNull
|
|
public Builder putBooleanArray(@NonNull String str, @NonNull boolean[] zArr) {
|
|
this.mValues.put(str, Data.convertPrimitiveBooleanArray(zArr));
|
|
return this;
|
|
}
|
|
|
|
@NonNull
|
|
public Builder putByte(@NonNull String str, byte b) {
|
|
this.mValues.put(str, Byte.valueOf(b));
|
|
return this;
|
|
}
|
|
|
|
@NonNull
|
|
public Builder putByteArray(@NonNull String str, @NonNull byte[] bArr) {
|
|
this.mValues.put(str, Data.convertPrimitiveByteArray(bArr));
|
|
return this;
|
|
}
|
|
|
|
@NonNull
|
|
public Builder putDouble(@NonNull String str, double d) {
|
|
this.mValues.put(str, Double.valueOf(d));
|
|
return this;
|
|
}
|
|
|
|
@NonNull
|
|
public Builder putDoubleArray(@NonNull String str, @NonNull double[] dArr) {
|
|
this.mValues.put(str, Data.convertPrimitiveDoubleArray(dArr));
|
|
return this;
|
|
}
|
|
|
|
@NonNull
|
|
public Builder putFloat(@NonNull String str, float f) {
|
|
this.mValues.put(str, Float.valueOf(f));
|
|
return this;
|
|
}
|
|
|
|
@NonNull
|
|
public Builder putFloatArray(@NonNull String str, @NonNull float[] fArr) {
|
|
this.mValues.put(str, Data.convertPrimitiveFloatArray(fArr));
|
|
return this;
|
|
}
|
|
|
|
@NonNull
|
|
public Builder putInt(@NonNull String str, int i) {
|
|
this.mValues.put(str, Integer.valueOf(i));
|
|
return this;
|
|
}
|
|
|
|
@NonNull
|
|
public Builder putIntArray(@NonNull String str, @NonNull int[] iArr) {
|
|
this.mValues.put(str, Data.convertPrimitiveIntArray(iArr));
|
|
return this;
|
|
}
|
|
|
|
@NonNull
|
|
public Builder putLong(@NonNull String str, long j) {
|
|
this.mValues.put(str, Long.valueOf(j));
|
|
return this;
|
|
}
|
|
|
|
@NonNull
|
|
public Builder putLongArray(@NonNull String str, @NonNull long[] jArr) {
|
|
this.mValues.put(str, Data.convertPrimitiveLongArray(jArr));
|
|
return this;
|
|
}
|
|
|
|
@NonNull
|
|
public Builder putString(@NonNull String str, @Nullable String str2) {
|
|
this.mValues.put(str, str2);
|
|
return this;
|
|
}
|
|
|
|
@NonNull
|
|
public Builder putStringArray(@NonNull String str, @NonNull String[] strArr) {
|
|
this.mValues.put(str, strArr);
|
|
return this;
|
|
}
|
|
}
|
|
|
|
public Data() {
|
|
}
|
|
|
|
public Data(@NonNull Data data) {
|
|
this.mValues = new HashMap(data.mValues);
|
|
}
|
|
|
|
public Data(@NonNull Map<String, ?> map) {
|
|
this.mValues = new HashMap(map);
|
|
}
|
|
|
|
@NonNull
|
|
public static Boolean[] convertPrimitiveBooleanArray(@NonNull boolean[] zArr) {
|
|
Boolean[] boolArr = new Boolean[zArr.length];
|
|
for (int i = 0; i < zArr.length; i++) {
|
|
boolArr[i] = Boolean.valueOf(zArr[i]);
|
|
}
|
|
return boolArr;
|
|
}
|
|
|
|
@NonNull
|
|
public static Byte[] convertPrimitiveByteArray(@NonNull byte[] bArr) {
|
|
Byte[] bArr2 = new Byte[bArr.length];
|
|
for (int i = 0; i < bArr.length; i++) {
|
|
bArr2[i] = Byte.valueOf(bArr[i]);
|
|
}
|
|
return bArr2;
|
|
}
|
|
|
|
@NonNull
|
|
public static Double[] convertPrimitiveDoubleArray(@NonNull double[] dArr) {
|
|
Double[] dArr2 = new Double[dArr.length];
|
|
for (int i = 0; i < dArr.length; i++) {
|
|
dArr2[i] = Double.valueOf(dArr[i]);
|
|
}
|
|
return dArr2;
|
|
}
|
|
|
|
@NonNull
|
|
public static Float[] convertPrimitiveFloatArray(@NonNull float[] fArr) {
|
|
Float[] fArr2 = new Float[fArr.length];
|
|
for (int i = 0; i < fArr.length; i++) {
|
|
fArr2[i] = Float.valueOf(fArr[i]);
|
|
}
|
|
return fArr2;
|
|
}
|
|
|
|
@NonNull
|
|
public static Integer[] convertPrimitiveIntArray(@NonNull int[] iArr) {
|
|
Integer[] numArr = new Integer[iArr.length];
|
|
for (int i = 0; i < iArr.length; i++) {
|
|
numArr[i] = Integer.valueOf(iArr[i]);
|
|
}
|
|
return numArr;
|
|
}
|
|
|
|
@NonNull
|
|
public static Long[] convertPrimitiveLongArray(@NonNull long[] jArr) {
|
|
Long[] lArr = new Long[jArr.length];
|
|
for (int i = 0; i < jArr.length; i++) {
|
|
lArr[i] = Long.valueOf(jArr[i]);
|
|
}
|
|
return lArr;
|
|
}
|
|
|
|
/* JADX WARNING: Removed duplicated region for block: B:24:0x004e A[SYNTHETIC, Splitter:B:24:0x004e] */
|
|
/* JADX WARNING: Removed duplicated region for block: B:35:0x006b A[SYNTHETIC, Splitter:B:35:0x006b] */
|
|
@NonNull
|
|
@TypeConverter
|
|
public static Data fromByteArray(@NonNull byte[] bArr) {
|
|
Throwable th;
|
|
ObjectInputStream objectInputStream;
|
|
Throwable e;
|
|
if (bArr.length <= 10240) {
|
|
HashMap hashMap = new HashMap();
|
|
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bArr);
|
|
try {
|
|
objectInputStream = new ObjectInputStream(byteArrayInputStream);
|
|
try {
|
|
for (int readInt = objectInputStream.readInt(); readInt > 0; readInt--) {
|
|
hashMap.put(objectInputStream.readUTF(), objectInputStream.readObject());
|
|
}
|
|
try {
|
|
objectInputStream.close();
|
|
} catch (IOException e2) {
|
|
Log.e(TAG, "Error in Data#fromByteArray: ", e2);
|
|
}
|
|
try {
|
|
byteArrayInputStream.close();
|
|
} catch (IOException e3) {
|
|
Log.e(TAG, "Error in Data#fromByteArray: ", e3);
|
|
}
|
|
} catch (IOException | ClassNotFoundException e4) {
|
|
e = e4;
|
|
try {
|
|
Log.e(TAG, "Error in Data#fromByteArray: ", e);
|
|
if (objectInputStream != null) {
|
|
}
|
|
byteArrayInputStream.close();
|
|
return new Data(hashMap);
|
|
} catch (Throwable th2) {
|
|
th = th2;
|
|
if (objectInputStream != null) {
|
|
}
|
|
try {
|
|
byteArrayInputStream.close();
|
|
} catch (IOException e5) {
|
|
Log.e(TAG, "Error in Data#fromByteArray: ", e5);
|
|
}
|
|
throw th;
|
|
}
|
|
}
|
|
} catch (IOException | ClassNotFoundException e6) {
|
|
objectInputStream = null;
|
|
e = e6;
|
|
Log.e(TAG, "Error in Data#fromByteArray: ", e);
|
|
if (objectInputStream != null) {
|
|
try {
|
|
objectInputStream.close();
|
|
} catch (IOException e7) {
|
|
Log.e(TAG, "Error in Data#fromByteArray: ", e7);
|
|
}
|
|
}
|
|
byteArrayInputStream.close();
|
|
return new Data(hashMap);
|
|
} catch (Throwable th3) {
|
|
objectInputStream = null;
|
|
th = th3;
|
|
if (objectInputStream != null) {
|
|
try {
|
|
objectInputStream.close();
|
|
} catch (IOException e8) {
|
|
Log.e(TAG, "Error in Data#fromByteArray: ", e8);
|
|
}
|
|
}
|
|
byteArrayInputStream.close();
|
|
throw th;
|
|
}
|
|
return new Data(hashMap);
|
|
}
|
|
throw new IllegalStateException("Data cannot occupy more than 10240 bytes when serialized");
|
|
}
|
|
|
|
/* JADX WARNING: Removed duplicated region for block: B:31:0x0078 A[SYNTHETIC, Splitter:B:31:0x0078] */
|
|
/* JADX WARNING: Removed duplicated region for block: B:41:0x008f A[SYNTHETIC, Splitter:B:41:0x008f] */
|
|
@NonNull
|
|
@TypeConverter
|
|
@RestrictTo({RestrictTo.Scope.LIBRARY_GROUP})
|
|
public static byte[] toByteArrayInternal(@NonNull Data data) {
|
|
Throwable th;
|
|
IOException e;
|
|
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
|
ObjectOutputStream objectOutputStream = null;
|
|
try {
|
|
ObjectOutputStream objectOutputStream2 = new ObjectOutputStream(byteArrayOutputStream);
|
|
try {
|
|
objectOutputStream2.writeInt(data.size());
|
|
for (Map.Entry<String, Object> entry : data.mValues.entrySet()) {
|
|
objectOutputStream2.writeUTF(entry.getKey());
|
|
objectOutputStream2.writeObject(entry.getValue());
|
|
}
|
|
try {
|
|
objectOutputStream2.close();
|
|
} catch (IOException e2) {
|
|
Log.e(TAG, "Error in Data#toByteArray: ", e2);
|
|
}
|
|
try {
|
|
byteArrayOutputStream.close();
|
|
} catch (IOException e3) {
|
|
Log.e(TAG, "Error in Data#toByteArray: ", e3);
|
|
}
|
|
if (byteArrayOutputStream.size() <= 10240) {
|
|
return byteArrayOutputStream.toByteArray();
|
|
}
|
|
throw new IllegalStateException("Data cannot occupy more than 10240 bytes when serialized");
|
|
} catch (IOException e4) {
|
|
e = e4;
|
|
objectOutputStream = objectOutputStream2;
|
|
try {
|
|
Log.e(TAG, "Error in Data#toByteArray: ", e);
|
|
byte[] byteArray = byteArrayOutputStream.toByteArray();
|
|
if (objectOutputStream != null) {
|
|
}
|
|
try {
|
|
byteArrayOutputStream.close();
|
|
} catch (IOException e5) {
|
|
Log.e(TAG, "Error in Data#toByteArray: ", e5);
|
|
}
|
|
return byteArray;
|
|
} catch (Throwable th2) {
|
|
th = th2;
|
|
if (objectOutputStream != null) {
|
|
try {
|
|
objectOutputStream.close();
|
|
} catch (IOException e6) {
|
|
Log.e(TAG, "Error in Data#toByteArray: ", e6);
|
|
}
|
|
}
|
|
try {
|
|
byteArrayOutputStream.close();
|
|
} catch (IOException e7) {
|
|
Log.e(TAG, "Error in Data#toByteArray: ", e7);
|
|
}
|
|
throw th;
|
|
}
|
|
} catch (Throwable th3) {
|
|
th = th3;
|
|
objectOutputStream = objectOutputStream2;
|
|
if (objectOutputStream != null) {
|
|
}
|
|
byteArrayOutputStream.close();
|
|
throw th;
|
|
}
|
|
} catch (IOException e8) {
|
|
e = e8;
|
|
Log.e(TAG, "Error in Data#toByteArray: ", e);
|
|
byte[] byteArray = byteArrayOutputStream.toByteArray();
|
|
if (objectOutputStream != null) {
|
|
try {
|
|
objectOutputStream.close();
|
|
} catch (IOException e9) {
|
|
Log.e(TAG, "Error in Data#toByteArray: ", e9);
|
|
}
|
|
}
|
|
byteArrayOutputStream.close();
|
|
return byteArray;
|
|
}
|
|
}
|
|
|
|
public boolean equals(Object obj) {
|
|
boolean z2;
|
|
if (this == obj) {
|
|
return true;
|
|
}
|
|
if (obj == null || Data.class != obj.getClass()) {
|
|
return false;
|
|
}
|
|
Data data = (Data) obj;
|
|
Set<String> keySet = this.mValues.keySet();
|
|
if (!keySet.equals(data.mValues.keySet())) {
|
|
return false;
|
|
}
|
|
for (String str : keySet) {
|
|
Object obj2 = this.mValues.get(str);
|
|
Object obj3 = data.mValues.get(str);
|
|
if (obj2 == null || obj3 == null) {
|
|
if (obj2 == obj3) {
|
|
z2 = true;
|
|
continue;
|
|
} else {
|
|
z2 = false;
|
|
continue;
|
|
}
|
|
} else if (!(obj2 instanceof Object[]) || !(obj3 instanceof Object[])) {
|
|
z2 = obj2.equals(obj3);
|
|
continue;
|
|
} else {
|
|
z2 = Arrays.deepEquals((Object[]) obj2, (Object[]) obj3);
|
|
continue;
|
|
}
|
|
if (!z2) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public boolean getBoolean(@NonNull String str, boolean z2) {
|
|
Object obj = this.mValues.get(str);
|
|
return obj instanceof Boolean ? ((Boolean) obj).booleanValue() : z2;
|
|
}
|
|
|
|
@Nullable
|
|
public boolean[] getBooleanArray(@NonNull String str) {
|
|
Object obj = this.mValues.get(str);
|
|
if (!(obj instanceof Boolean[])) {
|
|
return null;
|
|
}
|
|
Boolean[] boolArr = (Boolean[]) obj;
|
|
boolean[] zArr = new boolean[boolArr.length];
|
|
for (int i = 0; i < boolArr.length; i++) {
|
|
zArr[i] = boolArr[i].booleanValue();
|
|
}
|
|
return zArr;
|
|
}
|
|
|
|
public byte getByte(@NonNull String str, byte b) {
|
|
Object obj = this.mValues.get(str);
|
|
return obj instanceof Byte ? ((Byte) obj).byteValue() : b;
|
|
}
|
|
|
|
@Nullable
|
|
public byte[] getByteArray(@NonNull String str) {
|
|
Object obj = this.mValues.get(str);
|
|
if (!(obj instanceof Byte[])) {
|
|
return null;
|
|
}
|
|
Byte[] bArr = (Byte[]) obj;
|
|
byte[] bArr2 = new byte[bArr.length];
|
|
for (int i = 0; i < bArr.length; i++) {
|
|
bArr2[i] = bArr[i].byteValue();
|
|
}
|
|
return bArr2;
|
|
}
|
|
|
|
public double getDouble(@NonNull String str, double d) {
|
|
Object obj = this.mValues.get(str);
|
|
return obj instanceof Double ? ((Double) obj).doubleValue() : d;
|
|
}
|
|
|
|
@Nullable
|
|
public double[] getDoubleArray(@NonNull String str) {
|
|
Object obj = this.mValues.get(str);
|
|
if (!(obj instanceof Double[])) {
|
|
return null;
|
|
}
|
|
Double[] dArr = (Double[]) obj;
|
|
double[] dArr2 = new double[dArr.length];
|
|
for (int i = 0; i < dArr.length; i++) {
|
|
dArr2[i] = dArr[i].doubleValue();
|
|
}
|
|
return dArr2;
|
|
}
|
|
|
|
public float getFloat(@NonNull String str, float f) {
|
|
Object obj = this.mValues.get(str);
|
|
return obj instanceof Float ? ((Float) obj).floatValue() : f;
|
|
}
|
|
|
|
@Nullable
|
|
public float[] getFloatArray(@NonNull String str) {
|
|
Object obj = this.mValues.get(str);
|
|
if (!(obj instanceof Float[])) {
|
|
return null;
|
|
}
|
|
Float[] fArr = (Float[]) obj;
|
|
float[] fArr2 = new float[fArr.length];
|
|
for (int i = 0; i < fArr.length; i++) {
|
|
fArr2[i] = fArr[i].floatValue();
|
|
}
|
|
return fArr2;
|
|
}
|
|
|
|
public int getInt(@NonNull String str, int i) {
|
|
Object obj = this.mValues.get(str);
|
|
return obj instanceof Integer ? ((Integer) obj).intValue() : i;
|
|
}
|
|
|
|
@Nullable
|
|
public int[] getIntArray(@NonNull String str) {
|
|
Object obj = this.mValues.get(str);
|
|
if (!(obj instanceof Integer[])) {
|
|
return null;
|
|
}
|
|
Integer[] numArr = (Integer[]) obj;
|
|
int[] iArr = new int[numArr.length];
|
|
for (int i = 0; i < numArr.length; i++) {
|
|
iArr[i] = numArr[i].intValue();
|
|
}
|
|
return iArr;
|
|
}
|
|
|
|
@NonNull
|
|
public Map<String, Object> getKeyValueMap() {
|
|
return Collections.unmodifiableMap(this.mValues);
|
|
}
|
|
|
|
public long getLong(@NonNull String str, long j) {
|
|
Object obj = this.mValues.get(str);
|
|
return obj instanceof Long ? ((Long) obj).longValue() : j;
|
|
}
|
|
|
|
@Nullable
|
|
public long[] getLongArray(@NonNull String str) {
|
|
Object obj = this.mValues.get(str);
|
|
if (!(obj instanceof Long[])) {
|
|
return null;
|
|
}
|
|
Long[] lArr = (Long[]) obj;
|
|
long[] jArr = new long[lArr.length];
|
|
for (int i = 0; i < lArr.length; i++) {
|
|
jArr[i] = lArr[i].longValue();
|
|
}
|
|
return jArr;
|
|
}
|
|
|
|
@Nullable
|
|
public String getString(@NonNull String str) {
|
|
Object obj = this.mValues.get(str);
|
|
if (obj instanceof String) {
|
|
return (String) obj;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
@Nullable
|
|
public String[] getStringArray(@NonNull String str) {
|
|
Object obj = this.mValues.get(str);
|
|
if (obj instanceof String[]) {
|
|
return (String[]) obj;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public <T> boolean hasKeyWithValueOfType(@NonNull String str, @NonNull Class<T> cls) {
|
|
Object obj = this.mValues.get(str);
|
|
return obj != null && cls.isAssignableFrom(obj.getClass());
|
|
}
|
|
|
|
public int hashCode() {
|
|
return this.mValues.hashCode() * 31;
|
|
}
|
|
|
|
@RestrictTo({RestrictTo.Scope.LIBRARY_GROUP})
|
|
@VisibleForTesting
|
|
public int size() {
|
|
return this.mValues.size();
|
|
}
|
|
|
|
@NonNull
|
|
public byte[] toByteArray() {
|
|
return toByteArrayInternal(this);
|
|
}
|
|
|
|
@NonNull
|
|
public String toString() {
|
|
StringBuilder sb = new StringBuilder("Data {");
|
|
if (!this.mValues.isEmpty()) {
|
|
for (String str : this.mValues.keySet()) {
|
|
sb.append(str);
|
|
sb.append(" : ");
|
|
Object obj = this.mValues.get(str);
|
|
if (obj instanceof Object[]) {
|
|
sb.append(Arrays.toString((Object[]) obj));
|
|
} else {
|
|
sb.append(obj);
|
|
}
|
|
sb.append(", ");
|
|
}
|
|
}
|
|
sb.append("}");
|
|
return sb.toString();
|
|
}
|
|
}
|