Parcel storage type and data container in Android Development

Source: Internet
Author: User
Tags object serialization

In android, Parcel is used to bind containers that store basic data types and reference data types through IBinder. This method enables data to be transmitted between processes, parcel positioning is a lightweight and efficient object serialization and deserialization mechanism.
The Framework has the parcel class. The source code path is:
Frameworks/base/core/java/android/OS/Parcel. java
Typical source code snippets are as follows:
/
Write an integer value into the parcel at the current dataPosition (),
Growing dataCapacity () if needed.
/
Public final native void writeInt (int val );
/
Write a long integer value into the parcel at the current dataPosition (),
Growing dataCapacity () if needed.
/
Public final native void writeLong (long val );
The method is as follows: www.2cto.com
Parcel parcel = Parcel. obtain ();//
After obtaining a Parcel object, you can perform operations on it,
CreateXXX (), wirteXXX (), readXXX (),
DataPosition () returns the offset of the data stored in the current Parcel object, while setDataPosition () sets the offset of the current Parcel object to facilitate reading data in parcel, the problem is that the data I read is either null or always the value at the first offset. What mechanism does Parcel adopt and what form of storage is used, so that I can operate on it at will.
Value range of basic data types: boolean 1bit, short 16bit, int 32bit, long 64bit, float 32bit, double 64bit, char 16bit, and byte 8bit
From this, I can guess that Parcel 32bit is used as the basic unit to store the written variables. 4 byte * 8 = 32bit. The referenced address variable in the memory is encoded in hexadecimal notation and used as the offset, that is, the offset is a multiple of 4, 16, 20, 24, 28, 32, 36, 40, 44, 48 ...... 4 * N, f (x) = 4 * y {y> = 0 & y is a natural number}
I don't think there will be data like a forward offset of 3, 6, and 9... From this we can infer that no matter whether it stores a basic data type or a variable that references the data type, it uses the basic unit of 32 bits as the offset.
Parcel. writeInt (1); parcel. writeInt (2); parcel. writeInt (3); parcel. writeInt (4); parcel. writeInt (5); parcel. writeInt (6); parcel. writeInt (7); parcel. writeInt (81011111); parcel. writeFloat (1f); parcel. writeFloat (partition F); parcel. writeXXX (): Every time data is written, the variable to be put can be stored in a 32-bit space. How can it occupy only one offset, that is, four moves.
When the stored data such as parcel. writeFloat,
Parcel. writeString ("a"); parcel. writeString ("B"); parcel. writeString ("d"); parcel. writeString ("c"); and parcel. writeString ("abcd.
It can be seen that the original memory allocation is like this. So how can I sequentially extract the data I have stored? SetDataPosition (), set the offset of parcel, read data in readXXX ()
Int size = parcel. dataSize ();
Int I = 0;
While (I <= size)
{
Parcel. setDataPosition (I );
Int curr_int = parcel. readInt ();
I + = 4;
Int j = 0;
J ++;
}
It can be seen that the data written by parcel is stored in a 32-bit container in sequence, basic and reference (in fact, there are also a combination of multiple basic data types into OBJECTS-attribute | method ), when reading data, we can extract the data that has been stored according to this rule according to the offset position (curr_position) of the target data and the offset size (size.
Int I = curr_position;
While (I <= size)
{
Parcel. setDataPosition (I );
Int curr_int = parcel. readXXXt ();
I + = 4;
Int j = 0;
J ++;
}
Finally, I would like to remind you that in java, the value range of the basic data type refers to the data of the reference type, which is equivalent to the pointer in c, and the conversion and flexible reference between hexadecimal systems, and customize any hexadecimal data types you want. Into serialization, serialization ......, It can store data objects in byte streams and regenerate objects as needed. The main application is to use an external storage device to save the object status and transmit objects over the network.


From the column of andy_android

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.