Lesson 68. A little about Parcel


In this lesson:

– get acquainted with Parcel

Parcel itself has never been used to me before and I don’t know if it will. He intrigued me when I began to understand the Parcelable interface. This interface is used when transferring objects through Intent and I was curious how to create my own objects with the support of such transfer. As a result, I got a bit of a grasp on Parcel and Parcelable, though I didn’t understand everything. I’ll try to talk about it now.

Parcel is a data container. He has a bunch of methods for storing and retrieving data. In this lesson, let’s look at the simplest of them.

Let’s create a project:

Project name: P0681_Parcel
Build Target: Android 2.3.3
Application name: Parcel
Package name: ru.startandroid.develop.p0681parcel
Create Activity: MainActivity

We won’t need the screen in this lesson, we leave the main.xml unchanged. We will work with the beam.

Kodyma in MainActivity.java:

package ru.startandroid.develop.p0681parcel;

import android.app.Activity;
import android.os.Bundle;
import android.os.Parcel;
import android.util.Log;

public class MainActivity extends Activity {

  final String LOG_TAG = "myLogs";
  Parcel p;

  /** Called when the activity is first created. */
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    writeParcel();
    readParcel();
  }

  void writeParcel() {
    p = Parcel.obtain();

    byte b = 1;
    int i = 2;
    long l = 3;
    float f = 4;
    double d = 5;
    String s = "abcdefgh";

    logWriteInfo("before writing");
    p.writeByte(b);
    logWriteInfo("byte");
    p.writeInt(i);
    logWriteInfo("int");
    p.writeLong(l);
    logWriteInfo("long");
    p.writeFloat(f);
    logWriteInfo("float");
    p.writeDouble(d);
    logWriteInfo("double");
    p.writeString(s);
    logWriteInfo("String");
  }

  void logWriteInfo(String txt) {
    Log.d(LOG_TAG, txt + ": " + "dataSize = " + p.dataSize());
  }

  void readParcel() {
  }

  void logReadInfo(String txt) {
  }

}

method writeParcel – we get a copy of Parcel, describe a set of variables and write them in Parcel, using the appropriate methods for this purpose. After each entry, log information about Parcel is logged using the logWriteInfo method.

method logWriteInfo writes logs about Parcel. dataSize is the volume of recorded data.

methods readParcel and logReadInfo – while empty. Let’s fill in later.

We all save and launch the application. We look at the log.

before writing: dataSize = 0
byte: dataSize = 4
int: dataSize = 8
long: dataSize = 16
float: dataSize = 20
double: dataSize = 28
String: dataSize = 52

Let’s sort it out in order.

before writing: Before recording we have a data size of 0. Recorded byte: DataSize = 4 (4 bytes were used to write byte data). recorded int: DataSize = 8 (4 more bytes were used to write int data in addition to 4 bytes previously filled). recorded long: DataSize = 16 (8 more bytes were used to write long in addition to the 8 bytes previously filled with byte and int). Etc. As a result, we see that dataSize shows how many bytes are occupied.

Note the types int, long, float and double occupy as many bytes as they actually occupy in Java – respectively 4, 8, 4 and 8. byte – instead of one byte for some reason it took the whole 4. And String each character uses two bytes, but writes more service information, so more is obtained.

Now let’s try to read what they wrote down. Fill in the blank reading methods:

  void readParcel() {
    logReadInfo("before reading");
    p.setDataPosition(0);
    logReadInfo("byte = " + p.readByte());
    logReadInfo("int = " + p.readInt());
    logReadInfo("long = " + p.readLong());
    logReadInfo("float = " + p.readFloat());
    logReadInfo("double = " + p.readDouble());
    logReadInfo("string = " + p.readString());
  }

  void logReadInfo(String txt) {
    Log.d(LOG_TAG, txt + ": " + "dataPosition = " + p.dataPosition());
  } 

In the method readParcel we set (setDataPosition method) to 0 because we need to read from the beginning. We read the data in the same order as recorded: byte, int, long, float, double, String. Output read result and current position (dataPosition).

Let’s save everything, run the program and watch the log.

The first lines of the recording beam are already familiar to us. We are interested in reading lines.

before reading: dataPosition = 52
byte = 1: dataPosition = 4
int = 2: dataPosition = 8
long = 3: dataPosition = 16
float = 4.0: dataPosition = 20
double = 5.0: dataPosition = 28
string = abcdefgh: dataPosition = 52

Before we set the position to 0 (before reading), We see that it is equal to 52. It is there after recording. Each data entry moves a position to the number equal to the size of the recorded data. The size of all sequentially recorded data for us was 52, and the position has accordingly shifted to 52. You can display as a experiment the position after each data entry. I only output for reading procedures.

So, we set the position to 0 and start reading the data. read the values byte, It is 1, as we wrote down. The position has shifted to the size of the read value, and now we will read from position 4. Read int, It is 2, the position has shifted to 8. etc.

All the values ​​we consistently recorded, we in the same order considered. Here you should understand that if you have recorded int, And then you will read double, Then the result will turn out not the right one. Because int writes 4 bytes, a double reads 8. Thus he climbs to the next recorded type and takes from it 4 bytes. Out came porridge. So here you need to be careful.

You can always set the position you want and count the stored value. The main thing is to know what type is stored there. For example, we now write double from position 20 when writing a double, so we can translate a position to 20 and run readDouble. We will successfully get recorded double there, and the position will be equal to 28.

If you want to watch Parcel content you can use its marshall () method, it will return an array of Parcel bytes written.

Here’s a short excursion. This knowledge will be required to understand the next lesson.

In the next lesson:

– we add support to our object Parcelable
– pass the object using Intent




Discuss in the forum [2 replies]

Leave a Comment