编程知识 cdmana.com

Android serialization problems and thinking

Let's talk about Android Object serialization in , How much do you know ?

What serialization means ? What's the usage?

serialize It means that the objects become orderly Byte stream , After it becomes a byte stream, a series of operations such as transfer and storage can be carried out .
Deserialization It's serialized Do the opposite , In other words, the bytes generated by serialization are transferred to objects in our memory .

introduce Android There are two serialization interfaces in

Serializable

yes Java A serialization interface provided , Is an empty interface , Provides serialization and deserialization operations specifically for objects . The specific use is as follows :

public class User implements Serializable { private static final long serialVersionUID=519067123721561165l;  private int id; public int getId() {  return id; } public void setId(int id) {  this.id = id; }}

Realization Serializable Interface , Make a statement serialVersionUID.

Someone may have asked here , Not ah , I don't usually have this serialVersionUID ah . you 're right ,serialVersionUID It's not necessary , Because if you don't write , The system will automatically generate this variable . What's the use of it ? When serializing , The system will put the current class serialVersionUID Write to the serialized file , This will be detected when deserializing serialVersionUID, See if he's with the current class serialVersionUID Agreement , The same can be deserialized normally , If it's not the same, you'll get a wrong report .

So this serialVersionUID It is an identifier in the process of serialization and deserialization , For consistency . What effect would it have if we didn't add it ? If we serialize , Changed some member variables of this class , that serialVersionUID Will change , At this time, if you take the previously serialized data to deserialize, an error will be reported . So if we manually specify serialVersionUID To ensure maximum recovery of data .

Serializable In fact, the essence of is to put Java Objects are serialized into binary files , And then you can pass... Between processes , And for network transmission or local storage and other operations , Because of its nature, it stores files . You can look at the source code :

private void writeObject0(Object obj, boolean unshared) throws IOException{ ... try {    Object orig = obj;  Class<?> cl = obj.getClass();  ObjectStreamClass desc;    desc = ObjectStreamClass.lookup(cl, true);   if (obj instanceof Class) {   writeClass((Class) obj, unshared);  } else if (obj instanceof ObjectStreamClass) {   writeClassDesc((ObjectStreamClass) obj, unshared);  // END Android-changed: Make Class and ObjectStreamClass replaceable.  } else if (obj instanceof String) {   writeString((String) obj, unshared);  } else if (cl.isArray()) {   writeArray(obj, desc, unshared);  } else if (obj instanceof Enum) {   writeEnum((Enum<?>) obj, desc, unshared);  } else if (obj instanceof Serializable) {   writeOrdinaryObject(obj, desc, unshared);  } else {   if (extendedDebugInfo) {    throw new NotSerializableException(     cl.getName() + "\n" + debugInfoStack.toString());   } else {    throw new NotSerializableException(cl.getName());   }  } }  ...}private void writeOrdinaryObject(Object obj,          ObjectStreamClass desc,          boolean unshared)  throws IOException {  ...  try {   desc.checkSerialize();      // Writing binary files , Magic numbers at the beginning of ordinary objects 0x73   bout.writeByte(TC_OBJECT);   // Write the descriptor of the corresponding class , See the bottom of the source code    writeClassDesc(desc, false);      handles.assign(unshared ? null : obj);   if (desc.isExternalizable() && !desc.isProxy()) {    writeExternalData((Externalizable) obj);   } else {    writeSerialData(obj, desc);   }  } finally {   if (extendedDebugInfo) {    debugInfoStack.pop();   }  } } public long getSerialVersionUID() {  //  If there is no definition serialVersionUID, The serialization mechanism will call a function to calculate a hash value   if (suid == null) {   suid = AccessController.doPrivileged(    new PrivilegedAction<Long>() {     public Long run() {      return computeDefaultSUID(cl);     }    }   );  }  return suid.longValue(); }

You can see that it is through reflection to get information about the object and its properties , And then write the data to a Binary , And write the serialization protocol version and so on .
And get ·serialVersionUID· The logic is also reflected , If id If it is empty, a calculation will be generated hash value .

Parcelable

Android The interface that comes with it , It's a lot of trouble to use : Need to achieve Parcelable Interface , rewrite describeContents(),writeToParcel(Parcel dest, @WriteFlags int flags), And add a static member variable CREATOR And implement Parcelable.Creator Interface

public class User implements Parcelable {  private int id; protected User(Parcel in) {  id = in.read.........

版权声明
本文为[Irving the procedural ape]所创,转载请带上原文链接,感谢

Scroll to Top