Deserializing an Object that has been serialized using ObjectOutputStream's writeObject method

69 views Asked by At

If you serialize an Object using ObjectOutputStream's writeObject method, is the only way to deserialize it through ObjectInputStream's readObject method? Can you call ObjectInputStream's other read methods?

1

There are 1 answers

0
user207421 On

Items in object streams are tagged. If you try to use the wrong readXXX() primitive to read them you will get an EOFException. The only methods you can use to read a written object are readObject() and readUnshared(). You can always read the raw bytes with the read() methods of course, but you'll probably make it impossible to use any of the readXXX() methods afterwards, by moving to an arbitrary position in the stream.