简体   繁体   English

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

[英]`java.io.StreamCorruptedException: unexpected end of block data` when reading certain objects from file

I have some very basic serialization code: 我有一些非常基本的序列化代码:

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);
            }
        }
    }
}

And also some very deserialization code: 还有一些非常反序列化的代码:

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;
}

This code works fine for most objects. 此代码适用于大多数对象。 But for some objects I get the following exception: 但对于某些对象,我得到以下异常:

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]

I do not try to serialize more than one object per file (as suggested by other questions on the topic). 我不会尝试为每个文件序列化多个对象(正如关于该主题的其他问题所建议的那样)。 The code works for most objects, but not for some. 该代码适用于大多数对象,但不适用于某些对象。 Since ArrayList.readObject() appears in the stack trace: has that got something to do with the problem? 由于ArrayList.readObject()出现在堆栈跟踪中:是否与问题有关? If so, what? 如果是这样,什么? To my surprise and dismay I found nothing on the net concerning that problem... 令我惊讶和沮丧的是,我在网上没有发现任何问题......

I appreciate any pointers on where to go from here...! 我很欣赏从这里开始的任何指示...!

I found the problem: One of the objects deep down in the object graph implemented the following method: 我发现了问题:对象图中深处的一个对象实现了以下方法:

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

without actually writing its internals to the ObjectOutputStream and without throwing an exception (the method was implemented for validation purposes). 没有实际将其内部结构写入ObjectOutputStream并且没有抛出异常(该方法是为了验证目的而实现的)。 So the solution: when implementing this method, either throw an Exception of some sort or write the internals of the object to the ObjectOutputStream . 所以解决方案是:在实现此方法时,要么抛出某种Exception ,要么将对象的内部结构写入ObjectOutputStream Easiest is to call 最简单的就是打电话

out.defaultWriteObject();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.io.StreamCorruptedException:块数据的意外结束-在不同的JVM之间进行通信时 - java.io.StreamCorruptedException: unexpected end of block data - when communicating between defferent JVMs 带有序列化对象的java.io.StreamCorruptedException - java.io.StreamCorruptedException with serialized objects 我无法从文件中读取其他对象:java.io.streamcorruptedexception:无效类型代码:ac - I can't read the other objects from file: java.io.streamcorruptedexception: invalid type code: ac 附加数据中的java.io.StreamCorruptedException - java.io.StreamCorruptedException in appending data 线程“main”中的异常 javax.ejb.EJBException: java.io.StreamCorruptedException: 读取对象时发现意外字节 - Exception in thread "main" javax.ejb.EJBException: java.io.StreamCorruptedException: Unexpected byte found when reading an object java.io.StreamCorruptedException-Android - java.io.StreamCorruptedException - Android RandomFileAccess-java.io.StreamCorruptedException - RandomFileAccess - java.io.StreamCorruptedException java.io.StreamCorruptedException:意外的重置; 递归深度1 - java.io.StreamCorruptedException:unexpected reset; recursion depth 1 为什么将字节数组读取到对象会引发java.io.StreamCorruptedException? - Why reading byte array to an Object throws java.io.StreamCorruptedException? ObjectOutputStream和java.io.StreamCorruptedException - ObjectOutputStream and java.io.StreamCorruptedException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM