ClassCastException反序列化时,无法将ObjectStreamClass强制转换为java.lang.String

时间:2013-05-10 11:14:23

标签: java serialization deserialization classcastexception

我正在尝试序列化,发送和接收以下类的实例(ServerGameDataObject object = new ServerGameDataObject())

public class ServerGameDataObject implements Serializable {
    private static final long serialVersionUID = 5410058267505412928L;

    private TestClass t1 =null;
    private TestClass t2 =null;

    public ServerGameDataObject(){

        t1 = new TestClass();
        t2 = new TestClass();

    }
}

其中TestClass的定义是:

import java.io.Serializable;


public class TestClass  implements Serializable{
    transient public static final long serialVersionUID = -3089302783006861199L;

    private int val =-1;
    public  TestClass(){
        val=-1;

    }
}

..... 对象按如下方式发送:

    ObjectOutputStream o = new AppendingObjectOutputStream(
            <some outputstream>);

    o.writeUnshared(object);
    o.flush();

AppendingObjectOutputStream的定义是:

public class AppendingObjectOutputStream extends ObjectOutputStream {

    public AppendingObjectOutputStream(OutputStream out) throws IOException {
        super(out);
    }

    @Override
    protected void writeStreamHeader() throws IOException {
        // do not write a header, but reset:
        // this line added after another question
        // showed a problem with the original
        reset();
    }

}



 The code on the receiving end is :

    Object o = null;
   while ((o = inputStream.readUnshared()) != null) { // this is Receiver.java, line 74
    ...
    }

inputStream is an instance of ObjectInputStream 

在接收端抛出此异常:

> 05-10 10:54:51.572: E/AndroidRuntime(3133): FATAL EXCEPTION:
> Thread-134 05-10 10:54:51.572: E/AndroidRuntime(3133):
> java.lang.ClassCastException: java.io.ObjectStreamClass cannot be cast
> to java.lang.String 05-10 10:54:51.572: E/AndroidRuntime(3133):   at
> java.io.ObjectInputStream.readFieldDescriptors(ObjectInputStream.java:935)
> 05-10 10:54:51.572: E/AndroidRuntime(3133):   at
> java.io.ObjectInputStream.readClassDescriptor(ObjectInputStream.java:1744)
> 05-10 10:54:51.572: E/AndroidRuntime(3133):   at
> java.io.ObjectInputStream.readNewClassDesc(ObjectInputStream.java:1656)
> 05-10 10:54:51.572: E/AndroidRuntime(3133):   at
> java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:683)
> 05-10 10:54:51.572: E/AndroidRuntime(3133):   at
> java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1806)
> 05-10 10:54:51.572: E/AndroidRuntime(3133):   at
> java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:787)
> 05-10 10:54:51.572: E/AndroidRuntime(3133):   at
> java.io.ObjectInputStream.readObject(ObjectInputStream.java:2006)
> 05-10 10:54:51.572: E/AndroidRuntime(3133):   at
> java.io.ObjectInputStream.readObject(ObjectInputStream.java:1963)
> 05-10 10:54:51.572: E/AndroidRuntime(3133):   at
> java.io.ObjectInputStream.readFieldValues(ObjectInputStream.java:1140)
> 05-10 10:54:51.572: E/AndroidRuntime(3133):   at
> java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:455)
> 05-10 10:54:51.572: E/AndroidRuntime(3133):   at
> java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1372)
> 05-10 10:54:51.572: E/AndroidRuntime(3133):   at
> java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1269)
> 05-10 10:54:51.572: E/AndroidRuntime(3133):   at
> java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1858)
> 05-10 10:54:51.572: E/AndroidRuntime(3133):   at
> java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:787)
> 05-10 10:54:51.572: E/AndroidRuntime(3133):   at
> java.io.ObjectInputStream.readObject(ObjectInputStream.java:2006)
> 05-10 10:54:51.572: E/AndroidRuntime(3133):   at
> java.io.ObjectInputStream.readUnshared(ObjectInputStream.java:1978)
> 05-10 10:54:51.572: E/AndroidRuntime(3133):   at
> Receiver.run(Receiver.java:74)

如果在ServerGameDataObject内部,我将t2定义为瞬态(瞬态私有TestClass t2 = null;),那么我会对对象执行

怎么了?为什么我不能同时收到t1和t2?

1 个答案:

答案 0 :(得分:0)

这里犯了很多错误。

  1. 第一次写入时必须使用普通ObjectOutputStream,并且只有在追加时才能使用附加版本。如果输出是套接字,那么在套接字的生命周期中使用相同的ObjectOutputStream会更有意义。

  2. readObject()readUnshared()不会在EOS返回null。他们抛出EOFException。因此,在它们不返回null时循环是不正确的。