wood burning stoves*
The moose likes I/O and Streams and the fly likes understand readObjectNoData callback - Serializable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "understand readObjectNoData callback - Serializable" Watch "understand readObjectNoData callback - Serializable" New topic
Author

understand readObjectNoData callback - Serializable

Adolfo Eloy
Ranch Hand

Joined: Mar 21, 2009
Posts: 146

Hi all.

I'm studying the java IO and faced the callback readObjectNoData at java API about Serializable interface.
I understood the use of readObject and writeObject callbacks, but this readObjectNoData is not clear for me.
So I'd like to see real example of using it. I've tried to create a Class "Monkey" that extends "Animal" (and Animal implements Serializable), and I've serialized it using FileOutputStream and ObjectOutputStream. So I changed the Monkey hierarchy making this class to extend Mammal instead. So, I thought it will be a good scenario to test the use of readObjectNoData, but I wrote a readObjectNoData within Monkey class to configure Mammal attributes manually. So my readObjectNoData aren't called when deserializing this object.

Could someone help me to understood better about readObjectNoData, and if possible show some example?

Tks!!


Adolfo Eloy
Software Developer
OCPJP 6
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18825
    
  40

Adolfo Eloy wrote:
I understood the use of readObject and writeObject callbacks, but this readObjectNoData is not clear for me.
I've tried to create a Class "Monkey" that extends "Animal" (and Animal implements Serializable). I changed the Monkey hierarchy making this class to extend Mammal instead. So, I thought it will be a good scenario to test the use of readObjectNoData, but I wrote a readObjectNoData within Monkey class to configure Mammal attributes manually. So my readObjectNoData aren't called when deserializing this object.


Never actually used this method, as I have never had the need to protect against corrupted / changing class hierarchies.

But... I always thought readObjectNoData() was similar to the readObject() method, but for the case where the data isn't available. Given that, in your example, shouldn't it be the Mammal class that has the method, to handle the case of configuring itself, when it is not in the serialized data?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Adolfo Eloy
Ranch Hand

Joined: Mar 21, 2009
Posts: 146

Henry Wong wrote:Given that, in your example, shouldn't it be the Mammal class that has the method, to handle the case of configuring itself, when it is not in the serialized data?


Thanks Henry Wong! It's exactly as you said. I was studying it today testing all possibilities and reading more about it.
I'd like to add some more information from my studying bellow only to document more about, and if I'm wrong, correct me if possible.

Considering an object Monkey that extends only Object java class and that it's serialized as a file in disk with monkey.dat filename. So consider that another day, this Monkey starts extending Animal (that also implements Serializable interface) and we try to restore this file (monkey.dat). ObjectInputStream will try to recover all data that belongs to the actual class hierarchy, but there is no Animal data within this file. So, in this case readObjectNoData will be invoked, Animal is the best place to implement readObjectNoData because there is no better one that knows how to be restored than Animal itself.

Thank you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: understand readObjectNoData callback - Serializable