Browse Source

Add support for byte[]

master
mar-v-in 8 years ago
parent
commit
982f1e1285
  1. 10
      safe-parcel/src/main/java/org/microg/safeparcel/SafeParcelReader.java
  2. 19
      safe-parcel/src/main/java/org/microg/safeparcel/SafeParcelUtil.java

10
safe-parcel/src/main/java/org/microg/safeparcel/SafeParcelReader.java

@ -170,6 +170,16 @@ public class SafeParcelReader { @@ -170,6 +170,16 @@ public class SafeParcelReader {
return arr;
}
public static byte[] readByteArray(Parcel parcel, int position) {
int length = readStart(parcel, position);
int start = parcel.dataPosition();
if (length == 0)
return null;
byte[] arr = parcel.createByteArray();
parcel.setDataPosition(start + length);
return arr;
}
public static void skip(Parcel parcel, int position) {
int i = readStart(parcel, position);
parcel.setDataPosition(parcel.dataPosition() + i);

19
safe-parcel/src/main/java/org/microg/safeparcel/SafeParcelUtil.java

@ -7,7 +7,6 @@ import android.util.Log; @@ -7,7 +7,6 @@ import android.util.Log;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@ -27,7 +26,7 @@ public class SafeParcelUtil { @@ -27,7 +26,7 @@ public class SafeParcelUtil {
return t;
} catch (NoSuchMethodException e) {
throw new RuntimeException("createObject() requires a default constructor");
} catch (InvocationTargetException | InstantiationException | IllegalAccessException e) {
} catch (Exception e) {
throw new RuntimeException("Can't construct object", e);
}
}
@ -96,13 +95,11 @@ public class SafeParcelUtil { @@ -96,13 +95,11 @@ public class SafeParcelUtil {
}
}
private static Parcelable.Creator getCreator(Field field)
throws IllegalAccessException {
private static Parcelable.Creator getCreator(Field field) throws IllegalAccessException {
try {
return (Parcelable.Creator) field.getType().getDeclaredField("CREATOR").get(null);
} catch (NoSuchFieldException e) {
throw new RuntimeException(
field.getType() + " is an Parcelable without CREATOR");
throw new RuntimeException(field.getType() + " is an Parcelable without CREATOR");
}
}
@ -141,6 +138,9 @@ public class SafeParcelUtil { @@ -141,6 +138,9 @@ public class SafeParcelUtil {
case StringArray:
SafeParcelWriter.write(parcel, num, (String[]) field.get(object), mayNull);
break;
case ByteArray:
SafeParcelWriter.write(parcel, num, (byte[]) field.get(object), mayNull);
break;
case Integer:
SafeParcelWriter.write(parcel, num, (Integer) field.get(object));
break;
@ -184,6 +184,9 @@ public class SafeParcelUtil { @@ -184,6 +184,9 @@ public class SafeParcelUtil {
case StringArray:
field.set(object, SafeParcelReader.readStringArray(parcel, position));
break;
case ByteArray:
field.set(object, SafeParcelReader.readByteArray(parcel, position));
break;
case Integer:
field.set(object, SafeParcelReader.readInt(parcel, position));
break;
@ -206,7 +209,7 @@ public class SafeParcelUtil { @@ -206,7 +209,7 @@ public class SafeParcelUtil {
}
private enum SafeParcelType {
Parcelable, Binder, List, ParcelableArray, StringArray,
Parcelable, Binder, List, ParcelableArray, StringArray, ByteArray,
Integer, Long, Boolean, Float, Double, String;
public static SafeParcelType fromClass(Class clazz) {
@ -214,6 +217,8 @@ public class SafeParcelUtil { @@ -214,6 +217,8 @@ public class SafeParcelUtil {
return ParcelableArray;
if (clazz.isArray() && String.class.isAssignableFrom(clazz.getComponentType()))
return StringArray;
if (clazz.isArray() && byte.class.isAssignableFrom(clazz.getComponentType()))
return ByteArray;
if (Parcelable.class.isAssignableFrom(clazz))
return Parcelable;
if (IBinder.class.isAssignableFrom(clazz))

Loading…
Cancel
Save