Android Byte Array Convert From toByteArray(Spanned spanned)

Here you can find the source of toByteArray(Spanned spanned)

Description

to Byte Array

License

Apache License

Declaration

public static byte[] toByteArray(Spanned spanned) 

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 .j ava2s  .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(Bundle bundle)
  2. toByteArray(ContentValues contentValues)
  3. toByteArray(Number... numbers)
  4. toByteArray(Serializable serializable)
  5. toByteArray(T array)
  6. toByteArray(T array, int sizeOfTBits)
  7. toByteArray(byte[] array)
  8. toByteArray(char[] array)