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. ajax发送请求时为url添加参数(使用函数)

    <script> // ajax的get请求,使用函数向其url添加参数 function addURLParam(url,name,value){ url+=(url.indexOf(' ...

  2. session用户账号认证(一个用户登陆,踢出前一个用户)

    在web.xml中配置: <listener> <listener-class>cn.edu.hbcf.common.listener.SessionAttributeList ...

  3. springboot2.0 fastjson 日期格式化不生效解决

    网上有很多介绍,主要有两种. 1.在启动类中注入bean /** * 集成fastjson * * @return */ @Bean public HttpMessageConverters fast ...

  4. matlab hornerDemo

    % a quick demo of Horner's method and its effects clear all close all % first a comparison of ways t ...

  5. 后台运行 screen命令

    nohub不能用的,用这个 后台运行 yum install screen 只要Screen本身没有终止,在其内部运行的会话都可以恢复 登录到主机上执行screen -r就可以恢复会话的运行. 同样在 ...

  6. FXGameEngine

    最初是在 http://jayskills.com/ 这个网站发现的,但一直未找到源码,今天偶然发现其源码位于 https://github.com/eppleton/FXGameEngine 下载, ...

  7. vmware复制虚拟机出现Error:No suitable device found:no device found for connection 'System eth0'

    vmware复制虚拟机出现Error:No suitable device found:no device found for connection 'System eth0' 废话不多说,直接给出解 ...

  8. 04 Java图形化界面设计——布局管理器之BorderLayout(边界布局)

    边界布局管理器把容器的的布局分为五个位置:CENTER.EAST.WEST.NORTH.SOUTH.依次对应为:上北(NORTH).下南(SOUTH).左西(WEST).右东(EAST),中(CENT ...

  9. jquery取iframe中元素

    采取方法: $("#iframe_path").contents().find(".select_path_hide").val(); DOM方法:父窗口操作I ...

  10. Laravel5.1 登录和注册

    关于登录和注册 Laravel自带了一套组件实现了这一功能,我们只需要实现简单的视图即可. AuthController是专门管理用户注册和登录的. PassWordController是重置密码用的 ...