mirror of
https://github.com/Qortal/altcoinj.git
synced 2025-02-14 19:25:51 +00:00
Utils: Add isOpenJDKRuntime() and isJavaSERuntime() helpers.
This commit is contained in:
parent
39386853a1
commit
cdbf9ecc9f
@ -36,6 +36,8 @@ import java.util.concurrent.TimeUnit;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.bouncycastle.crypto.digests.RIPEMD160Digest;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.google.common.base.Joiner;
|
||||
import com.google.common.base.Splitter;
|
||||
@ -69,6 +71,8 @@ public class Utils {
|
||||
|
||||
private static BlockingQueue<Boolean> mockSleepQueue;
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(Utils.class);
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* The regular {@link BigInteger#toByteArray()} includes the sign bit of the number and
|
||||
@ -546,13 +550,35 @@ public class Utils {
|
||||
return maxItem;
|
||||
}
|
||||
|
||||
private static int isAndroid = -1;
|
||||
private enum Runtime {
|
||||
ANDROID, OPENJDK, ORACLE_JAVA
|
||||
}
|
||||
|
||||
private static Runtime runtime = null;
|
||||
static {
|
||||
String runtimeProp = System.getProperty("java.runtime.name").toLowerCase(Locale.US);
|
||||
if (runtimeProp == null)
|
||||
runtime = null;
|
||||
else if (runtimeProp.contains("android"))
|
||||
runtime = Runtime.ANDROID;
|
||||
else if (runtimeProp.contains("openjdk"))
|
||||
runtime = Runtime.OPENJDK;
|
||||
else if (runtimeProp.contains("java(tm) se"))
|
||||
runtime = Runtime.ORACLE_JAVA;
|
||||
else
|
||||
log.info("Unknown java.runtime.name '{}'", runtimeProp);
|
||||
}
|
||||
|
||||
public static boolean isAndroidRuntime() {
|
||||
if (isAndroid == -1) {
|
||||
final String runtime = System.getProperty("java.runtime.name");
|
||||
isAndroid = (runtime != null && runtime.equals("Android Runtime")) ? 1 : 0;
|
||||
}
|
||||
return isAndroid == 1;
|
||||
return runtime == Runtime.ANDROID;
|
||||
}
|
||||
|
||||
public static boolean isOpenJDKRuntime() {
|
||||
return runtime == Runtime.OPENJDK;
|
||||
}
|
||||
|
||||
public static boolean isOracleJavaRuntime() {
|
||||
return runtime == Runtime.ORACLE_JAVA;
|
||||
}
|
||||
|
||||
public static boolean isLinux() {
|
||||
|
@ -112,4 +112,11 @@ public class UtilsTest {
|
||||
byte[] actual = Utils.bigIntegerToBytes(b, 1);
|
||||
assertTrue(Arrays.equals(expected, actual));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void runtime() {
|
||||
// This test assumes it is run within a Java runtime for desktop computers.
|
||||
assertTrue(Utils.isOpenJDKRuntime() || Utils.isOracleJavaRuntime());
|
||||
assertFalse(Utils.isAndroidRuntime());
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user