今天在試驗對象序列化,看到在類繼承了Serializable接口,還有兩個函數會在對象序列化及反序列化時默認自動執行,分別是writeObject和readObject。java
進行了簡單的試驗,發如今在程序執行過程當中出現了下述異常:eclipse
java.io.StreamCorruptedException: invalid type code: 00socket
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1520)ide
at java.io.ObjectInputStream.readEnum(ObjectInputStream.java:1725)函數
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1347)測試
at java.io.ObjectInputStream.skipCustomData(ObjectInputStream.java:1956)lua
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1918)spa
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1798)code
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1350)對象
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1990)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1915)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1798)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1350)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370)
at sm.test.TestSerializeDemo.test(TestSerializeDemo.java:59)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4Cla***unner.runChild(BlockJUnit4Cla***unner.java:68)
at org.junit.runners.BlockJUnit4Cla***unner.runChild(BlockJUnit4Cla***unner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
感受很是奇怪,上網去查了一些資料,大致都是和socket通訊相關的。可是個人測試里根本沒有用到socket,大致的思路是在序列化的時候會向流中寫一個標誌。
通過排查發現我在要進行序列化的類中,只實現了writeObject這一個方法,並無實現readObject方法。當我把readObject方法也寫上以後,再運行代碼發現沒有異常了。
總結:要想使用
private void writeObject(java.io.ObjectOutputStream out) throws IOException
private void readObject(java.io.ObjectInputStream in) throws IOException
這兩個自動執行的方法,要成對出現,要不就都沒有,要不就都有,不然會出現奇怪的異常。