When reading GNU Classpath a serialized BigInteger with Sun's
VM/rt.jar, I was getting a StreamCorruptedException: signum-magnitude
mismatch.

Comparing serialized blobs I've found that for ZERO-valued
BigIntegers, Sun expect that magnitude comes out as a zero-element
byte array.

Below is a patch for those who care to fix it. I just don't have the
time to go over all the bureaucracy you've put to accept patches, so
I'm just publicly documenting the fix I've applied for my uses

Regards,

--
Rafael "Monoman" Teixeira
---------------------------------------
As I'm currently working a lot with Java and even fixing Java VMs
(JamVM/Kaffe) and GNU Classpath code, I think I may partly borrow the
title (Javaman) from my friend Bruno Souza and become the
MonoNJavaMan. Yeah, I may currently be crazier than usual...

Patch

--- classpath/java/math/BigInteger.java 2005-09-21 14:00:33.000000000 -0300
+++ classpath-nexus/java/math/BigInteger.java    2006-02-16
11:50:27.000000000 -0200
@@ -356,9 +356,9 @@

   public int signum()
   {
+       if (ival == 0 && words == null)
+         return 0;
     int top = words == null ? ival : words[ival-1];
-    if (top == 0 && words == null)
-      return 0;
     return top < 0 ? -1 : 1;
   }

@@ -2227,17 +2227,22 @@
     throws IOException, ClassNotFoundException
   {
     s.defaultReadObject();
-    words = byteArrayToIntArray(magnitude, signum < 0 ? -1 : 0);
-    BigInteger result = make(words, words.length);
-    this.ival = result.ival;
-    this.words = result.words;
+    if (magnitude.length == 0 || signum == 0) {
+      this.ival = 0;
+      this.words = null;
+    } else {
+      words = byteArrayToIntArray(magnitude, signum < 0 ? -1 : 0);
+      BigInteger result = make(words, words.length);
+      this.ival = result.ival;
+      this.words = result.words;
+    }
   }

   private void writeObject(ObjectOutputStream s)
     throws IOException, ClassNotFoundException
   {
     signum = signum();
-    magnitude = toByteArray();
+    magnitude = (signum == 0) ? new byte[0] : toByteArray();
     s.defaultWriteObject();
   }
 }

Reply via email to