Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Urgent help needed--EOFException  RSS feed

 
fei long
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, i have a problem about ObjectInputStream. my part of code looks like this:
---------------------------------------------
byte[] message = something.
bInput = new ByteArrayInputStream(message);
wInput = new ObjectInputStream(bInput);
aObject = (aObjectType)wInput.readObject();
-------------------------------------------
when i read object from this stream, i always got java.io.EOFException. Please help me.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Errr... what's "something"? The only thing an ObjectInputStream knows how to read is a stream of bytes which were created by an ObjectOutputStream. (Or by some other method which follows the Object Serialization Specification, which you probably don't want to mess around with yourself.) If you've got an array of bytes created some other way, ObjectInputStream will almost certainly throw an exception. What are you trying to do?
 
fei long
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, yes.
actually, i have some codes before that.
-------------------------------------------
message = "some message".getBytes();
mp.Initialize(message, messageSize, messageNumber);
bOutput = new ByteArrayOutputStream(256);
wOutput = new ObjectOutputStream(bOutput);
wOutput.writeObject(mp);
----------------------------------------------
'mp' is a of defined in my class.
and follows the code mentioned before
----------------------------------------------
DatagramPacket packet;
byte[] message;
message = packet.getData();
bInput = new ByteArrayInputStream(message);
wInput = new ObjectInputStream(bInput);
mp = (MulticastPacket) wInput.readObject();
----------------------------------------------
actually, i am doing a socket programing simulating multicast. I have two .class, and they are sending and receiving packet from each other. I found the problem is 'wInput.readObject()', but i don't know how to solve it. please help me.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the EOFException seems to indicate that the stream is ending before the ObjectInputStream finds bytes to form a (complete) object. You seem to write the object OK - do you call wOutput.close() afterwards (which should make sure the serialization is complete)? And after that, do you call bOutput.toByteArray() to get the bytes? And somehow transfer them to the packet which is read later? It might be useful to print out the length of the byte array returned by toByteArray(). And do the same for the message extracted from the packet later, to see if they're the same size. If not, you know something's gone wrong in the transfer. Experiment with different messages to see if the serialized length increases or decreases according to the message length. You can even look at the actual bytes in the message to see if you understand them. Part of it should be a stream of UTF-8 for the text message (which looks like ASCII, so it should be readable). You might want to look at the Object Serialization Specification to try to understand if the byte array is actually a valid serialized message. Good luck...
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!