Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

EOF Exception

 
Salman Mushtaq
Greenhorn
Posts: 27
Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone !
Whenever i write a program about Streams and serialization i faced EOF exception where i read Object

Object obj = null;
obj = br.readObject();
while(obj !=null)
{
...
...
obj = br.readObject();
}
Here compiler show me the error please explain .
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, how are the objects serialized in the first place? My guess is, someone just serialized one or more objects, then closed the stream. You code, on the other hand, assumes that there will be a null reference sent. Apparently, there isn't one, and you get EOF instead.

Basically, both sender and receiver need to agree on a communication protocol. In this case, you've presumably agreed on what type of objects are being sent, but you don't know how to tell when the objects are done. There are several possibilities:

1. Agree on a special value that will be sent to indicate the end of your objects. Null is a good possibility. In that case, have the sender be sure and send a null, before they close the stream.

2. Before your list of objects, send an int representing the number of objects that will be sent. Then loop that number of times, and exit.

3. Wrap the objects in an array or list, and serialize that instead.

4. Just catch the EOFException and move on. That's the signal for the end of your processing.

Options 1-3 require the sender to change their code, as well as the receiver. You may or may not be able to do this. Options 2 and 3 require that you know how many objects there are before you send them. You may or may not be able to do this, as well. Personally I like option 1 best, but if you can't get the sender to change their code, option 4 may be your only option.
 
Salman Mushtaq
Greenhorn
Posts: 27
Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes i got the idea ,
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!