Only 48 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Bug in ObjectOutputStream  RSS feed

 
Geoffrey Falk
Ranch Hand
Posts: 171
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code demonstrates that ObjectOutputStream may not write the current contents of an object.

We initialize an array to [null] and write it to an output stream. Then we change the value of an array element to a non-null value, and write it again. This program demonstrates that [null] is again written, instead of the current contents of the array.

 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not a bug, but documented behavior. ObjectOutputStream keeps track of every object that it writes. When you write an object a second time, a reference to that object is written instead of a new copy of that object's data. This is how circular references are dealt with. So once you write your array, writing it again wwill just cause a reference to the earlier copy to be written; the current state won't be inspected.

You can call reset() on an ObjectOutputStream to flush the cache of old object references and start anew -- but note that if you write an object, change it, call reset(), and write it again, deserialization will give you two separate objects.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!