0

我在使用 ObjectInputStream 时遇到了困难。我写了一个简单的测试,它在读取时因 EOF 错误而失败。任何投入将不胜感激。

public class Test
{  
    @Test
    public void testObjectStreams( ) throws IOException, ClassNotFoundException
    {     
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);

        String stringTest = "TEST";
        oos.writeObject( stringTest );

        oos.close();
        baos.close();

        byte[] bytes = baos.toByteArray();
        String hexString = DatatypeConverter.printHexBinary( bytes);
        byte[] reconvertedBytes = DatatypeConverter.parseHexBinary(hexString);

        assertArrayEquals( bytes, reconvertedBytes );

        ByteArrayInputStream bais = new ByteArrayInputStream(reconvertedBytes);
        ObjectInputStream ois = new ObjectInputStream(bais);

        String readString = (String) ois.readObject();

        assertEquals( stringTest, readString);
    }
}
4

0 回答 0