How do I handle an Invalid Stream header Exception: 00000001?

て烟熏妆下的殇ゞ 提交于 2020-01-06 12:50:58

问题


java.io.StreamCorruptedException: invalid stream header: 00000001 Simple Project

I found this, and this seems to be a common problem. If you write to a directory with files and then manually deleted one later, you will end up getting this error.

java.io.StreamCorruptedException: invalid stream header: 00000001
     at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:806)
     at java.io.ObjectInputStream.<init>(ObjectInputStream.java:299)

The code:

private void deserialize(File input){
    // Let's deserialize an Object
    System.out.println("Here");
    try {
                FileInputStream fileIn = new FileInputStream(input); 
                ObjectInputStream in = new ObjectInputStream(fileIn);
                    //System.out.println("Deserialized Data: \n" + ((Song)in.readObject()));
                    database.add((Song)in.readObject());
                in.close();
                fileIn.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException ex) {
                    Logger.getLogger(Runner.class.getName()).log(Level.SEVERE, null, ex);
    }
}

private void open(){
    for(File fIn: f.listFiles()){
        deserialize(fIn);
    }
}

So whats going on is that there is a database of Objects called Song. Songs are serialized to a folder in my home library. I can see the files and they do get deserialized completely. The program has no problem running, it's just annoying to see the Invalid Stream Header Exception pop up and I don't want to have problems on later down the line.

How do I deal with this exception other than "Don't touch that directory after it's written to"?


回答1:


Basically, it means that the file you are trying to read does not contain a serialized object.

How do I deal with this exception other than "Don't touch that directory after it's written to"?

The other ways are:

  • Only deserialize files whose filename is one that your application would use.

  • Filter out specific filenames that are likely to be suspect; e.g. on Macs, the name of the hidden file that contains directory metadata.

  • Catch and ignore the StreamIgnoredException, though that is a bit risky. (The file could be a genuine serialized object that is corrupted for some other reason ... and you need to know about it.)


By the way, dealing with exceptions like you are doing is a bad idea. If the exception is genuinely expected, then you don't want to print a stacktrace. If it is unexpected, then it may be advisable to stop loading the directory, and allow the exception to propagate. At the very least, you need to report that the load had problems to the caller. (On the other hand, the user should not be confronted with stack traces if the program is ... erm ... functioning.)



来源:https://stackoverflow.com/questions/28912214/how-do-i-handle-an-invalid-stream-header-exception-00000001

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!