Reputation: 77
Im getting this exception while deserializing an object:
Output on the console
sending request: GET_OBJS
java.io.EOFException
receiving response
at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.readUTFBody(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.readUTF(Unknown Source)
at java.io.ObjectInputStream.readUTF(Unknown Source)
at java.io.ObjectStreamClass.readNonProxy(Unknown Source)
at java.io.ObjectInputStream.readClassDescriptor(Unknown Source)
at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
at java.io.ObjectInputStream.readClassDesc(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at thesis.bot.wab.applet.DApplet.extracted(DApplet.java:218)
I'm trying to deserialize an object without using a file. The object is sent using a DatagramSocket.
From a java applet i call:
sendPacket("GET_OBJS");
[...]
input=new ObjectInputStream(new ByteArrayInputStream(receivedPacket.getData()));
objs=extracted(input);
toWrite=Integer.toString(objs.size());
input.close();
sendPacket("PRINT_"+toWrite);
[...]
private LinkedList<Objects> extracted(ObjectInputStream obj)
throws IOException, ClassNotFoundException {
return (LinkedList<Objects>) obj.readObject();
}
private void sendPacket(String msg) throws IOException{
System.out.println("sending request: "+msg);
buf=msg.getBytes();
DatagramPacket tosend=new DatagramPacket(buf,buf.length,addr,9999);
socket.send(tosend);
}
on the other side:
else if(req.compareTo("GET_OBJS")==0){
Environement env=(Environement) ctx.getService(ctx.getServiceReference(Environement.class.getName()));
ByteArrayOutputStream baos=new ByteArrayOutputStream();
ObjectOutputStream out=new ObjectOutputStream(baos);
out.writeObject(env.getObjects());
out.close();
buf=baos.toByteArray();
resp="buffer_ready";
}
where env.getObjects() return a LinkedList of "Objects and Objects is a Serializable class.
What's the problem?
Upvotes: 1
Views: 1501
Reputation: 77
I've modified the code to use tcp socket and Now Im not getting exceptions.
Modified code:
From a java applet i call:
sendPacket("GET_OBJS");
[...]
ObjectInputStream ois=new ObjectInputStream(socket.getInputStream());
objs=extracted(ois);
System.out.println(objs.size());
[...]
private LinkedList<Objects> extracted(ObjectInputStream ois)
throws IOException, ClassNotFoundException {
return (LinkedList<Objects>) ois.readObject();
}
private void sendPacket(String msg) throws IOException{
bw.write(msg);
bw.newLine();
bw.flush();
System.out.println("Message sent");
}
on the other side:
else if(req.compareTo("GET_OBJS")==0){
Environement env=(Environement) ctx.getService(ctx.getServiceReference(Environement.class.getName()));
ObjectOutputStream out=new ObjectOutputStream(clientSock.getOutputStream());
out.flush();
out.writeObject(env.getObjects());
out.flush();
resp="buffer_ready";
}
Now it works! Thank u for the help!
PS: is necessary that the ObjectOutputStream calls flush() before starting write.
Upvotes: 2