<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ro">
	<id>http://wiki.dcae.pub.ro/index.php?action=history&amp;feed=atom&amp;title=Serialization</id>
	<title>Serialization - Revizia istoricului</title>
	<link rel="self" type="application/atom+xml" href="http://wiki.dcae.pub.ro/index.php?action=history&amp;feed=atom&amp;title=Serialization"/>
	<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Serialization&amp;action=history"/>
	<updated>2026-05-27T17:42:18Z</updated>
	<subtitle>Istoricul versiunilor pentru această pagină din wiki</subtitle>
	<generator>MediaWiki 1.35.14</generator>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Serialization&amp;diff=1603&amp;oldid=prev</id>
		<title>Rhobincu: Pagină nouă: Besides Input/Output streams which were thoroughly described in the stream chapter, the Java language also provides a powerful system of serializing objects. We already know that a cl...</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Serialization&amp;diff=1603&amp;oldid=prev"/>
		<updated>2013-12-23T11:01:15Z</updated>

		<summary type="html">&lt;p&gt;Pagină nouă: Besides Input/Output streams which were thoroughly described in the stream chapter, the Java language also provides a powerful system of serializing objects. We already know that a cl...&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Pagină nouă&lt;/b&gt;&lt;/p&gt;&lt;div&gt;Besides Input/Output streams which were thoroughly described in the stream chapter, the Java language also provides a powerful system of serializing objects. We already know that a class is a structure that encapsulates data and functionality. Serialization is the procedure through which the data encapsulated in an instance of a class are sent and received by streams. The classes that do this are [http://docs.oracle.com/javase/7/docs/api/java/io/ObjectOutputStream.html java.io.ObjectOutputStream] and [http://docs.oracle.com/javase/7/docs/api/java/io/ObjectInputStream.html java.io.ObjectInputStream], and the methods used are &amp;#039;&amp;#039;writeObject(Object)&amp;#039;&amp;#039; and &amp;#039;&amp;#039;readObject()&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;regula&amp;quot;&amp;gt;&amp;lt;font color=&amp;quot;#ff0000&amp;quot;&amp;gt;Rule:&amp;lt;/font&amp;gt; For a class be serializable (or better said, for the objects of that class to be serializable), the class has to implement the [http://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html java.io.Serializable] interface:&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public class Question implements Serializable{&lt;br /&gt;
&lt;br /&gt;
    public String questionBody;&lt;br /&gt;
    public String[] answers;&lt;br /&gt;
    public int correctAnswerIndex;&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Object Serialization ==&lt;br /&gt;
&lt;br /&gt;
[[Fișier:objoutputstream.png|The block diagram of an ObjectOutputStream with an associated OutputStream]]&lt;br /&gt;
&lt;br /&gt;
An &amp;#039;&amp;#039;&amp;#039;ObjectOutputStream&amp;#039;&amp;#039;&amp;#039; is, just as class &amp;#039;&amp;#039;&amp;#039;FilterOutputStream&amp;#039;&amp;#039;&amp;#039;, a &amp;quot;wrapper&amp;quot; that needs another &amp;#039;&amp;#039;&amp;#039;OutputStream&amp;#039;&amp;#039;&amp;#039; in order to function. The role of &amp;#039;&amp;#039;&amp;#039;ObjectOutputStream&amp;#039;&amp;#039;&amp;#039; is to transform an object into a sequence of bytes which can be then written to the byte-level stream:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public class ObjectWriter{&lt;br /&gt;
&lt;br /&gt;
public static void main(String[] _args){&lt;br /&gt;
    try{&lt;br /&gt;
        OutputStream _byteStream = getSomeOutputStream();&lt;br /&gt;
        ObjectOutputStream _objectStream = new ObjectOutputStream(_byteStream);&lt;br /&gt;
        Question _q1 = new Question();&lt;br /&gt;
        _objectStream.writeObject(_q1);&lt;br /&gt;
        _objectStream.close();&lt;br /&gt;
    }catch(IOException _ioe){&lt;br /&gt;
        System.out.println(&amp;quot;Unable to write object: &amp;quot; + _ioe.getMessage());&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public static OutputStream getSomeOutputStream(){&lt;br /&gt;
    //...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Static fields are not serialized (since they don&amp;#039;t belong to an object, and serialization is only used for objects). If the serialized object contains references to other objects, those are recursively serialized, following the same rules. This is called creating a &amp;quot;deep copy&amp;quot; of the original object.&lt;br /&gt;
&lt;br /&gt;
There is an option of NOT serializing a field in an object, if that field is defined as &amp;#039;&amp;#039;&amp;#039;transient&amp;#039;&amp;#039;&amp;#039;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public class Question implements Serializable{&lt;br /&gt;
&lt;br /&gt;
    public String questionBody;&lt;br /&gt;
    public String[] answers;&lt;br /&gt;
    public transient int correctAnswerIndex;&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Object De-Serialization ==&lt;br /&gt;
&lt;br /&gt;
[[Fișier:objinputstream.png|Block diagram of an ObjectInputStream with an associated InputStream]]&lt;br /&gt;
&lt;br /&gt;
Reading serialized objects is done by using an objects of type &amp;#039;&amp;#039;&amp;#039;ObjectInputStream&amp;#039;&amp;#039;&amp;#039; which uses another generic &amp;#039;&amp;#039;&amp;#039;InputStream&amp;#039;&amp;#039;&amp;#039;. As specified above, a serialized objects is in fact a sequence of bytes representing the values of each non-static, non-transient field of the class. This only contains minimal information about the class, to, in order for an object to be deserialized, the application that reads it needs to have available the class of which the object is an instance of. If this class is not available, then the method &amp;#039;&amp;#039;readObject()&amp;#039;&amp;#039; will throw an exeception of type [http://docs.oracle.com/javase/7/docs/api/java/lang/ClassNotFoundException.html java.lang.ClassNotFoundException].&lt;br /&gt;
&lt;br /&gt;
The class checking is done at runtime (during the program execution, during the call to the &amp;#039;&amp;#039;readObject()&amp;#039;&amp;#039; method) and it also involves comparing a field defined as &amp;#039;&amp;#039;static final long serialVersionUID&amp;#039;&amp;#039; in the class, with the value contained by the serialized object. So, it is not sufficient that the source and destination classes have identical source codes, in order for them to be compatible during serialization, they have to have the same value for field &amp;#039;&amp;#039;serialVersionUID&amp;#039;&amp;#039;. If this value is not specified by the programmer, it is generated by the compiler during class compilation and it may differ for the same source code, compiled on two different computers:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public class Question implements Serializable{&lt;br /&gt;
&lt;br /&gt;
    static final long serialVersionUID = 0x0000CAFEBABE0000;&lt;br /&gt;
&lt;br /&gt;
    public String questionBody;&lt;br /&gt;
    public String[] answers;&lt;br /&gt;
    public int correctAnswerIndex;&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If any problems appear during deserialization, then the method &amp;#039;&amp;#039;readObject()&amp;#039;&amp;#039; will throw an exception of type [http://docs.oracle.com/javase/7/docs/api/java/io/InvalidClassException.html java.io.InvalidClassException]. All serialization related exceptions are extended from [http://docs.oracle.com/javase/7/docs/api/java/io/ObjectStreamException.html java.io.ObjectStreamException].&lt;br /&gt;
&lt;br /&gt;
In order to be compatible with any kind of object, method &amp;#039;&amp;#039;readObject()&amp;#039;&amp;#039; from class &amp;#039;&amp;#039;&amp;#039;ObjectInputStream&amp;#039;&amp;#039;&amp;#039;  returns a reference to an &amp;#039;&amp;#039;&amp;#039;Object&amp;#039;&amp;#039;&amp;#039;. So, an explicit down-cast is required in order to use the object type class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.io.*;&lt;br /&gt;
&lt;br /&gt;
public class ObjectReader{&lt;br /&gt;
&lt;br /&gt;
public static void main(String[] _args){&lt;br /&gt;
    try{&lt;br /&gt;
        InputStream _byteStream = getSomeInputStream();&lt;br /&gt;
        ObjectInputStream _objectStream = new ObjectInputStream(_byteStream);&lt;br /&gt;
        Question _q1;&lt;br /&gt;
        _q1 = (Question)_objectStream.readObject();&lt;br /&gt;
        _objectStream.close();&lt;br /&gt;
    }catch (ClassNotFoundException _cnfe) {&lt;br /&gt;
        System.out.println(&amp;quot;Class not available: &amp;quot; + _cnfe.getMessage());&lt;br /&gt;
    }catch(ObjectStreamException _ose){&lt;br /&gt;
        // ObjectStreamException first since it inherits IOException&lt;br /&gt;
        System.out.println(&amp;quot;Unable to deserialize (Object stream problem): &amp;quot; + _ose.getMessage());&lt;br /&gt;
    }catch(IOException _ioe){&lt;br /&gt;
        System.out.println(&amp;quot;Unable to deserialize (Byte stream problem): &amp;quot; + _ioe.getMessage());&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public static InputStream getSomeInputStream(){&lt;br /&gt;
    //...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Known Serialization Issues ==&lt;br /&gt;
&lt;br /&gt;
# If an object is written then read from a stream, the read object will have a different reference than the original one, so it will, in fact, be a copy of the object, with the same content.&lt;br /&gt;
# The virtual machine created a cache of objects when these are written. So, if an object is written on the stream, modified, then written again, the change will not be reflected during the second serialization, since it is written from the cache. There are two work-arounds:&lt;br /&gt;
#* you can copy/ clone the original object, change it, and write the copy;&lt;br /&gt;
#* you can call &amp;#039;&amp;#039;&amp;#039;ObjectOutputStream.reset()&amp;#039;&amp;#039;&amp;#039; which will empty the cache.&lt;br /&gt;
# If you write a lot of different objects without clearing the cache, this cache will become so large that it can eventually occupy all of the heap memory, crashing your program with an exception of type [http://docs.oracle.com/javase/7/docs/api/java/lang/OutOfMemoryError.html java.lang.OutOfMemoryError].&lt;br /&gt;
# If a resource contains both an &amp;#039;&amp;#039;&amp;#039;InputStream&amp;#039;&amp;#039;&amp;#039; and an &amp;#039;&amp;#039;&amp;#039;OutputStream&amp;#039;&amp;#039;&amp;#039; (like a Socket), and it is required to create an &amp;#039;&amp;#039;&amp;#039;ObjectInputStream&amp;#039;&amp;#039;&amp;#039; and an &amp;#039;&amp;#039;&amp;#039;ObjectOutputStream&amp;#039;&amp;#039;&amp;#039; on top of them, always create the &amp;#039;&amp;#039;&amp;#039;ObjectOutputStream&amp;#039;&amp;#039;&amp;#039; first, or the &amp;#039;&amp;#039;&amp;#039;ObjectInputStream&amp;#039;&amp;#039;&amp;#039; constructor will block until the other side of the stream is opened, potentially hanging your application.&lt;/div&gt;</summary>
		<author><name>Rhobincu</name></author>
	</entry>
</feed>