如何防止InputStream.readObject()抛出EOFException? [英] How to prevent InputStream.readObject() from throwing EOFException?

查看:208
本文介绍了如何防止InputStream.readObject()抛出EOFException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将序列化对象并将其另存为硬盘上的文件。当我阅读它时,在某些情况下,它会引发 EOFException类。经过几个小时的调试,我无法找到问题。



这是我的代码:

  public void serialize(MyClass myClass,String path){
FileOutputStream foStream = null;
ObjectOutputStream ooStream = null;
尝试{
文件文件=新建文件(路径);
if(!file.exists()){
file.createNewFile();
}
foStream = new FileOutputStream(file);
ooStream = new ObjectOutputStream(foStream);
ooStream.writeObject(myClass);
} catch(Throwable t){
log.error(t);
} finally {
if(ooStream!= null){
try {
ooStream.flush();
ooStream.close();
} catch(IOException e){
log.error(e);
}
}

}
}

获取对象

  public MyClass deSerialize(String path){
MyClass myClass = null;
FileInputStream fiStream = null;
ObjectInputStream oiStream = null;
String errorMessage =;
尝试{
文件文件=新建文件(路径);
if(!file.exists()){
return null;
}
fiStream = new FileInputStream(path);
oiStream = new ObjectInputStream(fiStream);
Object o = oiStream.readObject();
myClass =(MyClass)o;
} catch(Throwable t){
log.warn(t);
} finally {
if(oiStream!= null){
try {
oiStream.close();
} catch(IOException e){
log.error(e);
}
}
}
return myClass;
}

Stacktrace:


java.io.EOFException在
java.io.ObjectInputStream $ BlockDataInputStream.peekByte(ObjectInputStream.java:2498)
在java.io. ObjectInputStream.readObject0(ObjectInputStream.java:1273)
在java.io.ObjectInputStream.readObject(ObjectInputStream.java:348)
在java.util.LinkedList.readObject(LinkedList.java:776)at
sun.reflect.GeneratedMethodAccessor583.invoke(Unknown Source)at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method。 java $ 5 $)
java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:946)

java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1809)
at
java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1719)
在java.io.ObjectInputStream.readObje ct0(ObjectInputStream.java:1305)

java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1908)

java.io.ObjectInputStream.readSerialData(ObjectInputStream。 java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1305)



在java.io.ObjectInputStream.readObject(ObjectInputStream.java:348)


问题:
我的序列化对象现在已经被破坏,现在是垃圾吗?

因为这个对象负责渲染用户保存的UI。如果用户登录它应该呈现以前保存的UI状态。但是对于某些用户而言,该文件无法反序列化。

解决方案

EOFException 你试图读取文件的最后一个。通常情况下,除了尝试之外,您还没有任何方式知道更多的对象,因此您不应该将 EOFException 视为问题首先。如果在您认为您知道的情况下,文件中有更多的对象,例如当您将对象计数加入文件时,表示写入文件的代码或文件本身可能损坏的问题。另一个例子是零长度文件,不应该是零长度。无论问题是什么,阅读不了解,现在已经太迟了。


I serialize an object and save it as a file on my HDD. When I'm reading it, in only some occasions it throws EOFException. After couple of hours debugging I am not able to find a problem.

Here is my code:

   public void serialize(MyClass myClass,String path) {
        FileOutputStream foStream = null;
        ObjectOutputStream ooStream = null;
        try {
            File file = new File(path);
            if (!file.exists()) {
                file.createNewFile();
            }
            foStream = new FileOutputStream(file);
            ooStream = new ObjectOutputStream(foStream);
            ooStream.writeObject(myClass);
        } catch (Throwable t) {
            log.error(t);
        } finally {
            if (ooStream != null) {
                try {
                    ooStream.flush();
                    ooStream.close();
                } catch (IOException e) {
                    log.error(e);
                }
            }

        }
    }

For getting Object:

  public MyClass deSerialize(String path) {
        MyClass myClass=null;
        FileInputStream fiStream = null;
        ObjectInputStream oiStream = null;
        String errorMessage = "";
        try {
            File file = new File(path);
            if (!file.exists()) {
                return null;
            }
            fiStream = new FileInputStream(path);
            oiStream = new ObjectInputStream(fiStream);
            Object o = oiStream.readObject();
            myClass = (MyClass) o;
        } catch (Throwable t) {
            log.warn(t);
        } finally {
            if (oiStream != null) {
                try {
                    oiStream.close();
                } catch (IOException e) {
                    log.error(e);
                }
            }
        }
        return myClass;
    }

Stacktrace:

java.io.EOFException at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2498) at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1273) at java.io.ObjectInputStream.readObject(ObjectInputStream.java:348) at java.util.LinkedList.readObject(LinkedList.java:776) at sun.reflect.GeneratedMethodAccessor583.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:585) at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:946) at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1809) at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1719) at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1305) at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1908) at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1832) at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1719) at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1305) at java.io.ObjectInputStream.readObject(ObjectInputStream.java:348)

Question: My serialized object is now corrupted and then is it rubbish now?
Because this object is responsible for rendering the UI which saved by user. If User logs in it should render previously saved state of UI. However for some user the file cannot be deserialized.

解决方案

EOFException means you are trying to read past the end of the file. Normally you don't have any way of knowing whethere there are more objects to read, other than trying it, so you shouldn't regard EOFException as a problem in the first place. If it is thrown in a situation where you think you know there are more objects in the file, e.g. when you have prefixed an object count to the file, it indicates a problem with the code that wrote the file, or possible corruption of the file itself. Another example is a zero length file that shouldn't be zero length. Whatever the problem is, it can't be solved by the reading end, it is already too late.

这篇关于如何防止InputStream.readObject()抛出EOFException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