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的更多相关文章
- android 进程间通信数据(一)------parcel的起源
关于parcel,我们先来讲讲它的“父辈” Serialize. Serialize 是java提供的一套序列化机制.但是为什么要序列化,怎么序列化,序列化是怎么做到的,我们将在本文探讨下. 一:ja ...
- 如何利用Cloudera Manager来手动安装parcel包
1.问题的描述: 当你利用Cloudera Manager部署了CDH的集群后,也许随着你的业务需求,你需要对你的就去哪做一些优化,或者扩展之类的,这个时候你可能需要下载安装一些组件.例如,我最近在阅 ...
- android 进程间通信数据(二)------parcel的实现
Serialize是java原生就自带的东西,我们可以看到android的源码 所以看看android是如何实现parcel的,这对我们自己代码设计有什么启发. Parcel: 在android中,p ...
- Cloudera CDH 、Impala本地通过Parcel安装配置详解及什么是Parcel
本文引用自:Cloudera CDH .Impala本地通过Parcel安装配置详解及什么是Parcelhttp://www.aboutyun.com/forum.php?mod=viewthread ...
- Cloudera CDH 、Impala本地通过Parcel安装配置详解
一.Parcel本地源与Package本地源的区别 本地通过Parcel安装过程与本地通过Package安装过程完全一致,不同的是两者的本地源的配置. 区别如下: Package本地源:软件包是.rp ...
- ANDROID_MARS学习笔记_S01原始版_018_SERVICE之Parcel
一.代码 1.xml(1)activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk ...
- Android中的Parcel机制 实现Bundle传递对象
Android中的Parcel机制 实现了Bundle传递对象 使用Bundle传递对象,首先要将其序列化,但是,在Android中要使用这种传递对象的方式需要用到Android Parc ...
- android 对象传输及parcel机制
在开发中不少要用到Activity直接传输对象,下面我们来看看,其实跟java里面差不多 自定义对象的传递:通过intent传递自定义对象的方法有两个 第一是实现Serialization接口: ...
- 能取悦生理期的女性吗?Le Parcel提供女性卫生用品按月订购服务,不是按包出售而是可以按片自由搭配 | 36氪
能取悦生理期的女性吗?Le Parcel提供女性卫生用品按月订购服务,不是按包出售而是可以按片自由搭配 | 36氪 能取悦生理期的女性吗?Le Parcel提供女性卫生用品按月订购服务,不是按包出售而 ...
- 探索Android中的Parcel机制(上)
一.先从Serialize说起 我们都知道JAVA中的Serialize机制,译成串行化.序列化……,其作用是能将数据对象存入字节流其中,在须要时又一次生成对象.主要应用是利用外部存储设备保存对象状态 ...
随机推荐
- 李洪强和你一起学习前端之(5)css书写位置 优先级和伪类
亲爱的,时间过得真快,不知不觉我们一起学习前端已经4天了,这4天的时间里,我们是不是收获很大呢,每当我们学习一个新的知识点的时候,每当我们做出一个新的东西来的时候,我们是不是欣喜若狂,世界从来否不会辜 ...
- rdb 和 aof
Redis 中 默认会开启rdb 持久化方式,aof 默认不开启,Redis 提供不同级别的持久化方式rdb: 在指定的时间间隔对你的数据进行快照存储aof:记录每次Redis服务写操作,当Redis ...
- UITextField/UITextView限制字数
一,UITextFild限制字数(三步) 1,给textfild添加响应事件,类型为:UIControlEventEditingChanged [self.nickNameFild addTarget ...
- PHP代码审计学习之命令执行漏洞挖掘及防御
[1]可能存在命令执行漏洞的函数: 00x1:常用的命令执行函数:exec.system.shell_exec.passthru 00x2:常用的函数处理函数:call_user_func.call_ ...
- :(23, 7) in class Queen, multiple overloaded alternatives of constructor Queen define default arguments. class Queen private(val name:String,prop:Array[String],private[scala02] val age:Int = 18){
- jqgrid的排序问题
今天是本人解决的一个小bug 所以写的粗略些. 问题是这样的ORDER BY a.$sidx $sord 当时本人排序时候没用jqgrid的默认排序(可能今天这个大家看不懂,很抱歉啊各位,今天主要 ...
- 《linux系统及其编程》实验课记录(二)
实验 2:获取对使用命令的帮助 实验环境: 安装了 Red Hat Enterprise Linux 6.0 可运行系统,并且是成功验证系统.有另外一个无特权用户 student,密码 student ...
- 【Raspberry Pi】修改时区
Raspberry Pi没有时钟模块,所以每次断电都会丢失时间,但它有联网获取时间的预设.但要修改默认时区 http://outofmemory.cn/code-snippet/2899/shumei ...
- 【Raspberry pi】系统安装及基础配置
1.系统安装 见官网:http://www.raspberrypi.org/quick-start-guide 2.基础配置 转载自http://www.eeboard.com/bbs/thread- ...
- WPF MVVM(Caliburn.Micro) 数据验证
书接前文 前文中仅是WPF验证中的一种,我们暂且称之为View端的验证(因为其验证规是写在Xaml文件中的). 还有一种我们称之为Model端验证,Model通过继承IDataErrorInfo接口来 ...