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).