序列化之Parcelable

序列化之Serializable
上篇文章寫了Serializable,本篇?jiǎng)t介紹Parcelable

Serializable是java的序列化方式,Parcelable是Android中推薦使用的方式,采用共享內(nèi)存的方式實(shí)現(xiàn)用戶空間和內(nèi)核空間的交換,性能很好,但是實(shí)現(xiàn)方式比較復(fù)雜。

Parcelable與Serializable比較

Parcelable Serizlizable
Parcelable Serizlizable
實(shí)現(xiàn)Parcelable接口 實(shí)現(xiàn)Serizlizable接口
android 專用 Java 自帶
內(nèi)存消耗:低 內(nèi)存消耗:一般
內(nèi)存中直接進(jìn)行讀寫 通過(guò)使用IO流的形式將數(shù)據(jù)讀寫入在硬盤上
不支持持久化 支持持久化
速度快 速度一般

速度上,Parcelable 比 Serializable快了10多倍

對(duì)比總結(jié):

  • 在使用內(nèi)存的時(shí)候,Parcelable 類比Serializable性能高,推薦使用Parcelable類。
  • Serializable在序列化的時(shí)候采用了大量的反射,并且會(huì)產(chǎn)生大量的臨時(shí)變量,從而產(chǎn)生過(guò)高的負(fù)載,而Parcelable沒(méi)有這種情況。
  • Parcelable不能使用在要將數(shù)據(jù)存儲(chǔ)在磁盤上的情況,因?yàn)镻arcelable不能很好的保證數(shù)據(jù)的持續(xù)性在外界有變化的情況下。盡管Serializable效率低點(diǎn), 但在這種情況下,還是要用Serializable 。

Parcelable實(shí)現(xiàn)類介紹

該篇文章中,介紹了Parcelable對(duì)象的快速生成,這里就不再贅述,下面Source 類是一個(gè)標(biāo)準(zhǔn)的實(shí)現(xiàn)了Parcelable的類:

public class Source implements Parcelable {

    private String name;
    private String type;

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.name);
        dest.writeString(this.type);
    }

    public Source() {
    }

    protected Source(Parcel in) {
        this.name = in.readString();
        this.type = in.readString();
    }

    public static final Creator<Source> CREATOR = new Creator<Source>() {
        @Override
        public Source createFromParcel(Parcel source) {
            return new Source(source);
        }

        @Override
        public Source[] newArray(int size) {
            return new Source[size];
        }
    };
}

describeContents

實(shí)現(xiàn)Parcelable的類都需要實(shí)現(xiàn)這個(gè)方法,返回值一般都是0,但特殊情況下(包含 file descriptor時(shí))需要返回1。

就是Parcelable中定義的變量。
/**
* 表示 Parcelable 對(duì)象的扁平化表示包括文件描述符。
*/
public static final int CONTENTS_FILE_DESCRIPTOR = 0x0001;

writeToParcel

writeToParcel是正在序列化的方法,內(nèi)部會(huì)通過(guò)Parcel中的writeXXX方法,最終調(diào)用nativeWritexxx方法,將數(shù)據(jù)寫入。

參數(shù)為Parcel的構(gòu)造函數(shù)

通過(guò)Parcel的readxxx的方法,進(jìn)行反序列化,順序要和寫入保持一致

CREATOR

提供方法,創(chuàng)建列化對(duì)象和創(chuàng)建對(duì)象數(shù)組

Parcel

Parcelable使用了Parcel的read和write方法,來(lái)進(jìn)行序列化和反序列化。Parcel調(diào)用jni方法,將數(shù)據(jù)寫入和讀出。

