Developing Java Beans Now that we have serialized

Developing Java Beans Now that we have serialized the objects to file Example5.tmp, let’s reconstruct the objects by running the application again as follows: java Example5 restore First an instance of java.io.FileInputStream is created based on the file Example5.tmp. The FileInputStream is then used to create an instance of class java.io.ObjectInputStream. Next, the instance of Container is reconstructed by calling the readObject() method on the stream. After that, the dump() method is called on the Container, which prints the following in the console window: 13:26:39 5.3 Class-Specific Serialization Sometimes the data members that are marked as transient are an important part of the run-time state of the object. Consider a class that maintains an instance of a java.io.FileOutputStream, a class that cannot be serialized because it uses a file handle. It would be dangerous to save the handle because it is allocated at run-time, and the objects may be reconstructed on another system that uses a different scheme for allocating file handles. Also, if the file handle were to be saved it could be modified to resemble a handle that is not normally accessible. This would be a security violation, and could even result in unwanted or malicious behavior. Nevertheless, when the object is reconstructed, we would want to reestablish the instance of the java.io.FileOutputStream. We might handle this by saving the name of the file along with the current value of the seek offset. It is possible to extend the default serialization behavior. Any class that implements java.io.Serializable can implement its own writeObject() and readObject() methods for this purpose. If you want the serialization mechanism to ask your object to write its own data, it should provide a writeObject() method with the following signature: private void writeObject(ObjectOutputStream stream) throws java.io.IOException; If this method exists, the data members will not automatically be serialized. It is up to the writeObject() method to store the state of the object. However, it doesn’t need to consider the state of any subclass or superclass data. That information will still be handled by the default serialization process. This means that you can store data that is not represented by data members. You can use write-Object() in order to add additional state information to the stream, including any relevant information about objects that are marked as transient. You should always call the defaultWriteObject() method of the java.io.ObjectOutputStream instance before you add any additional information. This is the method that implements default serialization of the non-static and non-transient data members of the object for the class being serialized. The same process is repeated for each class in an object’s inheritance hierarchy. After you invoke defaultWriteObject() on the stream, you are free to add anything else you want to that stream. If you implement writeObject() to store the state of your object, then you’ll need to implement readObject() so that you can be in control of the reconstruction of the object from the stream. The method signature for readObject() is as follows: private void readObject(ObjectInputStream stream) page 78

Comments are closed.