src/share/classes/java/math/BigDecimal.java
Print this page
*** 3714,3723 ****
--- 3714,3724 ----
} else if (val[1].scale < val[0].scale) {
val[1] = val[1].setScale(val[0].scale, ROUND_UNNECESSARY);
}
}
+ private static class UnsafeHolder {
private static final sun.misc.Unsafe unsafe = sun.misc.Unsafe.getUnsafe();
private static final long intCompactOffset;
private static final long intValOffset;
static {
try {
*** 3727,3743 ****
(BigDecimal.class.getDeclaredField("intVal"));
} catch (Exception ex) {
throw new Error(ex);
}
}
!
! private void setIntCompactVolatile(long val) {
! unsafe.putLongVolatile(this, intCompactOffset, val);
}
! private void setIntValVolatile(BigInteger val) {
! unsafe.putObjectVolatile(this, intValOffset, val);
}
/**
* Reconstitute the {@code BigDecimal} instance from a stream (that is,
* deserialize it).
--- 3728,3744 ----
(BigDecimal.class.getDeclaredField("intVal"));
} catch (Exception ex) {
throw new Error(ex);
}
}
! private static void setIntCompactVolatile(BigDecimal bd, long val) {
! unsafe.putLongVolatile(bd, intCompactOffset, val);
}
! private static void setIntValVolatile(BigDecimal bd, BigInteger val) {
! unsafe.putObjectVolatile(bd, intValOffset, val);
! }
}
/**
* Reconstitute the {@code BigDecimal} instance from a stream (that is,
* deserialize it).
*** 3752,3762 ****
if (intVal == null) {
String message = "BigDecimal: null intVal in stream";
throw new java.io.StreamCorruptedException(message);
// [all values of scale are now allowed]
}
! setIntCompactVolatile(compactValFor(intVal));
}
/**
* Serialize this {@code BigDecimal} to the stream in question
*
--- 3753,3763 ----
if (intVal == null) {
String message = "BigDecimal: null intVal in stream";
throw new java.io.StreamCorruptedException(message);
// [all values of scale are now allowed]
}
! UnsafeHolder.setIntCompactVolatile(this, compactValFor(intVal));
}
/**
* Serialize this {@code BigDecimal} to the stream in question
*
*** 3764,3774 ****
*/
private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException {
// Must inflate to maintain compatible serial form.
if (this.intVal == null)
! this.setIntValVolatile(BigInteger.valueOf(this.intCompact));
// Could reset intVal back to null if it has to be set.
s.defaultWriteObject();
}
/**
--- 3765,3775 ----
*/
private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException {
// Must inflate to maintain compatible serial form.
if (this.intVal == null)
! UnsafeHolder.setIntValVolatile(this, BigInteger.valueOf(this.intCompact));
// Could reset intVal back to null if it has to be set.
s.defaultWriteObject();
}
/**