src/share/classes/java/math/BigInteger.java

Print this page
rev 9307 : 8035279: Clean up internal deprecations in BigInteger
Summary: Modify lazily intialized values to use different marker values.
Reviewed-by: TBD


 130      * @serial
 131      */
 132     final int signum;
 133 
 134     /**
 135      * The magnitude of this BigInteger, in <i>big-endian</i> order: the
 136      * zeroth element of this array is the most-significant int of the
 137      * magnitude.  The magnitude must be "minimal" in that the most-significant
 138      * int ({@code mag[0]}) must be non-zero.  This is necessary to
 139      * ensure that there is exactly one representation for each BigInteger
 140      * value.  Note that this implies that the BigInteger zero has a
 141      * zero-length mag array.
 142      */
 143     final int[] mag;
 144 
 145     // These "redundant fields" are initialized with recognizable nonsense
 146     // values, and cached the first time they are needed (or never, if they
 147     // aren't needed).
 148 
 149      /**
 150      * One plus the bitCount of this BigInteger. Zeros means unitialized.

 151      *
 152      * @serial
 153      * @see #bitCount
 154      * @deprecated Deprecated since logical value is offset from stored
 155      * value and correction factor is applied in accessor method.
 156      */
 157     @Deprecated
 158     private int bitCount;
 159 
 160     /**
 161      * One plus the bitLength of this BigInteger. Zeros means unitialized.
 162      * (either value is acceptable).
 163      *
 164      * @serial
 165      * @see #bitLength()
 166      * @deprecated Deprecated since logical value is offset from stored
 167      * value and correction factor is applied in accessor method.
 168      */
 169     @Deprecated
 170     private int bitLength;
 171 
 172     /**
 173      * Two plus the lowest set bit of this BigInteger, as returned by
 174      * getLowestSetBit().

 175      *
 176      * @serial
 177      * @see #getLowestSetBit
 178      * @deprecated Deprecated since logical value is offset from stored
 179      * value and correction factor is applied in accessor method.
 180      */
 181     @Deprecated
 182     private int lowestSetBit;
 183 
 184     /**
 185      * Two plus the index of the lowest-order int in the magnitude of this
 186      * BigInteger that contains a nonzero int, or -2 (either value is acceptable).

 187      * The least significant int has int-number 0, the next int in order of
 188      * increasing significance has int-number 1, and so forth.
 189      * @deprecated Deprecated since logical value is offset from stored
 190      * value and correction factor is applied in accessor method.
 191      */
 192     @Deprecated
 193     private int firstNonzeroIntNum;
 194 
 195     /**
 196      * This mask is used to obtain the value of an int as if it were unsigned.
 197      */
 198     final static long LONG_MASK = 0xffffffffL;
 199 
 200     /**
 201      * This constant limits {@code mag.length} of BigIntegers to the supported
 202      * range.
 203      */
 204     private static final int MAX_MAG_LENGTH = Integer.MAX_VALUE / Integer.SIZE + 1; // (1 << 26)
 205 
 206     /**
 207      * Bit lengths larger than this constant can cause overflow in searchLen
 208      * calculation and in BitSieve.singleSearch method.
 209      */
 210     private static final  int PRIME_SEARCH_BIT_LENGTH_LIMIT = 500000000;
 211 
 212     /**
 213      * The threshold value for using Karatsuba multiplication.  If the number


3223         int intNum = n >>> 5;
3224         int[] result = new int[Math.max(intLength(), intNum+2)];
3225 
3226         for (int i=0; i < result.length; i++)
3227             result[result.length-i-1] = getInt(i);
3228 
3229         result[result.length-intNum-1] ^= (1 << (n & 31));
3230 
3231         return valueOf(result);
3232     }
3233 
3234     /**
3235      * Returns the index of the rightmost (lowest-order) one bit in this
3236      * BigInteger (the number of zero bits to the right of the rightmost
3237      * one bit).  Returns -1 if this BigInteger contains no one bits.
3238      * (Computes {@code (this == 0? -1 : log2(this & -this))}.)
3239      *
3240      * @return index of the rightmost one bit in this BigInteger.
3241      */
3242     public int getLowestSetBit() {
3243         @SuppressWarnings("deprecation") int lsb = lowestSetBit - 2;
3244         if (lsb == -2) {  // lowestSetBit not initialized yet
3245             lsb = 0;
3246             if (signum == 0) {
3247                 lsb -= 1;
3248             } else {
3249                 // Search for lowest order nonzero int
3250                 int i,b;
3251                 for (i=0; (b = getInt(i)) == 0; i++)
3252                     ;
3253                 lsb += (i << 5) + Integer.numberOfTrailingZeros(b);
3254             }
3255             lowestSetBit = lsb + 2;
3256         }
3257         return lsb;
3258     }
3259 
3260 
3261     // Miscellaneous Bit Operations
3262 
3263     /**
3264      * Returns the number of bits in the minimal two's-complement
3265      * representation of this BigInteger, <i>excluding</i> a sign bit.
3266      * For positive BigIntegers, this is equivalent to the number of bits in
3267      * the ordinary binary representation.  (Computes
3268      * {@code (ceil(log2(this < 0 ? -this : this+1)))}.)
3269      *
3270      * @return number of bits in the minimal two's-complement
3271      *         representation of this BigInteger, <i>excluding</i> a sign bit.
3272      */
3273     public int bitLength() {
3274         @SuppressWarnings("deprecation") int n = bitLength - 1;
3275         if (n == -1) { // bitLength not initialized yet
3276             int[] m = mag;
3277             int len = m.length;
3278             if (len == 0) {
3279                 n = 0; // offset by one to initialize
3280             }  else {
3281                 // Calculate the bit length of the magnitude
3282                 int magBitLength = ((len - 1) << 5) + bitLengthForInt(mag[0]);
3283                  if (signum < 0) {
3284                      // Check if magnitude is a power of two
3285                      boolean pow2 = (Integer.bitCount(mag[0]) == 1);
3286                      for (int i=1; i< len && pow2; i++)
3287                          pow2 = (mag[i] == 0);

3288 
3289                      n = (pow2 ? magBitLength -1 : magBitLength);
3290                  } else {
3291                      n = magBitLength;
3292                  }
3293             }
3294             bitLength = n + 1;
3295         }
3296         return n;
3297     }
3298 
3299     /**
3300      * Returns the number of bits in the two's complement representation
3301      * of this BigInteger that differ from its sign bit.  This method is
3302      * useful when implementing bit-vector style sets atop BigIntegers.
3303      *
3304      * @return number of bits in the two's complement representation
3305      *         of this BigInteger that differ from its sign bit.
3306      */
3307     public int bitCount() {
3308         @SuppressWarnings("deprecation") int bc = bitCount - 1;
3309         if (bc == -1) {  // bitCount not initialized yet
3310             bc = 0;      // offset by one to initialize
3311             // Count the bits in the magnitude
3312             for (int i=0; i < mag.length; i++)
3313                 bc += Integer.bitCount(mag[i]);
3314             if (signum < 0) {
3315                 // Count the trailing zeros in the magnitude
3316                 int magTrailingZeroCount = 0, j;
3317                 for (j=mag.length-1; mag[j] == 0; j--)
3318                     magTrailingZeroCount += 32;
3319                 magTrailingZeroCount += Integer.numberOfTrailingZeros(mag[j]);
3320                 bc += magTrailingZeroCount - 1;
3321             }
3322             bitCount = bc + 1;
3323         }
3324         return bc;
3325     }
3326 
3327     // Primality Testing
3328 
3329     /**
3330      * Returns {@code true} if this BigInteger is probably prime,
3331      * {@code false} if it's definitely composite.  If
3332      * {@code certainty} is &le; 0, {@code true} is
3333      * returned.
3334      *
3335      * @param  certainty a measure of the uncertainty that the caller is
3336      *         willing to tolerate: if the call returns {@code true}
3337      *         the probability that this BigInteger is prime exceeds
3338      *         (1 - 1/2<sup>{@code certainty}</sup>).  The execution time of
3339      *         this method is proportional to the value of this parameter.
3340      * @return {@code true} if this BigInteger is probably prime,
3341      *         {@code false} if it's definitely composite.
3342      */
3343     public boolean isProbablePrime(int certainty) {
3344         if (certainty <= 0)


4173      * sign ints).
4174      */
4175     private int getInt(int n) {
4176         if (n < 0)
4177             return 0;
4178         if (n >= mag.length)
4179             return signInt();
4180 
4181         int magInt = mag[mag.length-n-1];
4182 
4183         return (signum >= 0 ? magInt :
4184                 (n <= firstNonzeroIntNum() ? -magInt : ~magInt));
4185     }
4186 
4187     /**
4188      * Returns the index of the int that contains the first nonzero int in the
4189      * little-endian binary representation of the magnitude (int 0 is the
4190      * least significant). If the magnitude is zero, return value is undefined.
4191      */
4192     private int firstNonzeroIntNum() {
4193         int fn = firstNonzeroIntNum - 2;
4194         if (fn == -2) { // firstNonzeroIntNum not initialized yet
4195             fn = 0;
4196 
4197             // Search for the first nonzero int
4198             int i;
4199             int mlen = mag.length;
4200             for (i = mlen - 1; i >= 0 && mag[i] == 0; i--)
4201                 ;
4202             fn = mlen - i - 1;
4203             firstNonzeroIntNum = fn + 2; // offset by two to initialize
4204         }
4205         return fn;
4206     }
4207 
4208     /** use serialVersionUID from JDK 1.1. for interoperability */
4209     private static final long serialVersionUID = -8287574255936472291L;
4210 
4211     /**
4212      * Serializable fields for BigInteger.
4213      *
4214      * @serialField signum  int
4215      *              signum of this BigInteger.
4216      * @serialField magnitude int[]
4217      *              magnitude array of this BigInteger.
4218      * @serialField bitCount  int
4219      *              number of bits in this BigInteger
4220      * @serialField bitLength int
4221      *              the number of bits in the minimal two's-complement
4222      *              representation of this BigInteger
4223      * @serialField lowestSetBit int
4224      *              lowest set bit in the twos complement representation
4225      */
4226     private static final ObjectStreamField[] serialPersistentFields = {
4227         new ObjectStreamField("signum", Integer.TYPE),
4228         new ObjectStreamField("magnitude", byte[].class),
4229         new ObjectStreamField("bitCount", Integer.TYPE),
4230         new ObjectStreamField("bitLength", Integer.TYPE),
4231         new ObjectStreamField("firstNonzeroByteNum", Integer.TYPE),
4232         new ObjectStreamField("lowestSetBit", Integer.TYPE)
4233         };
4234 
4235     /**
4236      * Reconstitute the {@code BigInteger} instance from a stream (that is,
4237      * deserialize it). The magnitude is read in as an array of bytes
4238      * for historical reasons, but it is converted to an array of ints
4239      * and the byte array is discarded.
4240      * Note:
4241      * The current convention is to initialize the cache fields, bitCount,
4242      * bitLength and lowestSetBit, to 0 rather than some other marker value.
4243      * Therefore, no explicit action to set these fields needs to be taken in
4244      * readObject because those fields already have a 0 value be default since
4245      * defaultReadObject is not being used.
4246      */
4247     private void readObject(java.io.ObjectInputStream s)
4248         throws java.io.IOException, ClassNotFoundException {
4249         /*
4250          * In order to maintain compatibility with previous serialized forms,
4251          * the magnitude of a BigInteger is serialized as an array of bytes.
4252          * The magnitude field is used as a temporary store for the byte array
4253          * that is deserialized. The cached computation fields should be
4254          * transient but are serialized for compatibility reasons.
4255          */
4256 
4257         // prepare to read the alternate persistent fields
4258         ObjectInputStream.GetField fields = s.readFields();
4259 
4260         // Read the alternate persistent fields that we care about
4261         int sign = fields.get("signum", -2);
4262         byte[] magnitude = (byte[])fields.get("magnitude", null);
4263 
4264         // Validate signum
4265         if (sign < -1 || sign > 1) {


4271         int[] mag = stripLeadingZeroBytes(magnitude);
4272         if ((mag.length == 0) != (sign == 0)) {
4273             String message = "BigInteger: signum-magnitude mismatch";
4274             if (fields.defaulted("magnitude"))
4275                 message = "BigInteger: Magnitude not present in stream";
4276             throw new java.io.StreamCorruptedException(message);
4277         }
4278 
4279         // Commit final fields via Unsafe
4280         UnsafeHolder.putSign(this, sign);
4281 
4282         // Calculate mag field from magnitude and discard magnitude
4283         UnsafeHolder.putMag(this, mag);
4284         if (mag.length >= MAX_MAG_LENGTH) {
4285             try {
4286                 checkRange();
4287             } catch (ArithmeticException e) {
4288                 throw new java.io.StreamCorruptedException("BigInteger: Out of the supported range");
4289             }
4290         }






4291     }
4292 
4293     // Support for resetting final fields while deserializing
4294     private static class UnsafeHolder {
4295         private static final sun.misc.Unsafe unsafe;
4296         private static final long signumOffset;
4297         private static final long magOffset;
4298         static {
4299             try {
4300                 unsafe = sun.misc.Unsafe.getUnsafe();
4301                 signumOffset = unsafe.objectFieldOffset
4302                     (BigInteger.class.getDeclaredField("signum"));
4303                 magOffset = unsafe.objectFieldOffset
4304                     (BigInteger.class.getDeclaredField("mag"));
4305             } catch (Exception ex) {
4306                 throw new ExceptionInInitializerError(ex);
4307             }
4308         }
4309 
4310         static void putSign(BigInteger bi, int sign) {




 130      * @serial
 131      */
 132     final int signum;
 133 
 134     /**
 135      * The magnitude of this BigInteger, in <i>big-endian</i> order: the
 136      * zeroth element of this array is the most-significant int of the
 137      * magnitude.  The magnitude must be "minimal" in that the most-significant
 138      * int ({@code mag[0]}) must be non-zero.  This is necessary to
 139      * ensure that there is exactly one representation for each BigInteger
 140      * value.  Note that this implies that the BigInteger zero has a
 141      * zero-length mag array.
 142      */
 143     final int[] mag;
 144 
 145     // These "redundant fields" are initialized with recognizable nonsense
 146     // values, and cached the first time they are needed (or never, if they
 147     // aren't needed).
 148 
 149     /**
 150      * The bitCount of this BigInteger. {@code Integer.MIN_VALUE} means
 151      * uninitialized.
 152      *
 153      * @serial
 154      * @see #bitCount


 155      */
 156     private volatile int bitCount = Integer.MIN_VALUE;

 157 
 158     /**
 159      * The bitLength of this BigInteger. {@code Integer.MIN_VALUE} means
 160      * uninitialized.
 161      *
 162      * @serial
 163      * @see #bitLength()


 164      */
 165     private volatile int bitLength = Integer.MIN_VALUE;

 166 
 167     /**
 168      * The lowest set bit of this BigInteger, as returned by
 169      * getLowestSetBit(). {@code Integer.MIN_VALUE} means uninitialized,
 170      * and {@code -1} that this BigInteger contains no one bits.
 171      *
 172      * @serial
 173      * @see #getLowestSetBit


 174      */
 175     private volatile int lowestSetBit = Integer.MIN_VALUE;

 176 
 177     /**
 178      * The index of the lowest-order int in the magnitude of this
 179      * BigInteger that contains a nonzero int, or {@code Integer.MIN_VALUE} to
 180      * indicate uninitialized (either value is acceptable).
 181      * The least significant int has int-number 0, the next int in order of
 182      * increasing significance has int-number 1, and so forth.


 183      */
 184     private volatile int firstNonzeroIntNum = Integer.MIN_VALUE;

 185 
 186     /**
 187      * This mask is used to obtain the value of an int as if it were unsigned.
 188      */
 189     final static long LONG_MASK = 0xffffffffL;
 190 
 191     /**
 192      * This constant limits {@code mag.length} of BigIntegers to the supported
 193      * range.
 194      */
 195     private static final int MAX_MAG_LENGTH = Integer.MAX_VALUE / Integer.SIZE + 1; // (1 << 26)
 196 
 197     /**
 198      * Bit lengths larger than this constant can cause overflow in searchLen
 199      * calculation and in BitSieve.singleSearch method.
 200      */
 201     private static final  int PRIME_SEARCH_BIT_LENGTH_LIMIT = 500000000;
 202 
 203     /**
 204      * The threshold value for using Karatsuba multiplication.  If the number


3214         int intNum = n >>> 5;
3215         int[] result = new int[Math.max(intLength(), intNum+2)];
3216 
3217         for (int i=0; i < result.length; i++)
3218             result[result.length-i-1] = getInt(i);
3219 
3220         result[result.length-intNum-1] ^= (1 << (n & 31));
3221 
3222         return valueOf(result);
3223     }
3224 
3225     /**
3226      * Returns the index of the rightmost (lowest-order) one bit in this
3227      * BigInteger (the number of zero bits to the right of the rightmost
3228      * one bit).  Returns -1 if this BigInteger contains no one bits.
3229      * (Computes {@code (this == 0? -1 : log2(this & -this))}.)
3230      *
3231      * @return index of the rightmost one bit in this BigInteger.
3232      */
3233     public int getLowestSetBit() {
3234         // This computation has a known, acceptable non-critical race condition.
3235         if (lowestSetBit == Integer.MIN_VALUE) {  // lowestSetBit not initialized yet
3236             int lsb = 0;
3237             if (signum == 0) {
3238                 lsb -= 1;
3239             } else {
3240                 // Search for lowest order nonzero int
3241                 int i,b;
3242                 for (i=0; (b = getInt(i)) == 0; i++)
3243                     ;
3244                 lsb += (i << 5) + Integer.numberOfTrailingZeros(b);
3245             }
3246             lowestSetBit = lsb;
3247         }
3248         return lowestSetBit;
3249     }
3250 
3251 
3252     // Miscellaneous Bit Operations
3253 
3254     /**
3255      * Returns the number of bits in the minimal two's-complement
3256      * representation of this BigInteger, <i>excluding</i> a sign bit.
3257      * For positive BigIntegers, this is equivalent to the number of bits in
3258      * the ordinary binary representation.  (Computes
3259      * {@code (ceil(log2(this < 0 ? -this : this+1)))}.)
3260      *
3261      * @return number of bits in the minimal two's-complement
3262      *         representation of this BigInteger, <i>excluding</i> a sign bit.
3263      */
3264     public int bitLength() {
3265         // This computation has a known, acceptable non-critical race condition.
3266         if (bitLength == Integer.MIN_VALUE) { // bitLength not initialized yet
3267             int[] m = mag;
3268             int len = m.length;
3269             int n = 0;
3270             if (len != 0) {

3271                 // Calculate the bit length of the magnitude
3272                 int magBitLength = ((len - 1) << 5) + bitLengthForInt(mag[0]);
3273                 if (signum < 0) {
3274                     // Check if magnitude is a power of two
3275                     boolean pow2 = (Integer.bitCount(mag[0]) == 1);
3276                     for (int i = 1; i < len && pow2; i++) {
3277                         pow2 = (mag[i] == 0);
3278                     }
3279 
3280                     n = (pow2 ? magBitLength - 1 : magBitLength);
3281                 } else {
3282                     n = magBitLength;
3283                 }
3284             }
3285             bitLength = n;
3286         }
3287         return bitLength;
3288     }
3289 
3290     /**
3291      * Returns the number of bits in the two's complement representation
3292      * of this BigInteger that differ from its sign bit.  This method is
3293      * useful when implementing bit-vector style sets atop BigIntegers.
3294      *
3295      * @return number of bits in the two's complement representation
3296      *         of this BigInteger that differ from its sign bit.
3297      */
3298     public int bitCount() {
3299         // This computation has a known, acceptable non-critical race condition.
3300         if (bitCount == Integer.MIN_VALUE) {  // bitCount not initialized yet
3301             int bc = 0;
3302             // Count the bits in the magnitude
3303             for (int i=0; i < mag.length; i++)
3304                 bc += Integer.bitCount(mag[i]);
3305             if (signum < 0) {
3306                 // Count the trailing zeros in the magnitude
3307                 int magTrailingZeroCount = 0, j;
3308                 for (j=mag.length-1; mag[j] == 0; j--)
3309                     magTrailingZeroCount += 32;
3310                 magTrailingZeroCount += Integer.numberOfTrailingZeros(mag[j]);
3311                 bc += magTrailingZeroCount - 1;
3312             }
3313             bitCount = bc;
3314         }
3315         return bitCount;
3316     }
3317 
3318     // Primality Testing
3319 
3320     /**
3321      * Returns {@code true} if this BigInteger is probably prime,
3322      * {@code false} if it's definitely composite.  If
3323      * {@code certainty} is &le; 0, {@code true} is
3324      * returned.
3325      *
3326      * @param  certainty a measure of the uncertainty that the caller is
3327      *         willing to tolerate: if the call returns {@code true}
3328      *         the probability that this BigInteger is prime exceeds
3329      *         (1 - 1/2<sup>{@code certainty}</sup>).  The execution time of
3330      *         this method is proportional to the value of this parameter.
3331      * @return {@code true} if this BigInteger is probably prime,
3332      *         {@code false} if it's definitely composite.
3333      */
3334     public boolean isProbablePrime(int certainty) {
3335         if (certainty <= 0)


4164      * sign ints).
4165      */
4166     private int getInt(int n) {
4167         if (n < 0)
4168             return 0;
4169         if (n >= mag.length)
4170             return signInt();
4171 
4172         int magInt = mag[mag.length-n-1];
4173 
4174         return (signum >= 0 ? magInt :
4175                 (n <= firstNonzeroIntNum() ? -magInt : ~magInt));
4176     }
4177 
4178     /**
4179      * Returns the index of the int that contains the first nonzero int in the
4180      * little-endian binary representation of the magnitude (int 0 is the
4181      * least significant). If the magnitude is zero, return value is undefined.
4182      */
4183     private int firstNonzeroIntNum() {
4184         // This computation has a known, acceptable non-critical race condition.
4185         if (firstNonzeroIntNum == Integer.MIN_VALUE) { // firstNonzeroIntNum not initialized yet
4186             int fn = 0;
4187 
4188             // Search for the first nonzero int
4189             int i;
4190             int mlen = mag.length;
4191             for (i = mlen - 1; i >= 0 && mag[i] == 0; i--)
4192                 ;
4193             fn = mlen - i - 1;
4194             firstNonzeroIntNum = fn;
4195         }
4196         return firstNonzeroIntNum;
4197     }
4198 
4199     /** use serialVersionUID from JDK 1.1. for interoperability */
4200     private static final long serialVersionUID = -8287574255936472291L;
4201 
4202     /**
4203      * Serializable fields for BigInteger.
4204      *
4205      * @serialField signum  int
4206      *              signum of this BigInteger.
4207      * @serialField magnitude int[]
4208      *              magnitude array of this BigInteger.
4209      * @serialField bitCount  int
4210      *              number of bits in this BigInteger
4211      * @serialField bitLength int
4212      *              the number of bits in the minimal two's-complement
4213      *              representation of this BigInteger
4214      * @serialField lowestSetBit int
4215      *              lowest set bit in the twos complement representation
4216      */
4217     private static final ObjectStreamField[] serialPersistentFields = {
4218         new ObjectStreamField("signum", Integer.TYPE),
4219         new ObjectStreamField("magnitude", byte[].class),
4220         new ObjectStreamField("bitCount", Integer.TYPE),
4221         new ObjectStreamField("bitLength", Integer.TYPE),
4222         new ObjectStreamField("firstNonzeroByteNum", Integer.TYPE),
4223         new ObjectStreamField("lowestSetBit", Integer.TYPE)
4224         };
4225 
4226     /**
4227      * Reconstitute the {@code BigInteger} instance from a stream (that is,
4228      * deserialize it). The magnitude is read in as an array of bytes
4229      * for historical reasons, but it is converted to an array of ints
4230      * and the byte array is discarded.
4231      * Note:
4232      * The current convention is explicitly to initialize the cache fields
4233      * bitCount, bitLength, firstNonzeroIntNum, and lowestSetBit to
4234      * Integer.MIN_VALUE. This serves to indicate that the actual values need
4235      * to be calculated when accessed.

4236      */
4237     private void readObject(java.io.ObjectInputStream s)
4238         throws java.io.IOException, ClassNotFoundException {
4239         /*
4240          * In order to maintain compatibility with previous serialized forms,
4241          * the magnitude of a BigInteger is serialized as an array of bytes.
4242          * The magnitude field is used as a temporary store for the byte array
4243          * that is deserialized. The cached computation fields should be
4244          * transient but are serialized for compatibility reasons.
4245          */
4246 
4247         // prepare to read the alternate persistent fields
4248         ObjectInputStream.GetField fields = s.readFields();
4249 
4250         // Read the alternate persistent fields that we care about
4251         int sign = fields.get("signum", -2);
4252         byte[] magnitude = (byte[])fields.get("magnitude", null);
4253 
4254         // Validate signum
4255         if (sign < -1 || sign > 1) {


4261         int[] mag = stripLeadingZeroBytes(magnitude);
4262         if ((mag.length == 0) != (sign == 0)) {
4263             String message = "BigInteger: signum-magnitude mismatch";
4264             if (fields.defaulted("magnitude"))
4265                 message = "BigInteger: Magnitude not present in stream";
4266             throw new java.io.StreamCorruptedException(message);
4267         }
4268 
4269         // Commit final fields via Unsafe
4270         UnsafeHolder.putSign(this, sign);
4271 
4272         // Calculate mag field from magnitude and discard magnitude
4273         UnsafeHolder.putMag(this, mag);
4274         if (mag.length >= MAX_MAG_LENGTH) {
4275             try {
4276                 checkRange();
4277             } catch (ArithmeticException e) {
4278                 throw new java.io.StreamCorruptedException("BigInteger: Out of the supported range");
4279             }
4280         }
4281 
4282         // Initialize cached fields to indicate not computed yet.
4283         bitCount = Integer.MIN_VALUE;
4284         bitLength = Integer.MIN_VALUE;
4285         lowestSetBit = Integer.MIN_VALUE;
4286         firstNonzeroIntNum = Integer.MIN_VALUE;
4287     }
4288 
4289     // Support for resetting final fields while deserializing
4290     private static class UnsafeHolder {
4291         private static final sun.misc.Unsafe unsafe;
4292         private static final long signumOffset;
4293         private static final long magOffset;
4294         static {
4295             try {
4296                 unsafe = sun.misc.Unsafe.getUnsafe();
4297                 signumOffset = unsafe.objectFieldOffset
4298                     (BigInteger.class.getDeclaredField("signum"));
4299                 magOffset = unsafe.objectFieldOffset
4300                     (BigInteger.class.getDeclaredField("mag"));
4301             } catch (Exception ex) {
4302                 throw new ExceptionInInitializerError(ex);
4303             }
4304         }
4305 
4306         static void putSign(BigInteger bi, int sign) {