Skip to content

Instantly share code, notes, and snippets.

@piyush-malaviya
Created November 21, 2017 13:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save piyush-malaviya/6449d039ea6c7ca9bc0237d3ab51d690 to your computer and use it in GitHub Desktop.
Save piyush-malaviya/6449d039ea6c7ca9bc0237d3ab51d690 to your computer and use it in GitHub Desktop.
import android.os.Parcel;
import android.os.Parcelable;
public class ParcelableUtil {
public static byte[] marshall(Parcelable parceable) {
Parcel parcel = Parcel.obtain();
parceable.writeToParcel(parcel, 0);
byte[] bytes = parcel.marshall();
parcel.recycle();
return bytes;
}
public static Parcel unmarshall(byte[] bytes) {
Parcel parcel = Parcel.obtain();
parcel.unmarshall(bytes, 0, bytes.length);
parcel.setDataPosition(0); // This is extremely important!
return parcel;
}
public static <T> T unmarshall(byte[] bytes, Parcelable.Creator<T> creator) {
Parcel parcel = unmarshall(bytes);
T result = creator.createFromParcel(parcel);
parcel.recycle();
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment