Skip to content

Commit

Permalink
Merge pull request #4 from armills/int-arrays
Browse files Browse the repository at this point in the history
Add parcel support for integer arrays
  • Loading branch information
mar-v-in authored Aug 27, 2018
2 parents b98ac1d + d4d3ef1 commit 7249246
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,16 @@ public static byte[] readByteArray(Parcel parcel, int position) {
return arr;
}

public static int[] readIntArray(Parcel parcel, int position) {
int length = readStart(parcel, position);
int start = parcel.dataPosition();
if (length == 0)
return null;
int[] arr = parcel.createIntArray();
parcel.setDataPosition(start + length);
return arr;
}

public static Bundle readBundle(Parcel parcel, int position, ClassLoader classLoader) {
int length = readStart(parcel, position);
int start = parcel.dataPosition();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@ private static void writeField(SafeParcelable object, Parcel parcel, Field field
case ByteArray:
SafeParcelWriter.write(parcel, num, (byte[]) field.get(object), mayNull);
break;
case IntArray:
SafeParcelWriter.write(parcel, num, (int[]) field.get(object), mayNull);
break;
case Integer:
SafeParcelWriter.write(parcel, num, (Integer) field.get(object));
break;
Expand Down Expand Up @@ -279,6 +282,9 @@ private static void readField(SafeParcelable object, Parcel parcel, Field field,
case ByteArray:
field.set(object, SafeParcelReader.readByteArray(parcel, position));
break;
case IntArray:
field.set(object, SafeParcelReader.readIntArray(parcel, position));
break;
case Integer:
field.set(object, SafeParcelReader.readInt(parcel, position));
break;
Expand All @@ -302,8 +308,8 @@ private static void readField(SafeParcelable object, Parcel parcel, Field field,
}

private enum SafeParcelType {
Parcelable, Binder, StringList, List, Bundle, ParcelableArray, StringArray, ByteArray, Interface,
Integer, Long, Boolean, Float, Double, String;
Parcelable, Binder, StringList, List, Bundle, ParcelableArray, StringArray, ByteArray,
Interface, IntArray, Integer, Long, Boolean, Float, Double, String;

public static SafeParcelType fromField(Field field) {
Class clazz = field.getType();
Expand All @@ -313,6 +319,8 @@ public static SafeParcelType fromField(Field field) {
return StringArray;
if (clazz.isArray() && byte.class.isAssignableFrom(clazz.getComponentType()))
return ByteArray;
if (clazz.isArray() && int.class.isAssignableFrom(clazz.getComponentType()))
return IntArray;
if (Bundle.class.isAssignableFrom(clazz))
return Bundle;
if (Parcelable.class.isAssignableFrom(clazz))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,18 @@ public static void write(Parcel parcel, int position, byte[] val, boolean mayNul
}
}

public static void write(Parcel parcel, int position, int[] val, boolean mayNull) {
if (val == null) {
if (mayNull) {
writeStart(parcel, position, 0);
}
} else {
int start = writeStart(parcel, position);
parcel.writeIntArray(val);
writeEnd(parcel, start);
}
}

public static void write(Parcel parcel, int position, String[] val, boolean mayNull) {
if (val == null) {
if (mayNull) {
Expand Down

0 comments on commit 7249246

Please sign in to comment.