From 2134d6bac7625f75de967a2e9d9e080a040437a9 Mon Sep 17 00:00:00 2001 From: Anthony Balkissoon Date: Tue, 28 Feb 2006 18:04:37 +0000 Subject: 2006-02-28 Anthony Balkissoon * java/math/BigInteger.java: Committed patch by Rafael: developer.classpath.org/pipermail/classpath-patches/ 2006-February/000473.html (signum): Return early 0 if words == null and ival == 0. (readObject): Handle special case of magnitude.length or signum being 0. (writeObject): If signum is zero return a zero-sized byte[]. --- java/math/BigInteger.java | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) (limited to 'java/math/BigInteger.java') diff --git a/java/math/BigInteger.java b/java/math/BigInteger.java index 5a336b872..86d6924af 100644 --- a/java/math/BigInteger.java +++ b/java/math/BigInteger.java @@ -356,9 +356,9 @@ public class BigInteger extends Number implements Comparable public int signum() { - int top = words == null ? ival : words[ival-1]; - if (top == 0 && words == null) + if (ival == 0 && words == null) return 0; + int top = words == null ? ival : words[ival-1]; return top < 0 ? -1 : 1; } @@ -2227,17 +2227,25 @@ public class BigInteger extends Number implements Comparable 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(); } } -- cgit v1.2.1