File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to identify the end of an ObjectInputStream

 
Sanjaya Amarasinghe
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 385
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20388
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sanjaya Amarasinghe
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much.
I will serialize a whole vector with objects.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic