Android Byte Array Convert From toByteArray(Bundle bundle)

Here you can find the source of toByteArray(Bundle bundle)

Description

to Byte Array

License

Apache License

Declaration

public static byte[] toByteArray(Bundle bundle) 

Method Source Code

//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();/*from  w  w  w  .  ja v a 2  s. co m*/
        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;
            }

        }
    }
}

Related

  1. toByteArray(ContentValues contentValues)
  2. toByteArray(Number... numbers)
  3. toByteArray(Serializable serializable)
  4. toByteArray(Spanned spanned)