java.io.Serializable
interface as shown in Listing 1.import java.io.Serializable;
class TestSerial implements Serializable {
public byte version = 100;
public byte count = 0;
}
* This source code was highlighted with Source Code Highlighter .
java.io.Serializable
interface. The Serializable
interface is a marker interface; there is no method declared in it. But it tells the serializing mechanism that the class can be serialized.writeObject()
method of the java.io.ObjectOutputStream
class, as shown in Listing 2.public static void main( String args[]) throws IOException {
FileOutputStream fos = new FileOutputStream( "temp.out" );
ObjectOutputStream oos = new ObjectOutputStream(fos);
TestSerial ts = new TestSerial();
oos.writeObject(ts);
oos.flush();
oos.close();
}
* This source code was highlighted with Source Code Highlighter .
TestSerial
instance to a file called temp.out
public static void main( String args[]) throws IOException {
FileInputStream fis = new FileInputStream( "temp.out" );
ObjectInputStream oin = new ObjectInputStream(fis);
TestSerial ts = (TestSerial) oin.readObject();
System. out .println( "version=" +ts.version);
}
* This source code was highlighted with Source Code Highlighter .
oin.readObject()
method. The method reads a set of bytes from the file and creates an exact copy of the graph of the original object. oin.readObject()
can read any serialized object, so you must oin.readObject()
resulting object to a specific type.version=100
to standard output.TestSerial
class and writes to temp.out
. Listing 4 shows the contents of the temp.out
file, in hexadecimal.AC ED 00 05 73 72 00 0A 53 65 72 69 61 6C 54 65
73 74 A0 0C 34 00 FE B1 DD F9 02 00 02 42 00 05
63 6F 75 6E 74 42 00 07 76 65 72 73 69 6F 6E 78
70 00 64
TestSerial
again, you will see that it has only 2 byte members. As shown in Listing 5.public byte version = 100;
public byte count = 0;
* This source code was highlighted with Source Code Highlighter .
java.lang.object
is reachedclass parent implements Serializable {
int parentVersion = 10;
}
class contain implements Serializable {
int containVersion = 11;
}
public class SerialTest extends parent implements Serializable {
int version = 66;
contain con = new contain();
public int getVersion() {
return version;
}
public static void main( String args[]) throws IOException {
FileOutputStream fos = new FileOutputStream( "temp.out" );
ObjectOutputStream oos = new ObjectOutputStream(fos);
SerialTest st = new SerialTest();
oos.writeObject(st);
oos.flush();
oos.close();
}
}
* This source code was highlighted with Source Code Highlighter .
SerialTest
class is SerialTest
, which is inherited from parent
and contains the container object of the contain
class. Listing 7 shows the serialized object.AC ED 00 05 73 72 00 0A 53 65 72 69 61 6C 54 65
73 74 05 52 81 5A AC 66 02 F6 02 00 02 49 00 07
76 65 72 73 69 6F 6E 4C 00 03 63 6F 6E 74 00 09
4C 63 6F 6E 74 61 69 6E 3B 78 72 00 06 70 61 72
65 6E 74 0E DB D2 BD 85 EE 63 7A 02 00 01 49 00
0D 70 61 72 65 6E 74 56 65 72 73 69 6F 6E 78 70
00 00 00 0A 00 00 00 42 73 72 00 07 63 6F 6E 74
61 69 6E FC BB E6 0E FB CB 60 C7 02 00 01 49 00
0E 63 6F 6E 74 61 69 6E 56 65 72 73 69 6F 6E 78
70 00 00 00 0B
AC ED: STREAM_MAGIC
. Says that the serialization protocol is used.00 05: STREAM_VERSION
. Serialization version.0x73: TC_OBJECT
. The designation of the new object.SerialTest
class was serialized, hence the algorithm began to record the description of the SerailTest
class.0x72: TC_CLASSDESC
. Designation of a new class.00 0A
: The length of the class name.53 65 72 69 61 6c 54 65 73 74: SerialTest
, class name.05 52 81 5A AC 66 02 F6: SerialVersionUID
, class identifier.0x02
: Various flags. This particular flag indicates that the object supports serialization.00 02
: The number of fields in the class.int version = 66;
.0x49
: Field type code. 49 is “I”, which is assigned to Int.00 07
: The length of the field name.76 65 72 73 69 6F 6E: version
, field name.contain con = new contain();
. This object will therefore be written in the canonical JVM designation of this field.0x74: TC_STRING
. Indicates a new line.00 09
: The length of the string.4C 63 6F 6E 74 61 69 6E 3B: Lcontain;
, Canonical JVM notation.0x78: TC_ENDBLOCKDATA
, End of optional data block for an object.parent
class, which is the immediate superclass for SerialTest
.0x72: TC_CLASSDESC
. Designation of a new class.00 06
: The length of the class name.70 61 72 65 6E 74: parent
, class name0E DB D2 BD 85 EE 63 7A: SerialVersionUID
, class identifier.0x02
: Various flags. This flag indicates that the class supports serialization.00 01
: The number of fields in the class.parent
class fields, the class has one field, int parentVersion = 100;
.0x49
: Field type code. 49 stands for "I", which is assigned to Int.00 0D
: The length of the field name.70 61 72 65 6E 74 56 65 72 73 69 6F 6E
: parentVersion, field name.0x78: TC_ENDBLOCKDATA
, end of optional data block for object.0x70: TC_NULL
, means that there are no more superclasses, because we have reached the top of the class hierarchy.parent
class are written first:00 00 00 0A: 10
, The value of parentVersion
.SerialTest
00 00 00 42: 66
, version
value.contain
class.contain con = new contain();
* This source code was highlighted with Source Code Highlighter .
contain
class description. The time has come to do it.0x73: TC_OBJECT
, indicates a new object.0x72: TC_CLASSDESC
, denotes a new class.00 07
: The length of the class name.63 6F 6E 74 61 69 6E: contain
, class name.FC BB E6 0E FB CB 60 C7: SerialVersionUID
, identifier of this class.0x02
: Various flags. This flag indicates that the class supports serialization.00 01
: The number of fields in the class.conatin, int containVersion = 11;
.0x49
: Field type code. 49 stands for "I", which is assigned to Int.00 0E
: The length of the field name.63 6F 6E 74 61 69 6E 56 65 72 73 69 6F 6E: containVersion
, field name.0x78: TC_ENDBLOCKDATA
, end of optional data block for object.contain
parent class. If it does, then the algorithm starts recording this class; but in our case there is no superclass for contain
, and the algorithm writes TC_NULL
.0x70: TC_NULL
conatin
.00 00 00 0B: 11
, the value of containVersion
.Source: https://habr.com/ru/post/60317/
All Articles