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机制,译成串行化.序列化……,其作用是能将数据对象存入字节流其中,在须要时又一次生成对象.主要应用是利用外部存储设备保存对象状态 ...
随机推荐
- iptables简单配置
iptables简单配置 分类: Linux 安全2010-10-20 16:56 4241人阅读 评论(0) 收藏 举报 input防火墙tcpfilterubuntuservice # iptab ...
- 接口测试脚本之Jsoup解析HTML
第一次接触jsoup还是在处理收货地址的时候,当时在写一个下单流程,需要省市区id以及详细门牌号等等,因此同事介绍了jsoup,闲来无事,在此闲扯一番! 1.我们来看下,什么是jsoup,先来看看官方 ...
- 把一个project相关的jar放到project的lib文件夹中
例如你有一个project名为xxx,/xxx 文件夹中有src.bin.等等,然后你手工创建一个lib文件夹,这样你就把解压出来的第三方lib(jar包,含javadoc)都放到lib文件夹中,这样 ...
- linux学习笔记9--命令cat
cat命令的用途是连接文件或标准输入并打印.这个命令常用来显示文件内容,或者将几个文件连接起来显示,或者从标准输入读取内容并显示,它常与重定向符号配合使用. cat命令连接文件并打印到标准输出设备上, ...
- C扩展php的方法(制作php扩展库)
用PHP调用C扩展整个配置过程在CentOS下 今天终于把C扩展加入到PHP中了,并且可以调用,废话就不说了,看下文. 一.必须先要安装Apache和mysql,这两个的安装过程我就不说了. ...
- 使用Net.Sockets.TcpListener和Net.Sockets.TcpClient进行图片传输时怎样精确控制接收缓存数组大小
<span style="font-size:18px;">在dotnet平台Net.Sockets.TcpListener和Net.Sockets.TcpClient ...
- Easyui Datagrid的Rownumber行号显示问题
Datagrid中当你的行数据超过9999时,第一列的行号rownumber将会因为表格内容过长而导致无法显示全部数字, 这一点Easyui无法做到自适应 所以需要进行修改,这里扩展一个方法就行了. ...
- Oracle启动中,startup nomount、 startup mount 有什么差别?
Oracle启动中,startup nomount. startup mount 有什么差别? 解答: startup nomount:启动实例,读取参数文件,分配内存空间,启动后台进程,打开跟踪文件 ...
- jdk与jre的区别(转)
很多程序员已经干了一段时间java了依然不明白jdk与jre的区别.JDK就是Java Development Kit.简单的说JDK是面向开发人员使用的SDK,它提供了Java的开发环境和运行环境. ...
- 2018 ACM-ICPC 北京赛区小结 @ Reconquista
Statistics TYPE: Onsite Contest NAME: 2018 - ICPC Regional - Asia EC - Beijing PLAT: Hihocoder TIME: ...