• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Bug in ObjectOutputStream

Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

author and iconoclast
Posts: 24203
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
this llama doesn't want your drama, he just wants this tiny ad for his mama
Thread Boost feature
    Bookmark Topic Watch Topic
  • New Topic