This week's book giveaway is in the HTML/CSS/JavaScript forum.
We're giving away four copies of Practical SVG and have Chris Coyier on-line!
See this thread for details.
Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

OptionalDataException....???

 
Siddharth Ramavat
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

I was trying to read an arraylist from an inputstream... which was sent by servlet to my swing client... During this it threw a OptionalDataException on my Tomcat console.. I know that When I am try to read the object when the next element in the stream is premitive data type then this excepion is generates.. But I don't get exact idea that why this is happening b'cz I am only trying to read an arraylist .. and there is no premitive data type..

Waiting for your reply,
Regards,
Siddharth
 
Joe Ess
Bartender
Posts: 9362
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's two reasons for this exception:

# An attempt was made to read an object when the next element in the stream is primitive data. In this case, the OptionalDataException's length field is set to the number of bytes of primitive data immediately readable from the stream, and the eof field is set to false.
# An attempt was made to read past the end of data consumable by a class-defined readObject or readExternal method. In this case, the OptionalDataException's eof field is set to true, and the length field is set to 0.

Java Doc for OptionalDataException
So if you want to check for the particular cause, you can examine the eof and length fields.
Are you reading the array list with a single readObject() call?
 
Siddharth Ramavat
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for reply,

Yes, you are right then I am using single readObject() to read the arraylist. But actually what happned that before reading the arraylist I was reading the boolean value from the stream.. so now after reading it I want to clear the input sream . so is there any method like flush() or something with the inputstream so I can flush out the boolean value and then going for reading array list..

Waiting for your reply,

Regards,
Siddharth
 
Joe Ess
Bartender
Posts: 9362
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no "flush" for incoming data. Once you read it, it's gone.
Are you reading a Boolean or a boolean?
Did you try checking the length and eof fields of the exception?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!