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

Print this page




3723         } else if (val[1].scale < val[0].scale) {
3724             val[1] = val[1].setScale(val[0].scale, ROUND_UNNECESSARY);
3725         }
3726     }
3727 
3728     private static class UnsafeHolder {
3729         private static final sun.misc.Unsafe unsafe;
3730         private static final long intCompactOffset;
3731         private static final long intValOffset;
3732         static {
3733             try {
3734                 unsafe = sun.misc.Unsafe.getUnsafe();
3735                 intCompactOffset = unsafe.objectFieldOffset
3736                     (BigDecimal.class.getDeclaredField("intCompact"));
3737                 intValOffset = unsafe.objectFieldOffset
3738                     (BigDecimal.class.getDeclaredField("intVal"));
3739             } catch (Exception ex) {
3740                 throw new ExceptionInInitializerError(ex);
3741             }
3742         }
3743         static void setIntCompactVolatile(BigDecimal bd, long val) {
3744             unsafe.putLongVolatile(bd, intCompactOffset, val);
3745         }
3746 
3747         static void setIntValVolatile(BigDecimal bd, BigInteger val) {
3748             unsafe.putObjectVolatile(bd, intValOffset, val);
3749         }
3750     }
3751 
3752     /**
3753      * Reconstitute the {@code BigDecimal} instance from a stream (that is,
3754      * deserialize it).
3755      *
3756      * @param s the stream being read.
3757      */
3758     private void readObject(java.io.ObjectInputStream s)
3759         throws java.io.IOException, ClassNotFoundException {
3760         // Read in all fields
3761         s.defaultReadObject();
3762         // validate possibly bad fields
3763         if (intVal == null) {
3764             String message = "BigDecimal: null intVal in stream";
3765             throw new java.io.StreamCorruptedException(message);
3766         // [all values of scale are now allowed]
3767         }
3768         UnsafeHolder.setIntCompactVolatile(this, compactValFor(intVal));
3769     }
3770 
3771    /**
3772     * Serialize this {@code BigDecimal} to the stream in question
3773     *
3774     * @param s the stream to serialize to.
3775     */
3776    private void writeObject(java.io.ObjectOutputStream s)
3777        throws java.io.IOException {
3778        // Must inflate to maintain compatible serial form.
3779        if (this.intVal == null)
3780            UnsafeHolder.setIntValVolatile(this, BigInteger.valueOf(this.intCompact));
3781        // Could reset intVal back to null if it has to be set.
3782        s.defaultWriteObject();
3783    }
3784 
3785     /**
3786      * Returns the length of the absolute value of a {@code long}, in decimal
3787      * digits.
3788      *




3723         } else if (val[1].scale < val[0].scale) {
3724             val[1] = val[1].setScale(val[0].scale, ROUND_UNNECESSARY);
3725         }
3726     }
3727 
3728     private static class UnsafeHolder {
3729         private static final sun.misc.Unsafe unsafe;
3730         private static final long intCompactOffset;
3731         private static final long intValOffset;
3732         static {
3733             try {
3734                 unsafe = sun.misc.Unsafe.getUnsafe();
3735                 intCompactOffset = unsafe.objectFieldOffset
3736                     (BigDecimal.class.getDeclaredField("intCompact"));
3737                 intValOffset = unsafe.objectFieldOffset
3738                     (BigDecimal.class.getDeclaredField("intVal"));
3739             } catch (Exception ex) {
3740                 throw new ExceptionInInitializerError(ex);
3741             }
3742         }
3743         static void setIntCompact(BigDecimal bd, long val) {
3744             unsafe.putLong(bd, intCompactOffset, val);
3745         }
3746 
3747         static void setIntValVolatile(BigDecimal bd, BigInteger val) {
3748             unsafe.putObjectVolatile(bd, intValOffset, val);
3749         }
3750     }
3751 
3752     /**
3753      * Reconstitute the {@code BigDecimal} instance from a stream (that is,
3754      * deserialize it).
3755      *
3756      * @param s the stream being read.
3757      */
3758     private void readObject(java.io.ObjectInputStream s)
3759         throws java.io.IOException, ClassNotFoundException {
3760         // Read in all fields
3761         s.defaultReadObject();
3762         // validate possibly bad fields
3763         if (intVal == null) {
3764             String message = "BigDecimal: null intVal in stream";
3765             throw new java.io.StreamCorruptedException(message);
3766         // [all values of scale are now allowed]
3767         }
3768         UnsafeHolder.setIntCompact(this, compactValFor(intVal));
3769     }
3770 
3771    /**
3772     * Serialize this {@code BigDecimal} to the stream in question
3773     *
3774     * @param s the stream to serialize to.
3775     */
3776    private void writeObject(java.io.ObjectOutputStream s)
3777        throws java.io.IOException {
3778        // Must inflate to maintain compatible serial form.
3779        if (this.intVal == null)
3780            UnsafeHolder.setIntValVolatile(this, BigInteger.valueOf(this.intCompact));
3781        // Could reset intVal back to null if it has to be set.
3782        s.defaultWriteObject();
3783    }
3784 
3785     /**
3786      * Returns the length of the absolute value of a {@code long}, in decimal
3787      * digits.
3788      *