Add BigIntegers.intValueExact
@@ -124,6 +124,17 @@ public static BigInteger fromUnsignedByteArray(byte[] buf, int off, int length)
return new BigInteger(1, mag);
}
+ public static int intValueExact(BigInteger x)
+ {
+ // Since Java 1.8 could use BigInteger.intValueExact instead
+ if (x.bitLength() > 31)
+ {
+ throw new IllegalArgumentException("'x' must be at most 31 bits");
+ }
+
+ return x.intValue();
+ }
+
public static int getUnsignedByteLength(BigInteger n)
{
return (n.bitLength() + 7) / 8;
