我在反序列化对象时遇到此异常:
控制台输出
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)
我正在尝试在不使用文件的情况下反序列化对象。该对象是使用 DatagramSocket 发送的。
从我调用的java小程序:
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);
}
另一方面:
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";
}
其中 env.getObjects() 返回“对象和对象是可序列化类的 LinkedList。
有什么问题?