This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Serialization Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Serialization Problem" Watch "Serialization Problem" New topic
Author

Serialization Problem

Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

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.



SCJP 6. Learning more now.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

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:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

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

Joined: Oct 27, 2005
Posts: 19655
    
  18

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

Joined: Jan 21, 2011
Posts: 637

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

Joined: Oct 27, 2005
Posts: 19655
    
  18

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.
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

Why does making these methods public stop them from working?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

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

Joined: Jan 21, 2011
Posts: 637

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

Joined: Oct 27, 2005
Posts: 19655
    
  18

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

Joined: Jan 21, 2011
Posts: 637

Rob Spoor wrote:.....


Thanks a Bazillion !!!
bazillion is a "very big" number !
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Serialization Problem
 
Similar Threads
about serialization
Serialization: Non serialized class issue
A query regarding serialization in Java
Serialization JSCP 6.0 (manually invoking writeObject() )
Serialization