1. 检测jdk版本
@SuppressWarnings("LoopStatementThatDoesntLoop"
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Not really a loop</span>
<span style="color: #0000ff;">for</span><span style="color: #000000;"> (;;) {
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Android</span>
<span style="color: #0000ff;">if</span><span style="color: #000000;"> (isAndroid()) {
javaVersion </span>= 6<span style="color: #000000;">;
</span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
}
</span><span style="color: #0000ff;">try</span><span style="color: #000000;"> {
Class.forName(</span>"java.time.Clock",<span style="color: #0000ff;">false</span>,getClassLoader(Object.<span style="color: #0000ff;">class</span><span style="color: #000000;">));
javaVersion </span>= 8<span style="color: #000000;">;
</span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
} </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (Exception e) {
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Ignore</span>
<span style="color: #000000;"> }
</span><span style="color: #0000ff;">try</span><span style="color: #000000;"> {
Class.forName(</span>"java.util.concurrent.LinkedTransferQueue",getClassLoader(BlockingQueue.<span style="color: #0000ff;">class</span><span style="color: #000000;">));
javaVersion </span>= 7<span style="color: #000000;">;
</span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
} </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (Exception e) {
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Ignore</span>
<span style="color: #000000;"> }
javaVersion </span>= 6<span style="color: #000000;">;
</span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
}
</span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (logger.isDebugEnabled()) {
logger.debug(</span>"Java version: {}"<span style="color: #000000;">,javaVersion);
}
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> javaVersion;
}</span></pre>
2. 检测是否window
windows = SystemPropertyUtil.get("os.name","").toLowerCase(Locale.US).contains("win""Platform: Windows"
3. 检测是否root权限
String[] ID_COMMANDS </span>= { "/usr/bin/id","/bin/id","/usr/xpg4/bin/id","id"<span style="color: #000000;">};
Pattern UID_PATTERN </span>= Pattern.compile("^(?:0|[1-9][0-9]*)$"<span style="color: #000000;">);
</span><span style="color: #0000ff;">for</span><span style="color: #000000;"> (String idCmd: ID_COMMANDS) {
Process p </span>= <span style="color: #0000ff;">null</span><span style="color: #000000;">;
BufferedReader in </span>= <span style="color: #0000ff;">null</span><span style="color: #000000;">;
String uid </span>= <span style="color: #0000ff;">null</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">try</span><span style="color: #000000;"> {
p </span>= Runtime.getRuntime().exec(<span style="color: #0000ff;">new</span> String[] { idCmd,"-u"<span style="color: #000000;"> });
in </span>= <span style="color: #0000ff;">new</span> BufferedReader(<span style="color: #0000ff;">new</span><span style="color: #000000;"> InputStreamReader(p.getInputStream(),CharsetUtil.US_ASCII));
uid </span>=<span style="color: #000000;"> in.readLine();
in.close();
</span><span style="color: #0000ff;">for</span><span style="color: #000000;"> (;;) {
</span><span style="color: #0000ff;">try</span><span style="color: #000000;"> {
</span><span style="color: #0000ff;">int</span> exitCode =<span style="color: #000000;"> p.waitFor();
</span><span style="color: #0000ff;">if</span> (exitCode != 0<span style="color: #000000;">) {
uid </span>= <span style="color: #0000ff;">null</span><span style="color: #000000;">;
}
</span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
} </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (InterruptedException e) {
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Ignore</span>
<span style="color: #000000;"> }
}
}
<span style="color: #008000;">//<span style="color: #008000;"> Failed to run the command.
uid = <span style="color: #0000ff;">null<span style="color: #000000;">;
} <span style="color: #0000ff;">finally<span style="color: #000000;"> {
<span style="color: #0000ff;">if (in != <span style="color: #0000ff;">null<span style="color: #000000;">) {
<span style="color: #0000ff;">try<span style="color: #000000;"> {
in.close();
} <span style="color: #0000ff;">catch<span style="color: #000000;"> (IOException e) {
<span style="color: #008000;">//<span style="color: #008000;"> Ignore
<span style="color: #000000;"> }
}
<span style="color: #0000ff;">if (p != <span style="color: #0000ff;">null<span style="color: #000000;">) {
<span style="color: #0000ff;">try<span style="color: #000000;"> {
p.destroy();
} <span style="color: #0000ff;">catch<span style="color: #000000;"> (Exception e) {
<span style="color: #008000;">//<span style="color: #008000;"> Android sometimes triggers an ErrnoException.
<span style="color: #000000;"> }
}
}
</span><span style="color: #0000ff;">if</span> (uid != <span style="color: #0000ff;">null</span> &&<span style="color: #000000;"> UID_PATTERN.matcher(uid).matches()) {
logger.debug(</span>"UID: {}"<span style="color: #000000;">,uid);
</span><span style="color: #0000ff;">return</span> "0"<span style="color: #000000;">.equals(uid);
}
}
logger.debug(</span>"Could not determine the current UID using /usr/bin/id; attempting to bind at privileged ports."<span style="color: #000000;">);
Pattern PERMISSION_DENIED </span>= Pattern.compile(".*(?:denied|not.*permitted).*"<span style="color: #000000;">);
</span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> i = 1023; i > 0; i --<span style="color: #000000;">) {
ServerSocket ss </span>= <span style="color: #0000ff;">null</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">try</span><span style="color: #000000;"> {
ss </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> ServerSocket();
ss.setReuseAddress(</span><span style="color: #0000ff;">true</span><span style="color: #000000;">);
ss.bind(</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> InetSocketAddress(i));
</span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (logger.isDebugEnabled()) {
logger.debug(</span>"UID: 0 (succeded to bind at port {})"<span style="color: #000000;">,i);
}
</span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">;
} </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (Exception e) {
</span><span style="color: #008000;">//</span><span style="color: #008000;"> <a href="/tag/Failed/" target="_blank" class="keywords">Failed</a> to bind.
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Check the error message so that we don't always need to bind 1023 times.</span>
String message =<span style="color: #000000;"> e.getMessage();
</span><span style="color: #0000ff;">if</span> (message == <span style="color: #0000ff;">null</span><span style="color: #000000;">) {
message </span>= ""<span style="color: #000000;">;
}
message </span>=<span style="color: #000000;"> message.toLowerCase();
</span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (PERMISSION_DENIED.matcher(message).matches()) {
</span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
}
} </span><span style="color: #0000ff;">finally</span><span style="color: #000000;"> {
</span><span style="color: #0000ff;">if</span> (ss != <span style="color: #0000ff;">null</span><span style="color: #000000;">) {
</span><span style="color: #0000ff;">try</span><span style="color: #000000;"> {
ss.close();
} </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (Exception e) {
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Ignore.</span>
<span style="color: #000000;"> }
}
}
}
logger.debug(</span>"UID: non-root (<a href="/tag/Failed/" target="_blank" class="keywords">Failed</a> to bind at any privileged ports)"<span style="color: #000000;">);
</span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;
}</span></pre>
4.检测最大直接内存
maxDirectMemory = 0
Class> vmClass = Class.forName("sun.misc.VM",= vmClass.getDeclaredMethod("maxDirectMemory"= ((Number) m.invoke(
</span><span style="color: #0000ff;">if</span> (maxDirectMemory > 0<span style="color: #000000;">) {
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> maxDirectMemory;
}
</span><span style="color: #0000ff;">try</span><span style="color: #000000;"> {
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Now try to get the JVM option (-XX:MaxDirectMemorySize) and parse it.
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Note that we are using reflection because Android doesn't have these classes.</span>
Class<?> mgmtFactoryClass =<span style="color: #000000;"> Class.forName(
</span>"java.lang.management.ManagementFactory",getSystemClassLoader());
Class</span><?> runtimeClass =<span style="color: #000000;"> Class.forName(
</span>"java.lang.management.RuntimeMXBean",getSystemClassLoader());
Object runtime </span>= mgmtFactoryClass.getDeclaredMethod("getRuntimeMXBean").invoke(<span style="color: #0000ff;">null</span><span style="color: #000000;">);
@SuppressWarnings(</span>"unchecked"<span style="color: #000000;">)
List</span><String> vmArgs = (List<String>) runtimeClass.getDeclaredMethod("getInputArguments"<span style="color: #000000;">).invoke(runtime);
</span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> i = vmArgs.size() - 1; i >= 0; i --<span style="color: #000000;">) {
Matcher m </span>=<span style="color: #000000;"> MAX_DIRECT_MEMORY_SIZE_ARG_PATTERN.matcher(vmArgs.get(i));
</span><span style="color: #0000ff;">if</span> (!<span style="color: #000000;">m.matches()) {
</span><span style="color: #0000ff;">continue</span><span style="color: #000000;">;
}
maxDirectMemory </span>= Long.parseLong(m.group(1<span style="color: #000000;">));
</span><span style="color: #0000ff;">switch</span> (m.group(2).charAt(0<span style="color: #000000;">)) {
</span><span style="color: #0000ff;">case</span> 'k': <span style="color: #0000ff;">case</span> 'K'<span style="color: #000000;">:
maxDirectMemory </span>*= 1024<span style="color: #000000;">;
</span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">case</span> 'm': <span style="color: #0000ff;">case</span> 'M'<span style="color: #000000;">:
maxDirectMemory </span>*= 1024 * 1024<span style="color: #000000;">;
</span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">case</span> 'g': <span style="color: #0000ff;">case</span> 'G'<span style="color: #000000;">:
maxDirectMemory </span>*= 1024 * 1024 * 1024<span style="color: #000000;">;
</span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
}
</span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
}
} </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (Throwable t) {
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Ignore</span>
<span style="color: #000000;"> }
</span><span style="color: #0000ff;">if</span> (maxDirectMemory <= 0<span style="color: #000000;">) {
maxDirectMemory </span>=<span style="color: #000000;"> Runtime.getRuntime().maxMemory();
logger.debug(</span>"maxDirectMemory: {} bytes (maybe)"<span style="color: #000000;">,maxDirectMemory);
} </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
logger.debug(</span>"maxDirectMemory: {} bytes"<span style="color: #000000;">,maxDirectMemory);
}
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> maxDirectMemory;
}</span></pre>
等等