Parce提供了基本數(shù)據(jù)類型、String的寫入和讀出,及其數(shù)組,和java標(biāo)準(zhǔn)集合類等。
writeValue涵蓋了所有支持的類型。

    public final void writeValue(@Nullable Object v) {
        if (v == null) {
            writeInt(VAL_NULL);
        } else if (v instanceof String) {
            writeInt(VAL_STRING);
            writeString((String) v);
        } else if (v instanceof Integer) {
            writeInt(VAL_INTEGER);
            writeInt((Integer) v);
        } else if (v instanceof Map) {
            writeInt(VAL_MAP);
            writeMap((Map) v);
        } else if (v instanceof Bundle) {
            // Must be before Parcelable
            writeInt(VAL_BUNDLE);
            writeBundle((Bundle) v);
        } else if (v instanceof PersistableBundle) {
            writeInt(VAL_PERSISTABLEBUNDLE);
            writePersistableBundle((PersistableBundle) v);
        } else if (v instanceof Parcelable) {
            // IMPOTANT: cases for classes that implement Parcelable must
            // come before the Parcelable case, so that their specific VAL_*
            // types will be written.
            writeInt(VAL_PARCELABLE);
            writeParcelable((Parcelable) v, 0);
        } else if (v instanceof Short) {
            writeInt(VAL_SHORT);
            writeInt(((Short) v).intValue());
        } else if (v instanceof Long) {
            writeInt(VAL_LONG);
            writeLong((Long) v);
        } else if (v instanceof Float) {
            writeInt(VAL_FLOAT);
            writeFloat((Float) v);
        } else if (v instanceof Double) {
            writeInt(VAL_DOUBLE);
            writeDouble((Double) v);
        } else if (v instanceof Boolean) {
            writeInt(VAL_BOOLEAN);
            writeInt((Boolean) v ? 1 : 0);
        } else if (v instanceof CharSequence) {
            // Must be after String
            writeInt(VAL_CHARSEQUENCE);
            writeCharSequence((CharSequence) v);
        } else if (v instanceof List) {
            writeInt(VAL_LIST);
            writeList((List) v);
        } else if (v instanceof SparseArray) {
            writeInt(VAL_SPARSEARRAY);
            writeSparseArray((SparseArray) v);
        } else if (v instanceof boolean[]) {
            writeInt(VAL_BOOLEANARRAY);
            writeBooleanArray((boolean[]) v);
        } else if (v instanceof byte[]) {
            writeInt(VAL_BYTEARRAY);
            writeByteArray((byte[]) v);
        } else if (v instanceof String[]) {
            writeInt(VAL_STRINGARRAY);
            writeStringArray((String[]) v);
        } else if (v instanceof CharSequence[]) {
            // Must be after String[] and before Object[]
            writeInt(VAL_CHARSEQUENCEARRAY);
            writeCharSequenceArray((CharSequence[]) v);
        } else if (v instanceof IBinder) {
            writeInt(VAL_IBINDER);
            writeStrongBinder((IBinder) v);
        } else if (v instanceof Parcelable[]) {
            writeInt(VAL_PARCELABLEARRAY);
            writeParcelableArray((Parcelable[]) v, 0);
        } else if (v instanceof int[]) {
            writeInt(VAL_INTARRAY);
            writeIntArray((int[]) v);
        } else if (v instanceof long[]) {
            writeInt(VAL_LONGARRAY);
            writeLongArray((long[]) v);
        } else if (v instanceof Byte) {
            writeInt(VAL_BYTE);
            writeInt((Byte) v);
        } else if (v instanceof Size) {
            writeInt(VAL_SIZE);
            writeSize((Size) v);
        } else if (v instanceof SizeF) {
            writeInt(VAL_SIZEF);
            writeSizeF((SizeF) v);
        } else if (v instanceof double[]) {
            writeInt(VAL_DOUBLEARRAY);
            writeDoubleArray((double[]) v);
        } else {
            Class<?> clazz = v.getClass();
            if (clazz.isArray() && clazz.getComponentType() == Object.class) {
                // Only pure Object[] are written here, Other arrays of non-primitive types are
                // handled by serialization as this does not record the component type.
                writeInt(VAL_OBJECTARRAY);
                writeArray((Object[]) v);
            } else if (v instanceof Serializable) {
                // Must be last
                writeInt(VAL_SERIALIZABLE);
                writeSerializable((Serializable) v);
            } else {
                throw new RuntimeException("Parcel: unable to marshal value " + v);
            }
        }
    }

Parcel內(nèi)部維持了一個(gè)緩存,因此建議使用Parcel.obtain()來(lái)獲取Parcel

Parce包含大量write、read、native方法,native通過(guò)一個(gè)指針對(duì)數(shù)據(jù)進(jìn)行讀和寫,通過(guò)不同的

要進(jìn)一步分析Parcels的工作原理,涉及到binder機(jī)制

參考:
https://www.cnblogs.com/wytiger/p/12875640.html

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容