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

Print this page




4351     }
4352 
4353     // Support for resetting final fields while deserializing
4354     private static class UnsafeHolder {
4355         private static final sun.misc.Unsafe unsafe;
4356         private static final long signumOffset;
4357         private static final long magOffset;
4358         static {
4359             try {
4360                 unsafe = sun.misc.Unsafe.getUnsafe();
4361                 signumOffset = unsafe.objectFieldOffset
4362                     (BigInteger.class.getDeclaredField("signum"));
4363                 magOffset = unsafe.objectFieldOffset
4364                     (BigInteger.class.getDeclaredField("mag"));
4365             } catch (Exception ex) {
4366                 throw new ExceptionInInitializerError(ex);
4367             }
4368         }
4369 
4370         static void putSign(BigInteger bi, int sign) {
4371             unsafe.putIntVolatile(bi, signumOffset, sign);
4372         }
4373 
4374         static void putMag(BigInteger bi, int[] magnitude) {
4375             unsafe.putObjectVolatile(bi, magOffset, magnitude);
4376         }
4377     }
4378 
4379     /**
4380      * Save the {@code BigInteger} instance to a stream.  The magnitude of a
4381      * {@code BigInteger} is serialized as a byte array for historical reasons.
4382      * To maintain compatibility with older implementations, the integers
4383      * -1, -1, -2, and -2 are written as the values of the obsolete fields
4384      * {@code bitCount}, {@code bitLength}, {@code lowestSetBit}, and
4385      * {@code firstNonzeroByteNum}, respectively.  These values are compatible
4386      * with older implementations, but will be ignored by current
4387      * implementations.
4388      */
4389     private void writeObject(ObjectOutputStream s) throws IOException {
4390         // set the values of the Serializable fields
4391         ObjectOutputStream.PutField fields = s.putFields();
4392         fields.put("signum", signum);
4393         fields.put("magnitude", magSerializedForm());
4394         // The values written for cached fields are compatible with older
4395         // versions, but are ignored in readObject so don't otherwise matter.




4351     }
4352 
4353     // Support for resetting final fields while deserializing
4354     private static class UnsafeHolder {
4355         private static final sun.misc.Unsafe unsafe;
4356         private static final long signumOffset;
4357         private static final long magOffset;
4358         static {
4359             try {
4360                 unsafe = sun.misc.Unsafe.getUnsafe();
4361                 signumOffset = unsafe.objectFieldOffset
4362                     (BigInteger.class.getDeclaredField("signum"));
4363                 magOffset = unsafe.objectFieldOffset
4364                     (BigInteger.class.getDeclaredField("mag"));
4365             } catch (Exception ex) {
4366                 throw new ExceptionInInitializerError(ex);
4367             }
4368         }
4369 
4370         static void putSign(BigInteger bi, int sign) {
4371             unsafe.putInt(bi, signumOffset, sign);
4372         }
4373 
4374         static void putMag(BigInteger bi, int[] magnitude) {
4375             unsafe.putObject(bi, magOffset, magnitude);
4376         }
4377     }
4378 
4379     /**
4380      * Save the {@code BigInteger} instance to a stream.  The magnitude of a
4381      * {@code BigInteger} is serialized as a byte array for historical reasons.
4382      * To maintain compatibility with older implementations, the integers
4383      * -1, -1, -2, and -2 are written as the values of the obsolete fields
4384      * {@code bitCount}, {@code bitLength}, {@code lowestSetBit}, and
4385      * {@code firstNonzeroByteNum}, respectively.  These values are compatible
4386      * with older implementations, but will be ignored by current
4387      * implementations.
4388      */
4389     private void writeObject(ObjectOutputStream s) throws IOException {
4390         // set the values of the Serializable fields
4391         ObjectOutputStream.PutField fields = s.putFields();
4392         fields.put("signum", signum);
4393         fields.put("magnitude", magSerializedForm());
4394         // The values written for cached fields are compatible with older
4395         // versions, but are ignored in readObject so don't otherwise matter.