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

Print this page
rev 12972 : 8140606: Update library code to use internal Unsafe
Reviewed-by: duke


4509                 message = "BigInteger: Magnitude not present in stream";
4510             throw new java.io.StreamCorruptedException(message);
4511         }
4512 
4513         // Commit final fields via Unsafe
4514         UnsafeHolder.putSign(this, sign);
4515 
4516         // Calculate mag field from magnitude and discard magnitude
4517         UnsafeHolder.putMag(this, mag);
4518         if (mag.length >= MAX_MAG_LENGTH) {
4519             try {
4520                 checkRange();
4521             } catch (ArithmeticException e) {
4522                 throw new java.io.StreamCorruptedException("BigInteger: Out of the supported range");
4523             }
4524         }
4525     }
4526 
4527     // Support for resetting final fields while deserializing
4528     private static class UnsafeHolder {
4529         private static final sun.misc.Unsafe unsafe;
4530         private static final long signumOffset;
4531         private static final long magOffset;
4532         static {
4533             try {
4534                 unsafe = sun.misc.Unsafe.getUnsafe();
4535                 signumOffset = unsafe.objectFieldOffset
4536                     (BigInteger.class.getDeclaredField("signum"));
4537                 magOffset = unsafe.objectFieldOffset
4538                     (BigInteger.class.getDeclaredField("mag"));
4539             } catch (Exception ex) {
4540                 throw new ExceptionInInitializerError(ex);
4541             }
4542         }
4543 
4544         static void putSign(BigInteger bi, int sign) {
4545             unsafe.putInt(bi, signumOffset, sign);
4546         }
4547 
4548         static void putMag(BigInteger bi, int[] magnitude) {
4549             unsafe.putObject(bi, magOffset, magnitude);
4550         }
4551     }
4552 
4553     /**
4554      * Save the {@code BigInteger} instance to a stream.  The magnitude of a




4509                 message = "BigInteger: Magnitude not present in stream";
4510             throw new java.io.StreamCorruptedException(message);
4511         }
4512 
4513         // Commit final fields via Unsafe
4514         UnsafeHolder.putSign(this, sign);
4515 
4516         // Calculate mag field from magnitude and discard magnitude
4517         UnsafeHolder.putMag(this, mag);
4518         if (mag.length >= MAX_MAG_LENGTH) {
4519             try {
4520                 checkRange();
4521             } catch (ArithmeticException e) {
4522                 throw new java.io.StreamCorruptedException("BigInteger: Out of the supported range");
4523             }
4524         }
4525     }
4526 
4527     // Support for resetting final fields while deserializing
4528     private static class UnsafeHolder {
4529         private static final jdk.internal.misc.Unsafe unsafe;
4530         private static final long signumOffset;
4531         private static final long magOffset;
4532         static {
4533             try {
4534                 unsafe = jdk.internal.misc.Unsafe.getUnsafe();
4535                 signumOffset = unsafe.objectFieldOffset
4536                     (BigInteger.class.getDeclaredField("signum"));
4537                 magOffset = unsafe.objectFieldOffset
4538                     (BigInteger.class.getDeclaredField("mag"));
4539             } catch (Exception ex) {
4540                 throw new ExceptionInInitializerError(ex);
4541             }
4542         }
4543 
4544         static void putSign(BigInteger bi, int sign) {
4545             unsafe.putInt(bi, signumOffset, sign);
4546         }
4547 
4548         static void putMag(BigInteger bi, int[] magnitude) {
4549             unsafe.putObject(bi, magOffset, magnitude);
4550         }
4551     }
4552 
4553     /**
4554      * Save the {@code BigInteger} instance to a stream.  The magnitude of a