Parcel

1.IPC解决方案 而非
序列化机制

  Container for a message (data and object references) that can be sent
through an IBinder. A Parcel can contain both flattened

data that will be unflattened on the other side of the IPC (using the various
methods here for writing specific types, or the general

Parcelable interface), and references to
live IBinder objects that will result in the other side
receiving a proxy IBinder connected with

the original IBinder in the Parcel.

  Parcel is not a general-purpose
serialization mechanism. This class (and the
corresponding Parcelable API for placing arbitrary

objects into a Parcel) is designed as a high-performance IPC
transport. As such, it is not appropriate to place any Parcel data in to

persistent storage: changes in the underlying implementation of
any of the data in the Parcel can render older data unreadable.

2.指针移动


public final native int dataSize();
public final native int dataAvail();
public final native int dataPosition();
public final native int dataCapacity();

public final native void setDataSize(int size);
public final native void setDataPosition(int pos);
public final native void setDataCapacity(int size);

3.读写数据


        Parcel parcel = Parcel.obtain();
parcel.writeInt(1);
parcel.writeInt(2);
parcel.writeInt(3);

//不调用parcel.setDataPosition(i)读不出实际内容
//System.out.println("----------------" + parcel.readInt());

int size = parcel.dataSize();
Log.d("parcel", "--------- " + size);
int i = 0;
while (i <= size ) {
parcel.setDataPosition(i);
int cur = parcel.readInt();
Log.d("parcel", "--------- " + cur);
i+=4;
}

4.marshall &
unmarshall

public final native byte[]
marshall();

  • Returns the raw bytes of the parcel.
  • The data you retrieve here must
    not
     be placed in any kind of persistent storage (on local disk,
    across a network, etc). For that,

you should use standard serialization or another kind of
general serialization mechanism. The Parcel marshalled representation

is highly optimized for local IPC, and as such does not attempt
to maintain compatibility with data created in different

versions of the platform.

public final native void unmarshall(byte[] data, int
offest, int length);

  • Set the bytes in data to be the raw bytes of this
    Parcel.

作用类似于序列化和反序列化。即将当前Parcel的数据序列化为byte数组,或者将byte数组反序列化到当前Parcel中。

注:unmarshall后,如果要读取数据,首先需要将文件指针移动到初始化位置,即setDataPosition(0)。

5.工具


public class ParcelableUtil {

public static byte[] marshall(Parcelable parceable) {
Parcel parcel = Parcel.obtain();
parceable.writeToParcel(parcel, 0);
byte[] bytes = parcel.marshall();
parcel.recycle(); // notice
return bytes;
}

public static Parcel unmarshall(byte[] bytes) {
Parcel parcel = Parcel.obtain();
parcel.unmarshall(bytes, 0, bytes.length);
parcel.setDataPosition(0); // this is extremely important!
return parcel;
}

public static <T> T unmarshall(byte[] bytes, Parcelable.Creator<T> creator) {
Parcel parcel = unmarshall(bytes);
return creator.createFromParcel(parcel);
}
}

3.


With the help of the util class above, you can marshall/unmarshall instances of your class MyClass implements Parcelable like so:

//Unmarshalling (with CREATOR)
byte[] bytes = …
MyClass myclass = ParcelableUtil.unmarshall(bytes, MyClass.CREATOR);

//Unmarshalling (without CREATOR)
byte[] bytes = …
Parcel parcel = ParcelableUtil.unmarshall(bytes);
MyClass myclass = new MyClass(parcel); // or MyClass.CREATOR.createFromParcel(parcel)


//Marshalling
MyClass myclass = …
byte[] bytes = ParcelableUtil.marshall(myclass);

Parcel

时间: 2024-08-03 17:41:03

Parcel的相关文章

Android Binder机制分析(4) Parcel类分析

引言 在上一篇Blog中,在分析服务注册过程时,往data(Parcel对象)变量写入数据时,有这样的调用路径: BpServiceManager::addService()–>Parcel::writeStrongBinder()–>flatten_binder()–>finish_flatten_binder() 由于finish_flatten_binder()方法中涉及到的东西太多,在上一篇博客就没有展开来讲.这篇博客将详细分析数据是如何写入到data中的. 下面是Parcel类

Android中的Parcel机制 实现Bundle传递对象

