This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes How to know the number of items in your Seralization/Deserialization stream? 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 know the number of items in your Seralization/Deserialization stream?" Watch "How to know the number of items in your Seralization/Deserialization stream?" New topic
Author

How to know the number of items in your Seralization/Deserialization stream?

Jay Orsaw
Ranch Hand

Joined: Jun 14, 2011
Posts: 356

I'm basically trying to figure out if there is a method to know how many items I've seralized?




I want to be able to make an array of faces so for instance and then serialize the arrays by using a for loop

Then I want to be able to Deserialize it back into another face array in another class. The problem is I have no idea how to actually know how many are in the stream if it's a variable.



I want to be able to do something like



Not sure if I'm using ois or if I should be using something else?

Thanks,

~JO
Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287
The following code may help you. Always prefer lists to arrays.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Harsha Smith wrote:

ObjectInputStream.readObject does not return null when no more objects can be found. You would need to write a trailing null object in the serialization process. I don't like that approach.

I see three other options:
- In the serialization process, create an array with the objects and write that using a single writeObject call. In the deserialization process you read this array with a single readObject call.
- In the serialization process, create a List with the objects and write that using a single writeObject call. In the deserialization process you read this List with a single readObject call.
- In the serialization process, you first use writeInt to write the number of objects. You then write the objects using a writeObject call for each object. In the deserialization process you first use readInt to read the number of objects. You then call readObject exactly that amount of times.

I prefer the last option. It's also the one used by many classes in the Collections Framework, among others ArrayList, LinkedList, HashMap and TreeMap (HashMap and TreeMap write the key and value as separate objects so it's two writeObject calls per entry for serializing and two readObject calls per entry for deserializing).


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287
Without modifying the serialization process, the problem should be handled because the code that deserializes the object might not know how the objects have been serialized or written to the file.

All you have is a file that contains serialized objects. we have to handle this.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

If you don't know the number of objects, all you have left is catch the exception that is thrown by readObject. That's a very bad way to handle this, so if possible you really should modify the entire serialization process.
Jay Orsaw
Ranch Hand

Joined: Jun 14, 2011
Posts: 356

Rob Spoor wrote:
Harsha Smith wrote:

ObjectInputStream.readObject does not return null when no more objects can be found. You would need to write a trailing null object in the serialization process. I don't like that approach.

I see three other options:
- In the serialization process, create an array with the objects and write that using a single writeObject call. In the deserialization process you read this array with a single readObject call.
- In the serialization process, create a List with the objects and write that using a single writeObject call. In the deserialization process you read this List with a single readObject call.
- In the serialization process, you first use writeInt to write the number of objects. You then write the objects using a writeObject call for each object. In the deserialization process you first use readInt to read the number of objects. You then call readObject exactly that amount of times.

I prefer the last option. It's also the one used by many classes in the Collections Framework, among others ArrayList, LinkedList, HashMap and TreeMap (HashMap and TreeMap write the key and value as separate objects so it's two writeObject calls per entry for serializing and two readObject calls per entry for deserializing).


Yeah I wanted to try and do it either 1-2 by just looping, but I don't think that will work... I like the idea of Reading in the integers value of the amount, and then looping the deserialization on that #? Should I not be using an array? I figured that would be the best approach, but I could look into using an Arraylist.


Harsha was saying about how my deserialization might not know the serialization, but how would I do what he is talking about? Once you right off to the file, the 2 sets of code shouldn't interact... I don't know what the point would be since you're trying to just write, and the read from the file... Unless there is something special to be picked up?

Thanks for all of your help!!!
 
GeeCON Prague 2014
 
subject: How to know the number of items in your Seralization/Deserialization stream?