解决序列化中的问题java.io.StreamCorruptedException: invalid stream header:EFBFBDEF
来源:互联网 发布:海岛奇兵火箭炮手数据 编辑:程序博客网 时间:2024/05/19 15:41
Java代码
- 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(); -
} - }
抛出错误
Java代码
- [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
修改成为
Java代码
- 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 theByteArrayOutputStream.toString() method, converts the string back into a bytearray using the String.getBytes() method, and then attempts to deserialize theobject from the byte array using a ByteArrayInputStream. This procedure willin most cases fail because of the transformations that take place withinByteArrayOutputStream.toString() and String.getBytes(): in order to convert thecontained sequence of bytes into a string, ByteArrayOutputStream.toString()decodes the bytes according to the default charset in effect; similarly, inorder 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 isgenerally not an identity-preserving operation. As the javadoc for theString(byte[], int, int) constructor (which is called byByteArrayOutputStream.toString()) states, "the behavior ... when the givenbytes are not valid in the default charset is unspecified". In the test caseprovided, the first two bytes of the serialization stream, 0xac and 0xed (seejava.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'mrunning). The two '?' characters are then mapped back to the byte sequence0x3f 0x3f in the reconstructed data stream, which do not constitute a validheader. The solution, from the perspective of the test case, is to useByteArrayOutputStream.toByteArray() instead of toString(), which will yield theraw byte sequence; this can then be fed directly to theByteArrayInputStream(byte[]) constructor.
注:在转换成字符串的时候如果使用Base64则可以避免该问题
0 0
- 解决序列化中的问题java.io.StreamCorruptedException: invalid stream header:EFBFBDEF
- java.io.StreamCorruptedException: invalid stream header: EFBFBDEF
- java.io.StreamCorruptedException: invalid stream header: ABACED00
- java.io.StreamCorruptedException: invalid stream header: 异常
- java.io.StreamCorruptedException: invalid stream header报错原因
- java.io.StreamCorruptedException: invalid stream header错误的解决方法
- java.io.StreamCorruptedException: invalid stream header报错原因
- 由于ObjectInputstream和ObjectOutputSteam引起的java.io.StreamCorruptedException: invalid stream header: 737
- Caused by: net.sf.ehcache.CacheException: java.io.StreamCorruptedException: invalid stream header: 0
- 存储两次序列化对象,读取报错问题 java.io.StreamCorruptedException: invalid type code: A
- java.io.StreamCorruptedException: invalid type code: AC
- java.io.StreamCorruptedException: invalid type code: AC
- Java 序列化和反序列化工具类并解决StreamCorruptedException问题
- java.io.StreamCorruptedException: invalid type code: AC异常和对象流锁机制问题
- java.io.StreamCorruptedException: invalid type code: AC异常
- .java.io.StreamCorruptedException: invalid type code: AC解决办法
- .java.io.StreamCorruptedException: invalid type code: AC解决办法
- java.io.StreamCorruptedException: invalid type code: AC解决办法
- stlport5.2.1编译
- ACCESSING THE CLOUD FROM COCOA TOUCH
- Consuming XML Web Services in iPhone Applications
- java_xml读取
- 用Preferences API 存储对象
- 解决序列化中的问题java.io.StreamCorruptedException: invalid stream header:EFBFBDEF
- vim常用命令之多行注释和多行删除
- NSArray and KVC (muti-value per key)
- Erlang 函数(Efficiency Guide)
- NSString copy or retain
- KVO
- Set to nil in viewDidUnload, but release in dealloc
- OC遍历属性
- Android代码优化——使用Android lint工具