Example usage for android.os Parcel obtain

List of usage examples for android.os Parcel obtain

Introduction

In this page you can find the example usage for android.os Parcel obtain.

Prototype

public static Parcel obtain() 

Source Link

Document

Retrieve a new Parcel object from the pool.

Usage

From source file:Main.java

public static Bundle mutate(Bundle bundle) {

    if (bundle == null) {
        return null;
    }// w  ww .ja va  2 s.  c o m

    final Parcel in = Parcel.obtain();
    in.writeBundle(bundle);
    final byte[] bytes = in.marshall();

    final Parcel out = Parcel.obtain();
    out.unmarshall(bytes, 0, bytes.length);
    out.setDataPosition(0);

    try {
        return out.readBundle();
    } finally {
        in.recycle();
        out.recycle();
    }
}

From source file:Main.java

private static <T> T bytes2Parcelable(final byte[] bytes, final Parcelable.Creator<T> creator) {
    if (bytes == null)
        return null;
    Parcel parcel = Parcel.obtain();
    parcel.unmarshall(bytes, 0, bytes.length);
    parcel.setDataPosition(0);/*from w  w w  .j a  v a2  s  . com*/
    T result = creator.createFromParcel(parcel);
    parcel.recycle();
    return result;
}

From source file:Main.java

public static <T extends Parcelable> T clone(final T original, final Parcelable.Creator<T> creator) {
    final Parcel parcel = Parcel.obtain();
    original.writeToParcel(parcel, 0);//w w w . j  av a  2s.  c om
    parcel.setDataPosition(0);
    final T copy = creator.createFromParcel(parcel);
    parcel.recycle();
    return copy;
}

From source file:Main.java

private static Parcel unmarshall(byte[] bytes) {
    Parcel parcel = Parcel.obtain();
    parcel.unmarshall(bytes, 0, bytes.length);
    parcel.setDataPosition(0);/*from ww  w . j ava 2s  . c o  m*/
    return parcel;
}

From source file:Main.java

public static boolean writeParcelable(Context context, String fileName, Parcelable parcelObject) {
    boolean success = false;
    FileOutputStream fos = null;//w ww.  j a  v  a  2s . co m
    try {
        fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
        Parcel parcel = Parcel.obtain();
        parcel.writeParcelable(parcelObject, Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
        byte[] data = parcel.marshall();
        fos.write(data);

        success = true;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
    }

    return success;
}

From source file:Main.java

public static byte[] toByteArray(ContentValues contentValues) {
    Parcel obtain = Parcel.obtain();
    contentValues.writeToParcel(obtain, 0);
    byte[] byteArray = obtain.marshall();
    obtain.recycle();/*from w  w  w  .j  ava2  s  . c om*/
    return byteArray;
}

From source file:Main.java

public static byte[] marshall(Parcelable parcelable) {
    Parcel parcel = Parcel.obtain();

    parcelable.writeToParcel(parcel, 0);
    byte[] bytes = parcel.marshall();
    parcel.recycle();//  ww  w .  ja v  a2 s  . c o  m

    return bytes;
}

From source file:Main.java

public static Parcel unmarshall(byte[] bytes) {
    Parcel parcel = Parcel.obtain();

    parcel.unmarshall(bytes, 0, bytes.length);
    parcel.setDataPosition(0);/*from   w  ww  .  ja v a  2s .  co m*/

    return parcel;
}

From source file:Main.java

public static byte[] toByteArray(Bundle bundle) {
    Parcel obtain = Parcel.obtain();
    bundle.writeToParcel(obtain, 0);// ww w  .  j ava  2s . c o  m
    byte[] byteArray = obtain.marshall();
    obtain.recycle();
    return byteArray;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static Parcelable readParcelable(Context context, String fileName, ClassLoader classLoader) {
    Parcelable parcelable = null;//from   ww  w.  j ava2 s .  c  om
    FileInputStream fis = null;
    ByteArrayOutputStream bos = null;
    try {
        fis = context.openFileInput(fileName);
        if (fis != null) {
            bos = new ByteArrayOutputStream();
            byte[] b = new byte[4096];
            int bytesRead;
            while ((bytesRead = fis.read(b)) != -1) {
                bos.write(b, 0, bytesRead);
            }

            byte[] data = bos.toByteArray();

            Parcel parcel = Parcel.obtain();
            parcel.unmarshall(data, 0, data.length);
            parcel.setDataPosition(0);
            parcelable = parcel.readParcelable(classLoader);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
        parcelable = null;
    } finally {
        if (fis != null)
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        if (bos != null)
            try {
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
    }

    return parcelable;
}