Java Code Examples for android.os.Parcel#appendFrom()

The following examples show how to use android.os.Parcel#appendFrom() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: android_9.0.0_r45   File: UsageEvents.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void writeToParcel(Parcel dest, int flags) {
    Parcel data = Parcel.obtain();
    data.writeInt(mEventCount);
    data.writeInt(mIndex);
    if (mEventCount > 0) {
        data.writeStringArray(mStringPool);

        if (mEventsToWrite != null) {
            // Write out the events
            Parcel p = Parcel.obtain();
            try {
                p.setDataPosition(0);
                for (int i = 0; i < mEventCount; i++) {
                    final Event event = mEventsToWrite.get(i);
                    writeEventToParcel(event, p, flags);
                }

                final int listByteLength = p.dataPosition();

                // Write the total length of the data.
                data.writeInt(listByteLength);

                // Write our current position into the data.
                data.writeInt(0);

                // Write the data.
                data.appendFrom(p, 0, listByteLength);
            } finally {
                p.recycle();
            }

        } else if (mParcel != null) {
            // Write the total length of the data.
            data.writeInt(mParcel.dataSize());

            // Write out current position into the data.
            data.writeInt(mParcel.dataPosition());

            // Write the data.
            data.appendFrom(mParcel, 0, mParcel.dataSize());
        } else {
            throw new IllegalStateException(
                    "Either mParcel or mEventsToWrite must not be null");
        }
    }
    // Data can be too large for a transact. Write the data as a Blob, which will be written to
    // ashmem if too large.
    dest.writeBlob(data.marshall());
}
 
Example 2
Source Project: Phantom   File: IntentUtils.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * 4.x系统或某些定制机Intent中的Serializable数据反序列化时没有使用intent.setExtrasClassLoader(pluginClassLoader)设置的classloader,
 * 而是使用的VMStack.getClosestUserClassLoader,这意味着4.x的系统插件Intent中包含有自定义的Serializable对象只能在插件的类中
 * 才能被反序列化。为了能在宿主类中合并Intent中的数据,在4.x系统上直接合并Intent的序列化对象mParcelledData。
 * Parcel的内存结构为:
 * ------------------------------------------------------------------------------
 * |   dataLen(int)   |   BUNDLE_MAGIC(int)   |    item_count(int)    |   data
 * ------------------------------------------------------------------------------
 *将Parcel2合并到Parcel1的步骤为:
 * 1.将Parcel2从data开始追加到Parcel1后面
 * 2.修改Parcel1的dataLen为合并都的数据长度
 * 3.修改Parcel1的item_count为合并后台的序列化元素个数
 *
 * 该方法是将intent中的数据合并到originIntent,再用合并后的数据填充intent
 */
private static void mergeExtras(Intent intent, Intent originIntent) {
    Bundle originBundle = originIntent.getExtras();
    if (null == originBundle) {
        return;
    }
    //序列化originIntent中的Bundle
    Parcel originParcel = Parcel.obtain();
    originBundle.writeToParcel(originParcel, 0);

    //序列化intent中的bundle
    Parcel intentParcel = Parcel.obtain();
    Bundle intentBundle = intent.getExtras();
    intentBundle.writeToParcel(intentParcel, 0);

    //读取intentParcel的前3个整数,分别为:数据长度,魔术数,保存的数据个数
    intentParcel.setDataPosition(0);
    int intentParcelLen = intentParcel.readInt();
    intentParcel.readInt();
    int intentParcelOffset = intentParcel.dataPosition();
    int start = intentParcel.dataPosition();
    int intentItemCount = intentParcel.readInt();
    int intSize = intentParcel.dataPosition() - start;

    //读取originParcel的前3个整数,分别为:数据长度,魔术数,保存的数据个数
    originParcel.setDataPosition(0);
    int originParcelLen = originParcel.readInt();
    originParcel.readInt();
    int originParcelOffset = originParcel.dataPosition();
    int originItemCount = originParcel.readInt();
    originParcel.setDataPosition(originParcel.dataPosition() - intSize);
    //修改originParcel的数据个数为合并后的数据个数
    originParcel.writeInt(intentItemCount + originItemCount);

    //将intentParcel追加到originParcel
    originParcel.setDataPosition(originParcelOffset + originParcelLen);
    intentParcel.setDataPosition(intentParcelLen + intentParcelOffset);
    originParcel.appendFrom(intentParcel, intentParcelOffset + intSize, intentParcelLen - intSize);
    originParcel.setDataPosition(0);
    //修改originParcel的数据长度为合并后的数据长度
    originParcel.writeInt(originParcelLen + intentParcelLen - intSize);
    originParcel.setDataPosition(0);

    //用合并后的的数据替换intent中的数据
    Bundle newBundle = new Bundle();
    newBundle.readFromParcel(originParcel);
    intent.replaceExtras(newBundle);
    originParcel.recycle();
    intentParcel.recycle();
}
 
Example 3
private static Parcel copyParcel(Parcel data) {
  Parcel clone = Parcel.obtain();
  clone.appendFrom(data, 0, data.dataSize());
  clone.setDataPosition(0);
  return clone;
}