aspose file tools*
The moose likes I/O and Streams and the fly likes How to identify the end of an ObjectInputStream 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 "How to identify the end of an ObjectInputStream" Watch "How to identify the end of an ObjectInputStream" New topic
Author

How to identify the end of an ObjectInputStream

Sanjaya Amarasinghe
Greenhorn

Joined: Dec 11, 2009
Posts: 2
I want to get the serialized objects in a file to a vector.( For a library system for an assignment)
But I didn't come across a good way to identify the end of the ObjectInputStream.
What is the condition I must add for the while loop in the following code.


Here is a sample code:



This gives an output like this :


Object Written
0
java.io.StreamCorruptedException: invalid type code: AC
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at saveObj.main(saveObj.java:51)

Siva Masilamani
Ranch Hand

Joined: Sep 19, 2008
Posts: 385
Instead of writing two objects seperately you could put them in a ArrayList or vector and serialize that object.

Then when you do de-serialization first cast the object to ArrayList or Vector.then iterate them and using instanceof operator you could cast each element to either class b or b1.

But still b and b1 should implements serializable interface.


SCJP 6,SCWCD 5,SCBCD 5

Failure is not an option.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19785
    
  20

Siva Masilamani wrote:Instead of writing two objects seperately you could put them in a ArrayList or vector and serialize that object.

You can save the creation of one ArrayList / Vector by serializing how ArrayList does it internally:
1) write the number of objects
2) write the objects themselves

De-serializing is then:
1) read the number of objects
2) read that many different objects


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Sanjaya Amarasinghe
Greenhorn

Joined: Dec 11, 2009
Posts: 2
Thank you very much.
I will serialize a whole vector with objects.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to identify the end of an ObjectInputStream