解决序列化中的问题 java.io.StreamCorruptedException: invalid stream header: EFBFBDEF

public class TestDeserialize extends TestCase {
    public void testDeserialize() throws IOException, ClassNotFoundException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        BigInteger bi = new BigInteger("0");
        oos.writeObject(bi);
        String str = baos.toString();
        System.out.println(str);
        ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(new ByteArrayInputStream(str.getBytes())));
        Object obj = ois.readObject();
    }
}

 

抛出错误

 

 [junit] ------------- ---------------- ---------------
    [junit] Testcase: testDeserialize(org.jboss.remoting.loading.TestDeserialize):	Caused an ERROR
    [junit] invalid stream header: EFBFBDEF
    [junit] java.io.StreamCorruptedException: invalid stream header: EFBFBDEF
    [junit] 	at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:783)
    [junit] 	at java.io.ObjectInputStream.<init>(ObjectInputStream.java:280)
    [junit] 	at org.jboss.remoting.loading.TestDeserialize.testDeserialize(TestDeserialize.java:20)
    [junit] 
    [junit] 
    [junit] Test org.jboss.remoting.loading.TestDeserialize FAILED

 

修改成为

 

    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);
    }

 

搞定,原因请见

 

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4968673

 

The provided test code serializes an object to a ByteArrayOutputStream,
converts the generated byte array into a string using the
ByteArrayOutputStream.toString() method, converts the string back into a byte
array using the String.getBytes() method, and then attempts to deserialize the
object from the byte array using a ByteArrayInputStream.  This procedure will
in most cases fail because of the transformations that take place within
ByteArrayOutputStream.toString() and String.getBytes(): in order to convert the
contained sequence of bytes into a string, ByteArrayOutputStream.toString()
decodes the bytes according to the default charset in effect; similarly, in
order to convert the string back into a sequence of bytes, String.getBytes()
encodes the characters according to the default charset.

Converting bytes into characters and back again according to a given charset is
generally not an identity-preserving operation.  As the javadoc for the
String(byte[], int, int) constructor (which is called by
ByteArrayOutputStream.toString()) states, "the behavior ... when the given
bytes are not valid in the default charset is unspecified".  In the test case
provided, the first two bytes of the serialization stream, 0xac and 0xed (see
java.io.ObjectStreamConstants.STREAM_MAGIC), both get mapped to the character
'?' since they are not valid in the default charset (ISO646-US in the JDK I'm
running).  The two '?' characters are then mapped back to the byte sequence
0x3f 0x3f in the reconstructed data stream, which do not constitute a valid
header. 

The solution, from the perspective of the test case, is to use
ByteArrayOutputStream.toByteArray() instead of toString(), which will yield the
raw byte sequence; this can then be fed directly to the
ByteArrayInputStream(byte[]) constructor.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值