Created
November 21, 2017 13:27
-
-
Save piyush-malaviya/6449d039ea6c7ca9bc0237d3ab51d690 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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