1

Im trying to send multiple serializable objects of different classes through a single

ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());

But, I always get a

java.io.StreamCorruptedException: invalid type code: AC

Ive been through the forum and tried out.reset() but it doesnt seem to work. Am I doing something wrong or missing something?

Please help

Rohit Vipin Mathews
  • 11,629
  • 15
  • 57
  • 112
gp_xps
  • 639
  • 1
  • 8
  • 12
  • Post the exact code of everything you are pushing into that stream, as well as the code that tries to read from it at the other end. This is, after all, an error on the reader side. – Marko Topolnik Apr 25 '12 at 10:02

2 Answers2

2

It is the stream that is corrupted, not the ObjectOutputStream. That is the name of a class.

This problem arises when you try to read the result of multiple ObjectOutputStreams with a single ObjectInputStream. It isn't valid. ObjectOutputStream writes a header which starts with - guess what? 0xAC. So when you use a single ObjectInputStream to read a stream that has been created by multiple ObjectOutputStreams, it finds an unexpected 0xAC and throws that exception.

Solution: don't do that. You can't append multiple ObjectOutputStreams to a file, and you can't use multiple ObjectOutputStreams over a socket unless you have extraordinary co-ordination sufficient to create a new ObjectInputStream at the precise point in the stream where the last ObjectOutputStream left off: instead, you must use the same ObjectInputStream and ObjectOutputStream for the life of the socket.

user207421
  • 305,947
  • 44
  • 307
  • 483
1

You are most likely using ObjectOutputStream or ObjectInputStream incorrectly.

Common mistakes include

  • using multiple Object stream on the same underlying stream. Use one and only one.
  • using Object Stream and another stream wrapper like a Writer/Reader or DataInput/OutputStream.

BTW: You should always create the ObjectOutputStream first and flush() it before using the ObjectInputStream.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130