ObjectInputStream在读取时抛出异常

时间:2012-11-08 19:54:31

标签: java objectinputstream

我在使用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);
    }
}

0 个答案:

没有答案