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机制,译成串行化.序列化……,其作用是能将数据对象存入字节流其中,在须要时又一次生成对象.主要应用是利用外部存储设备保存对象状态 ...
随机推荐
- Oracle之批量生成数据
一.引言 由于测试程序,需要大量的数据 二.方法 1.pl/sql的Generate Data,在tool菜单中可以找到,但是我这里不能用,老是出现错误,应该是软件的原因,但是没找到解决办法,如下图: ...
- vs2008、vs2012添加公共头文件
当我们使用第三方库(opencv.boost)的时候,往往需要把它们的头文件和库文件添加到工程中去,然而如果每次新建工程都添加,那就太笨了,下面介绍方法可以让每个新建的工程都默认添加设置好的头文件和库 ...
- 15.3.14 DP练习2
拦截导弹 题目 某国为了防御敌国的导弹突击,发展出一种导弹拦截系统. 可是这样的导弹拦截系统有一个缺陷:尽管它的第一发炮弹可以到达随意的高度.可是以后每一发炮弹都不能高于前一发的高度. 某天,雷达捕捉 ...
- [转]Mac系统安装软件提示软件已损坏的解决办法
很多朋友下载本站提供的Mac软件资源,安装提示“xxx软件已损坏,打不开,您应该将它移到废纸篓”的提示,其实并不是软件本身有问题,而是Mac系统的一个安全机制问题,按照如下方法操作,即可打开并安装本站 ...
- shell课后总结
shell课后总结 作者:高波 归档:学习笔记 2017年12月4日13:31:08 快捷键: Ctrl + 1 标题1 Ctrl + 2 标题2 Ctrl + 3 标题3 Ctrl + 4 ...
- CI框架中 日志输出方法log_message()只允许输出字符串解决方案
1.修改CodeIgniter/index.php,添加如下: define('ROOTDIR',dirname(__FILE__).'/'); 2.修改CodeIgniter/application ...
- python爬取网站数据保存使用的方法
这篇文章主要介绍了使用Python从网上爬取特定属性数据保存的方法,其中解决了编码问题和如何使用正则匹配数据的方法,详情看下文 编码问题因为涉及到中文,所以必然地涉及到了编码的问题,这一次借这 ...
- JAVA源文件中是否可以包括多个类,有什么限制
JAVA源文件中是否可以包括多个类,有什么限制 解答:一个java源文件中可以包含多个类,每个源文件中至多有一个public类,如果有的话,那么源文件的名字必须与之相同.如果源文件中没有public类 ...
- EJB EJB定义了一组可重用的组件:Enterprise Beans
EJB EJB定义了一组可重用的组件:Enterprise Beans.开发人员可以利用这些组件,像搭积木一样建立分布式应用.
- 【vijos】1286 座位安排(状压dp)
https://vijos.org/p/1286 这题好神不会捉... 首先我们知道所有情况有C(n*m, k)种,这个好搞.但是两两不相邻这个有点难搞.. 原来是状压dp..sigh. 设状态f[i ...