Java tutorial
//package com.java2s; //License from project: Apache License import android.content.ContentValues; import android.content.Intent; import android.os.Bundle; import android.os.Parcel; import android.text.Spanned; import android.text.TextUtils; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectOutput; import java.io.ObjectOutputStream; import java.io.Serializable; public class Main { public static byte[] toByteArray(ContentValues contentValues) { Parcel obtain = Parcel.obtain(); contentValues.writeToParcel(obtain, 0); byte[] byteArray = obtain.marshall(); obtain.recycle(); return byteArray; } public static byte[] toByteArray(Bundle bundle) { Parcel obtain = Parcel.obtain(); bundle.writeToParcel(obtain, 0); byte[] byteArray = obtain.marshall(); obtain.recycle(); return byteArray; } public static byte[] toByteArray(Intent intent) { Parcel obtain = Parcel.obtain(); intent.writeToParcel(obtain, 0); byte[] byteArray = obtain.marshall(); obtain.recycle(); return byteArray; } public static byte[] toByteArray(Spanned spanned) { Parcel obtain = Parcel.obtain(); TextUtils.writeToParcel(spanned, obtain, 0); byte[] byteArray = obtain.marshall(); obtain.recycle(); return byteArray; } public static byte[] toByteArray(Serializable serializable) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutput out = null; try { out = new ObjectOutputStream(bos); out.writeObject(serializable); byte[] bytes = bos.toByteArray(); return bytes; } catch (IOException e) { throw new IllegalArgumentException(e); } finally { try { out.close(); bos.close(); } catch (IOException e) { //can be ignored; } } } }