即使文件中仍有数据,ObjectOutputStream.readInt()也会引发EOFException [英] ObjectOutputStream.readInt() throws EOFException even if there is still data in file

查看:129
本文介绍了即使文件中仍有数据,ObjectOutputStream.readInt()也会引发EOFException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的应用程序中存在一个问题,可以将其简化为以下代码:

We have a problem in our application which can be reduced to this code:

public static void main(String[] args) throws IOException, ClassNotFoundException {
        File tempFile = File.createTempFile("teststream", "");
        FileOutputStream fos = new FileOutputStream(tempFile);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeInt(1);
        oos.writeObject("foo");
        oos.writeInt(2);
        oos.flush();
        oos.close();
        FileInputStream fis = new FileInputStream(tempFile);
        ObjectInputStream ois = new ObjectInputStream(fis);
        int n1 = ois.readInt();
        Object o1 = ois.readObject();
        int n2 = ois.readInt();
    }

此代码有效,尽管如果您注释以下行:

This code works, although if you comment the following line:

Object o1 = ois.readObject();

以下行

int n2 = ois.readInt();

将抛出 EOFException 因为我写了一个对象和其中的一个整数 readInt的javadoc 并不表示此行为。我对此 EOFException 有点担心,因为我们想在代码中区分真实的文件中没有更多内容可以读取异常和错误的内容类型。

will throw an EOFException although there is data in my file since I wrote an object and an integer in it. The javadoc of readInt does not indicate this behavior. I'm a little bit worried about this EOFException, since we would like to distinguish in our code between a real there is nothing more to read in your file exception and a wrong type of content one.

该异常的堆栈跟踪为

Exception in thread "main" java.io.EOFException
    at java.io.DataInputStream.readInt(DataInputStream.java:392)
    at java.io.ObjectInputStream$BlockDataInputStream.readInt(ObjectInputStream.java:2793)
    at java.io.ObjectInputStream.readInt(ObjectInputStream.java:968)

这意味着 DataInputStream 中的以下代码引发异常:

Which means the following code in DataInputStream throws the exception:

 public final int readInt() throws IOException {
        int ch1 = in.read();
        int ch2 = in.read();
        int ch3 = in.read();
        int ch4 = in.read();
        if ((ch1 | ch2 | ch3 | ch4) < 0)
            throw new EOFException();

但是 in.read()不是应该在输入流中有数据时返回负数,所以我真的很感兴趣。

But in.read() is not supposed to return a negative number when there is data in the input stream so I'm really intrigued.

我的代码中是否可以做一些事情来防止这种情况发生

Is there something that can be done in my code to prevent this from happening (knowing that we might at some point call readInd where writeObject was used)?

我正在使用以下版本的Java:

I am using this version of java:

java version "1.7.0_07"
OpenJDK Runtime Environment (IcedTea7 2.3.2) (ArchLinux build 7.u7_2.3.2-2-x86_64)
OpenJDK 64-Bit Server VM (build 23.2-b09, mixed mode)


推荐答案

基础流以带有块标题的块形式编写。当您尝试 readInt 写入对象时,发现错误的块类型,并且 in.read()返回-1。

The underlying stream is written in blocks, with block headers. When you attempt to readInt when an object was written the wrong block type is found and the in.read() returns -1.

调用以下方法。

    /**
     * Attempts to read in the next block data header (if any).  If
     * canBlock is false and a full header cannot be read without possibly
     * blocking, returns HEADER_BLOCKED, else if the next element in the
     * stream is a block data header, returns the block data length
     * specified by the header, else returns -1.
     */
    private int readBlockHeader(boolean canBlock) throws IOException {
         // code deleted
                int tc = in.peek();
                switch (tc) {
                    case TC_BLOCKDATA:
         // code deleted
                    default:
                        if (tc >= 0 && (tc < TC_BASE || tc > TC_MAX)) {
                            throw new StreamCorruptedException(
                                String.format("invalid type code: %02X",
                                tc));
                        }
                        return -1;
    }

-1表示当 StreamCorruptedException 在这里可能是一个更好的选择。

The -1 means the end has been reached when a StreamCorruptedException might have been a better choice here.

这篇关于即使文件中仍有数据,ObjectOutputStream.readInt()也会引发EOFException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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