Java|序列化异常StreamCorruptedException的解决方法

niecprea

参考:http://tristan1.iteye.com/blo...

    public void testDeserialize() throws IOException, ClassNotFoundException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        BigInteger bi = new BigInteger("0");
        oos.writeObject(bi);
        byte[] str = baos.toByteArray();
        ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(new ByteArrayInputStream(str)));
        Object obj = ois.readObject();
        assertNotNull(obj);
        assertEquals(obj.getClass().getName(),"java.math.BigInteger");
        assertEquals(((BigInteger)obj).intValue(), 0);
    }

关键处理

byte[] str = baos.toByteArray();

阅读 2.1k

ThinkingPool
Y1RReE5qazFOVFl5

Hello world!

856 声望
21 粉丝
0 条评论

Hello world!

856 声望
21 粉丝
宣传栏