Android中的Parcel机制    实现了Bundle传递对象    使用Bundle传递对象,首先要将其序列化,但是,在Android中要使用这种传递对象的方式需要用到Android Parcel机制,即,Android实现的轻量级的高效的对象序列化和反序列化机制. JAVA中的Serialize机制,译成串行化.序列化……,其作用是能将数据对象存入字节流当中,在需要时重新生成对象.主要应用是利用外部存储设备保存对象状态,以及通过网络传输对象等.        Android中的新的序列

android 对象传输及parcel机制

在开发中不少要用到Activity直接传输对象,下面我们来看看,其实跟java里面差不多   自定义对象的传递:通过intent传递自定义对象的方法有两个  第一是实现Serialization接口:  第二是实现Parcelable接口: 下面来看个例子: package com.example.bean; import java.io.Serializable; public class Bed implements Serializable { private String name; p

Binder学习笔记(五)—— Parcel是怎么打包数据的?

前文中曾经遇到过Parcel,从命名上知道他负责数据打包.在checkService的请求/响应体系中,Parcel只打包了基本数据类型,如Int32.String16……后面还要用于打包抽象数据类型flat_binder_object,这会稍微复杂一些,因此有必要拿出来单独研究.我们从Parcel::writeInterfaceToken(…)追起,它的层层调用关系如下,这些函数都在frameworks/native/libs/binder/Parcel.cpp文件中,行数和函数名为: 582

探索Android该Parcel机制(上)

一.先从Serialize说起 我们都知道JAVA中的Serialize机制.译成串行化.序列化……,其作用是能将数据对象存入字节流其中,在须要时又一次生成对象.主要应用是利用外部存储设备保存对象状态,以及通过网络传输对象等. 二.Android中的新的序列化机制 在Android系统中,定位为针对内存受限的设备,因此对性能要求更高,另外系统中採用了新的IPC(进程间通信)机制,必定要求使用性能更出色的对象传输方式. 在这种环境下,Parcel被设计出来,其定位就是轻量级的高效的对象序列化和反序

Parcel 和 Parcelable

Parcel 在英文中有两个意思,其一是名词,为包裹,小包的意思: 其二为动词,意为打包,扎包.邮寄快递中的包裹也用的是这个词.Android采用这个词来表示封装消息数据.这个是通过IBinder通信的消息的载体.需要明确的是Parcel用来存放数据的是内存(RAM),而不是永久性介质(Nand等). Parcelable,定义了将数据写入Parcel,和从Parcel中读出的接口.一个实体(用类来表示),如果需要封装到消息中去,就必须实现这一接口,实现了这一接口,该实体就成为“可打包的”了.

ANDROID_MARS学习笔记_S01原始版_018_SERVICE之Parcel

一.代码 1.xml(1)activity_main.xml 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height=&qu

探索Android该Parcel机制上

一.先从Serialize说起 我们都知道JAVA中的Serialize机制.译成串行化.序列化……,其作用是能将数据对象存入字节流其中,在须要时又一次生成对象.主要应用是利用外部存储设备保存对象状态,以及通过网络传输对象等. 二.Android中的新的序列化机制 在Android系统中.定位为针对内存受限的设备,因此对性能要求更高,另外系统中採用了新的IPC(进程间通信)机制,必定要求使用性能更出色的对象传输方式.在这种环境下,Parcel被设计出来,其定位就是轻量级的高效的对象序列化和反序列

探索Android中的Parcel机制(上)

一.先从Serialize说起 我们都知道JAVA中的Serialize机制,译成串行化.序列化……,其作用是能将数据对象存入字节流其中,在须要时又一次生成对象.主要应用是利用外部存储设备保存对象状态,以及通过网络传输对象等. 二.Android中的新的序列化机制 在Android系统中,定位为针对内存受限的设备,因此对性能要求更高,另外系统中採用了新的IPC(进程间通信)机制,必定要求使用性能更出色的对象传输方式.在这种环境下,Parcel被设计出来,其定位就是轻量级的高效的对象序列化和反序列

Android中非aidl实现进程间通信(编写顺序的parcel写入与读出)

在android中进程间通信(IPC)的基石是Binder系统,Binder系统的核心Binder驱动是C来实现的,对于应用开发人员来说无疑晦涩难懂,而整个android框架是基于面向对象思想的,对于底层Binder驱动的操作细节全部隐藏,framework层提供了一个牛逼无比的Binder对象, 所以我们要实现进程间通信(IPC)只需玩转Binder对象即可. 在android源码中基于Binder对象的通信随处可见,几乎可以认定为以 I 打头的class,都具有进程间通信能力,如:IServ