即使文件中仍有数据,ObjectOutputStream.readInt() 也会抛出 EOFException

jol*_*ier 1 java exception

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

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();
    }
Run Code Online (Sandbox Code Playgroud)

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

Object o1 = ois.readObject();
Run Code Online (Sandbox Code Playgroud)

以下行

int n2 = ois.readInt();
Run Code Online (Sandbox Code Playgroud)

EOFException尽管我的文件中有数据,但会抛出一个,因为我在其中写入了一个对象和一个整数。readIntjavadoc没有表明这种行为。我对此有点担心EOFException,因为我们想在我们的代码中区分真实的文件异常和错误类型的内容异常。

异常的堆栈跟踪是

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)
Run Code Online (Sandbox Code Playgroud)

这意味着以下代码DataInputStream抛出异常:

 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();
Run Code Online (Sandbox Code Playgroud)

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

有什么事,可以在我的代码,以防止这种情况发生来完成(知道我们可能会在某个时候调用readInd其中writeObject使用)?

我正在使用这个版本的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)
Run Code Online (Sandbox Code Playgroud)

Pet*_*rey 5

底层流以块的形式写入,带有块头。当您尝试readInt写入对象时,会发现错误的块类型并in.read()返回 -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;
    }
Run Code Online (Sandbox Code Playgroud)

-1 表示当 aStreamCorruptedException在这里可能是更好的选择时已经到了尽头。