Java.io.StreamCorruptedException exceptions in the serialization of "Java" objects

Source: Internet
Author: User

Today in the experimental object serialization, see the class inherits the serializable interface, there are two of functions will be automatically executed when the object is serialized and deserialized, respectively, WriteObject and ReadObject.


A simple experiment was conducted to find that the following exception occurred during the execution of the program:

Java.io.StreamCorruptedException:invalid type code:00

At Java.io.ObjectInputStream.readClassDesc (objectinputstream.java:1520)

At Java.io.ObjectInputStream.readEnum (objectinputstream.java:1725)

At Java.io.ObjectInputStream.readObject0 (objectinputstream.java:1347)

At Java.io.ObjectInputStream.skipCustomData (objectinputstream.java:1956)

At Java.io.ObjectInputStream.readSerialData (objectinputstream.java:1918)

At Java.io.ObjectInputStream.readOrdinaryObject (objectinputstream.java:1798)

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.BlockJUnit4ClassRunner.runChild (blockjunit4classrunner.java:68)

At Org.junit.runners.BlockJUnit4ClassRunner.runChild (blockjunit4classrunner.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)


Feel very strange, the Internet to check some information, is generally related to the socket communication. But my test never used a socket, and the general idea was to write a flag to the stream when it was serialized.


After the troubleshooting found me in the class to be serialized, only implemented the WriteObject method, and did not implement the ReadObject method. When I put the ReadObject method on, I ran the code and found no exception.


Summary: To use

private void WriteObject (Java.io.ObjectOutputStream out) throws IOException

private void ReadObject (Java.io.ObjectInputStream in) throws IOException

These two automated methods, to appear in pairs, or none, or all, otherwise there will be strange anomalies.

Java.io.StreamCorruptedException exceptions in the serialization of "Java" objects

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.