Sunday, 26 August 2012

Understanding java.io.StreamCorruptedException: invalid type code: AC

We can transport Objects in Java in two ways

  1. Using Serialization 
  2. Using RMI ( Remote Method Invocation ) 
last night i was using 1st approach to transport Objects from my server to client, while doing so my code throws 
java.io.StreamCorruptedException: invalid type code: AC

Lets see first what Javadoc has to say about this 
StreamCorruptedException :-  Thrown when control information that was read from an object stream violates internal consistency checks.
 Do you understand any thing ???

No ???

Then, continue reading ...

 So lets look at this exception closely 


This exception (java.io.StreamCorruptedException: invalid type code: AC) only occurs when any one use's a new ObjectOutputStream to write to an existing ObjectInputStream that you have already used a prior ObjectOutputStream to write to. These streams have headers which are written and read by the respective constructors, so if you start another ObjectOutputStream you will write a new header, which starts with - guess what? - 0xAC, and the existing ObjectInputStream isn't expecting another header at this point so it barfs.

So now I guess you have enough understanding about this Exception and next time you will not commit this mistake, and hence this will escape you from wasting enormous amount of time figuring out why this exception is thrown

So the bottom line is

  1. Always use only one ObjectOutputStream & corresponding ObjectInputStream through out the life of Socket
  2. If you are writing data over Socket using ObjectOutputStream then read that data only using  ObjectInputStream otherwise Exception could be thrown
  3. If you want to forget about what you have written then do 
    objectOutputStream.reset();
    


4 comments:

  1. I have similar problem throwing this exception. I am implementing chat client. Client has to subscribe on server, this is fine. In case that other clients log in, I want the server to notify all already logged clients about this fact(to display new client in client list).

    This means, that I need loop on server to listen to events from client, and loop on client to listen for events fired from server. Is there a way how to accomplish this using only one socket? Because if I do it, I get exactly this exception - I have ObjectOutputStream and ObjectInputStream on both client and server, which is the cause. Clearly I cannot use same instance of these streams on both sides.

    Only way left to go is to open second socket, but I would like to avoid it. Any ideas? Thanks

    ReplyDelete
  2. Sorry for the late response, as i was busy with my examz in order to help you i need to know what exactly you are trying to accomplish and what is the problem in your algorithm, without the very details i can't help. Just saying that code is throwing an exception is not just enough.

    If you wanna share your problem than drop me a mail.
    bye

    ReplyDelete
  3. You just saved me a lot of stress and worry. Thanks, I appreciate it.

    ReplyDelete