A simple example of using the C ++ program to read and write Parcel in Android

Source: Internet
Author: User

The parcel class in Android puzzles me for a while (of course I haven't fully figured out parcel now), and I don't know what format it uses to store data after checking some information. Because the Read and Write functions provided by it do not mention the specific Read and Write function in which part of the data is read and written in parcel. For example, there are two codes on the Internet:

Public void writetoparcel (parcel out) {// write the current data to parcel
Out. writeint (left );
Out. writeint (top );
Out. writeint (right );
Out. writeint (bottom );
}

Public void readfromparcel (parcel in) {// read data from parcel
Left = in. readint ();
Top = in. readint ();
Right = in. readint ();
Bottom = in. readint ();
}

Whether it is the READ function writeint () or the write function readint (), the parameter does not mention where to write the left variable to the specific part of the parcel, it also reads the value from which part of parcel is assigned to the right variable. Later I checked some source code, including parcel. h and parcel. cpp. It seems that parcel may be similar to a one-dimensional data string, and various variables can be written in the past (through the specific functions provided by parcel for various variables ), there is a position pointer pointing to a position in this one-dimensional data string, which is the default read/write position. That is to say, if the read/write function is called now, it reads and writes the data at the pointer at the current position, move the position pointer back to a space (the length of the span is exactly the length of the data you have read or written in the last read/write function call) and continue to read and write the next part of the space.

To verify this idea, I wrote a C ++ program that reads and writes parcel. Due to the unfamiliar nature of C ++, it took me one afternoon to write this small program, but it was finally passed. Paste the code below:

/********************* The following is the parcel_test.cpp program ************* ***************/
/* The file extension here should be. CPP, that is, the C ++ file. If C is used to write a program, the extension is. c, add # include <utils/parcel. h> An error may occur after this sentence */
# Include <utils/parcel. h>
# Include <stdio. h>
# Include <stdlib. h>

Int main ()
{
Using namespace android;/* this line is not added at the beginning, and the result is always an error, prompting that parcel, status_t, and no_error are not defined. It has been depressing for a long time, I want to add utils/parcel to include. h, and in Android. parcel is also added to MK. the library libutils where H is located. After reading the android source code, we found that this sentence may be added */
Status_t status;
Size_t parcel_position;
Int INTP = 987654;
Char CHARP = 'G ';
Float floatp = 3.14159;
Double doublep = 12345.6789012;
Long longp = 1234567890;
Char * stringp = "this is my parcel test! ";

Parcel P;
Parcel_position = P. dataposition ();/* backup location */
Printf ("the current read/write location of parcel is: % d \ n", parcel_position );
/************* Write the int type ***************/
Status = p. writeInt32 (intp );
If (status = NO_ERROR)
Printf ("write int type success! \ N ");
Else
Printf ("write int type fail, the errno = % d \ n", errno );
/************* Write the char type ***************/
Status = p. writeInt32 (charp );
If (status = NO_ERROR)
Printf ("write char type success! \ N ");
Else
Printf ("write char type fail, the errno = % d \ n", errno );
/************* Write Float Type ***************/
Status = p. writeFloat (floatp );
If (status = NO_ERROR)
Printf ("write Float type success! \ N ");
Else
Printf ("write Float type fail, the errno = % d \ n", errno );
/************* Write Double type ***************/
Status = p. writeDouble (doublep );
If (status = NO_ERROR)
Printf ("write Double type success! \ N ");
Else
Printf ("write Double type fail, the errno = % d \ n", errno );
/************* Write the long type ***************/
Status = p. writeInt64 (longp );
If (status = NO_ERROR)
Printf ("write long type success! \ N ");
Else
Printf ("write long type fail, the errno = % d \ n", errno );
/************* Write the string type ***************/
Status = P. writecstring (stringp );
If (status = no_error)
Printf ("Write string type success! \ N ");
Else
Printf ("Write string type fail, the errno = % d \ n", errno );
/************ Set the parcel read and write location back to the original position ***************/
P. setdataposition (parcel_position );
/************* Read the variable ***************/
Printf ("read int type variables: % d \ n", p. readint32 ());
Printf ("read char type variable: % C \ n", (char) p. readint32 ());
Printf ("read Float type variables: % f \ n", (float) p. readFloat ());
Printf ("the Double type variables read are: % f \ n", (double) p. readDouble ());
Printf ("the variables of the read long type are % ld \ n", (long) p. readInt64 ());
Printf ("read string: % s \ n", p. readCString ());
}
/********************* The above is the Parcel_test.cpp program ************* ***************/

******************/
/*********************** The following is Android. mk file ****************************/
LOCAL_PATH: = $ (call my-dir)
Include $ (CLEAR_VARS)

LOCAL_SRC_FILES: = \
Parcel_test.cpp

LOCAL_SHARED_LIBRARIES: = \
Libutils \
Libcutils

LOCAL_CFLAGS: =

LOCAL_MODULE: = parcel_test

Include $ (build_executable)
/*********************** And later are android. MK file ****************************/

Put these two files in the parcel_test folder under the Development directory under the android source code directory (the DL folder is newly created), and then use the root permission on the terminal to enter the android source code directory, run make parcel_test. After successful execution, the parcel_test executable file is generated in the android source code directory/out/target/product/generic/system/bin.

Run the following command to put them into the android simulator.

ADB push Android source code directory/out/target/product/generic/system/bin/parcel_test/Data

Go to the data folder and execute
ADB Shell
# CD Data
#./Parcel_test
/// // The running result is as follows ////////// ////////////////////
The read/write position of the current parcel is: 0.
Write int type success!
Write char type success!
Write float type success!
Write double type success!
Write long type success!
Write string type success!
Int type variables read: 987654
Read char type variable: g
Read Float type variable: 3.141590
Read the Double type variable: 12345.678901
The read long type variable is: 1234567890
The read string is: this is my parcel test!

====================================
We can see that the data storage structure in Parcel is indeed a data string, and a position pointer marks its current read/write position, as we have guessed before. When writing data, you can follow certain conventions to write the data in certain order, and read the data in the same order. It is estimated that some data can be read or written through the function that sets the pointer position, but I did not perform the experiment here.

In addition, if you want to read the data after writing the data, remember to set the position pointer to the start point of the data to be read before reading the data, because the Data Pointer has been moved before writing.

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.