• 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

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

 
Ranch Hand
Posts: 356
Android Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following code may help you. Always prefer lists to arrays.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).
 
Harsha Smith
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
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
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
Posts: 356
Android Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!!!
 
Tongue wrestling. It's not what you think. And here, take this tiny ad. You'll need it.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic