• 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 ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Serialization Problem

 
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is an attempt to make a code snippet from kathy sierra's scjp book into a fully working program. First an overview of the problem :

Dog class implements Serializable and has :
a size - an int
a Collar - an object

Collar class should not implement Serializable.
I want to serialize and then de-serialize a "dog" object inside SerialTransient class. I do not know what code to use in the lines between 48 and 52.


 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to serialize the Dog object first, then de-serialize it again. This means using an ObjectOutputStream and ObjectInputStream, using either a file (FileOutputStream and FileInputStream) or memory (ByteArrayOutputStream and ByteArrayInputStream) as backing streams:
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:You need to serialize the Dog object first, then de-serialize it again. This means using an ObjectOutputStream and ObjectInputStream, using either a file (FileOutputStream and FileInputStream) or memory (ByteArrayOutputStream and ByteArrayInputStream) as backing streams:



I put your code in a try-catch block and inserted it into my program an got a runtime exception. Can you explain your approach a little ?

 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Strange. I tried your code with my code in the middle* and it ran successfully. Then again, I didn't use try-catch but declared the main method to throw Exception (just to be lazy).

Can you show us the code you added? I think the problem is in there somewhere.
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:Strange. I tried your code with my code in the middle* and it ran successfully. Then again, I didn't use try-catch but declared the main method to throw Exception (just to be lazy).

Can you show us the code you added? I think the problem is in there somewhere.



This is the line of code which causes an exception at runtime :


This is the exception i get :


Here is the complete code :



How do i use these methods, its not mentioned in the book :
1. public void writeObject(ObjectOutputStream oos) throws Exception
2. public void readObject(ObjectInputStream ois) throws Exception

Thanks for helping me.
 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you seriously don't use any indentation in your code?

The problem is that the two methods only work when they are private. For some reason you made them public somewhere, because they were private in your original post.
 
Ranch Hand
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why does making these methods public stop them from working?
 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To be honest, I have no idea. This page says this about it:

Notice that both methods are (and must be) declared private, proving that neither method is inherited and overridden or overloaded. The trick here is that the virtual machine will automatically check to see if either method is declared during the corresponding method call. The virtual machine can call private methods of your class whenever it wants but no other objects can. Thus, the integrity of the class is maintained and the serialization protocol can continue to work as normal. The serialization protocol is always used the same way, by calling either ObjectOutputStream.writeObject() or ObjectInputStream.readObject(). So, even though those specialized private methods are provided, the object serialization works the same way as far as any calling object is concerned.

 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:You need to serialize the Dog object first, then de-serialize it again. This means using an ObjectOutputStream and ObjectInputStream, using either a file (FileOutputStream and FileInputStream) or memory (ByteArrayOutputStream and ByteArrayInputStream) as backing streams:



Can you tell me what is the role of :
line 4 and why do we need it ?
line 5 ? why did we have to create an array ?
 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When object are serialized using an ObjectOutputStream, there are more bytes written than just the objects themselves. There is some overhead, definitely at the start and probably also at the end. By closing the ObjectOutputStream you make sure that the overhead at the end is also written.

The byte[] is where the serialized objects are stored. Not in a File, because I didn't use a FileOutputStream / FileInputStream pair, but in memory as a byte[]. I could write the same in almost the same way using a file:
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:.....



Thanks a Bazillion !!!
bazillion is a "very big" number !
 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic