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的更多相关文章

  1. android 进程间通信数据(一)------parcel的起源

    关于parcel,我们先来讲讲它的“父辈” Serialize. Serialize 是java提供的一套序列化机制.但是为什么要序列化,怎么序列化,序列化是怎么做到的,我们将在本文探讨下. 一:ja ...

  2. 如何利用Cloudera Manager来手动安装parcel包

    1.问题的描述: 当你利用Cloudera Manager部署了CDH的集群后,也许随着你的业务需求,你需要对你的就去哪做一些优化,或者扩展之类的,这个时候你可能需要下载安装一些组件.例如,我最近在阅 ...

  3. android 进程间通信数据(二)------parcel的实现

    Serialize是java原生就自带的东西,我们可以看到android的源码 所以看看android是如何实现parcel的,这对我们自己代码设计有什么启发. Parcel: 在android中,p ...

  4. Cloudera CDH 、Impala本地通过Parcel安装配置详解及什么是Parcel

    本文引用自:Cloudera CDH .Impala本地通过Parcel安装配置详解及什么是Parcelhttp://www.aboutyun.com/forum.php?mod=viewthread ...

  5. Cloudera CDH 、Impala本地通过Parcel安装配置详解

    一.Parcel本地源与Package本地源的区别 本地通过Parcel安装过程与本地通过Package安装过程完全一致,不同的是两者的本地源的配置. 区别如下: Package本地源:软件包是.rp ...

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

    一.代码 1.xml(1)activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk ...

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

    Android中的Parcel机制    实现了Bundle传递对象    使用Bundle传递对象,首先要将其序列化,但是,在Android中要使用这种传递对象的方式需要用到Android Parc ...

  8. android 对象传输及parcel机制

    在开发中不少要用到Activity直接传输对象,下面我们来看看,其实跟java里面差不多   自定义对象的传递:通过intent传递自定义对象的方法有两个  第一是实现Serialization接口: ...

  9. 能取悦生理期的女性吗?Le Parcel提供女性卫生用品按月订购服务,不是按包出售而是可以按片自由搭配 | 36氪

    能取悦生理期的女性吗?Le Parcel提供女性卫生用品按月订购服务,不是按包出售而是可以按片自由搭配 | 36氪 能取悦生理期的女性吗?Le Parcel提供女性卫生用品按月订购服务,不是按包出售而 ...

  10. 探索Android中的Parcel机制(上)

    一.先从Serialize说起 我们都知道JAVA中的Serialize机制,译成串行化.序列化……,其作用是能将数据对象存入字节流其中,在须要时又一次生成对象.主要应用是利用外部存储设备保存对象状态 ...

随机推荐

  1. python2和python3中str,bytes区别

    python2中,有basestring.str.bytes.unicode四种类型 其中str == bytes ,basestring = (str,unicode) >>> i ...

  2. Oracle之配置客户端登陆多个远程数据库

    一.引言 一直搞不明白Oracle数据库的客户端是怎么回事,怎么配置,前几天由于工作中需要用到Oracle,而且需要连接两个不同的数据库,就通过上网和请教同事终于把客户端的配置搞定了,记录之,学习之 ...

  3. Zabbix exp编写

    #/usr/bin/python #*-*coding=utf-8*-* import urllib logo = '''\n _____ _ _ _ _____ _ |__ /__ _| |__ | ...

  4. mysql查询条件字段值末尾有空格的问题

    mark MYSQL的binary解决mysql数据大小写敏感问题的方法

  5. css断句 word-break

    word-break:break-all;word-wrap:break-word; 兼容IE6 火狐 chrome

  6. flask 中xx.init_app(app)方法

    bootstrap = Bootstrap() mail = Mail() moment = Moment() db = SQLAlchemy() def create_app(config_name ...

  7. jQuery中bind函数绑定多个事件

    名人名言:道德是真理之花.——雨果 在jQuery中绑定多个事件名称是,使用空格隔开,举例如下: $("#foo").bind("mouseenter mouseleav ...

  8. LeetCode 第 231 题 (Power of Two)

    LeetCode 第 231 题 (Power of Two) Given an integer, write a function to determine if it is a power of ...

  9. 分享一个Unity3D小作品,源码地址已公布在文章开头!

    Update:回复量有点大,楼主工作期间可能无暇向童鞋们发送源码,为了不让童鞋们久等,现公布源码地址.  链接: http://pan.baidu.com/s/1sjpYW4d 密码: zhp9 请注 ...

  10. matlab中图片数据类型转换uint8与double

    matlab中处理图像像素点数据: img1=double(imread('lenna.bmp')); matlab中imshow图片,要先转换成uint8: subplot(1,2,1),imsho ...