Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4237853

java.math.BigInteger serialization is wrong in jdk 1.3

XMLWordPrintable

    • sparc
    • solaris_7



      Name: avC70361 Date: 05/13/99



        The java.math.BigInteger objects serialization is wrong under jdk1.3. In a case when BigInteger object
      represents 0 has been serialized, it can't be deserialized then because the "java.io.StreamCorruptedException:
      BigInteger: signum-magnitude mismatch" is thrown during deserialization. That is because BigInteger.
      writeObject() method use toByteArray() method to intialize serializable field 'magnitude', but this method
      returns not zero-length array for BigInteger representing 0. The 'magnitude' field should be zero-length array
      for zero BigInteger in order to be correcty read by readObject() of jdk 1.3 and earlier versions.
        Here is a test demonstrating the bug.
      ----------------BigIntegerTest.java--------------------
      import java.io.ByteArrayInputStream;
      import java.io.ByteArrayOutputStream;
      import java.io.ObjectInputStream;
      import java.io.ObjectOutputStream;
      import java.math.BigInteger;

      public class BigIntegerTest {
        public static void main(String[] args) {
          try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(baos);

            oos.writeObject(new BigInteger("0"));
            oos.flush();

            ObjectInputStream ois = new ObjectInputStream(
              new ByteArrayInputStream(baos.toByteArray())
            );

            ois.readObject();

            System.out.println("Passed.");
          } catch(Exception e) {
            System.out.println("Failed. Unexpected exception : " + e);
          }
        }
      }
      ----------------The test output---------------------
      > java BigIntegerTest
      Failed. Unexpected exception : java.io.StreamCorruptedException: BigInteger: signum-magnitude mismatch

      ======================================================================

            mmcclosksunw Michael Mccloskey (Inactive)
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: