`java.io.StreamCorruptedException:从文件中读取某些对象时意外的块数据结束

时间:2014-10-03 19:44:24

标签: java serialization deserialization

我有一些非常基本的序列化代码:

void serializeObjectToFile(Serializable serializable, File file) {
    ObjectOutputStream stream = null;
    try {
        stream = new ObjectOutputStream(new FileOutputStream(file));
        stream.writeObject(serializable);
        stream.flush();
    } catch (Exception exception) {
        logger.error("Failed to serialize object: {}.", serializable, exception);
    } finally {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException exc) {
                logger.error("Error closing stream.", exc);
            }
        }
    }
}

还有一些非常反序列化的代码:

Object deserializeObjectFromFile(File file) {
    ObjectInputStream oiStream = null;
    try {
        oiStream = new ObjectInputStream(new FileInputStream(file));
        return oiStream.readObject();
    } catch (Exception exc) {
        logger.error("Exception loading object from file '{}'.", file.getAbsolutePath(), exc);
    } finally {
        if (oiStream != null) {
            try {
                oiStream.close();
            } catch (IOException exc) {
                logger.error("Error closing stream.", exc);
            }
        }
    }
    return null;
}

此代码适用于大多数对象。但对于某些对象,我得到以下异常:

java.io.StreamCorruptedException: unexpected end of block data
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1350) ~[na:1.6.0_29]
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1946) ~[na:1.6.0_29]
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1870) ~[na:1.6.0_29]
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1752) ~[na:1.6.0_29]
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1328) ~[na:1.6.0_29]
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1946) ~[na:1.6.0_29]
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1870) ~[na:1.6.0_29]
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1752) ~[na:1.6.0_29]
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1328) ~[na:1.6.0_29]
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:350) ~[na:1.6.0_29]
at java.util.ArrayList.readObject(ArrayList.java:593) ~[na:1.6.0_29]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.6.0_29]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) ~[na:1.6.0_29]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) ~[na:1.6.0_29]
at java.lang.reflect.Method.invoke(Method.java:597) ~[na:1.6.0_29]
at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:974) ~[na:1.6.0_29]
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1848) ~[na:1.6.0_29]
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1752) ~[na:1.6.0_29]
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1328) ~[na:1.6.0_29]
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1946) ~[na:1.6.0_29]
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1870) ~[na:1.6.0_29]
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1752) ~[na:1.6.0_29]
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1328) ~[na:1.6.0_29]
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:350) ~[na:1.6.0_29]
at my.CustomClass.deserializeObjectFromFile(CustomClass.java:79) ~[classes/:na]

我不会尝试为每个文件序列化多个对象(正如关于该主题的其他问题所建议的那样)。该代码适用于大多数对象,但不适用于某些对象。由于ArrayList.readObject()出现在堆栈跟踪中:是否与问题有关?如果是这样,什么?令我惊讶和沮丧的是,我在网上没有发现任何关于这个问题的事情......

我很欣赏有关从哪里开始的任何建议......!

1 个答案:

答案 0 :(得分:1)

我发现了问题:对象图中深处的一个对象实现了以下方法:

private void writeObject(java.io.ObjectOutputStream out) throws IOException

没有实际将其内部结构写入ObjectOutputStream并且没有抛出异常(该方法是为验证目的而实现的)。所以解决方案是:在实现此方法时,要么抛出某种Exception,要么将对象的内部写入ObjectOutputStream。最简单的是致电

out.defaultWriteObject();