在 Linux/FreeBSD 平台读取 os-release (#3314)
* 简化 OperatingSystem.getPhysicalMemoryStatus * Read os-release * update * update
This commit is contained in:
@@ -17,6 +17,8 @@
|
||||
*/
|
||||
package org.jackhuang.hmcl.util.platform;
|
||||
|
||||
import org.jackhuang.hmcl.util.KeyValuePairProperties;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
@@ -28,8 +30,9 @@ import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Locale;
|
||||
import java.util.Optional;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@@ -118,6 +121,9 @@ public enum OperatingSystem {
|
||||
*/
|
||||
public static final String SYSTEM_VERSION;
|
||||
|
||||
public static final String OS_RELEASE_NAME;
|
||||
public static final String OS_RELEASE_PRETTY_NAME;
|
||||
|
||||
public static final Pattern INVALID_RESOURCE_CHARACTERS;
|
||||
private static final String[] INVALID_RESOURCE_BASENAMES;
|
||||
private static final String[] INVALID_RESOURCE_FULLNAMES;
|
||||
@@ -144,7 +150,7 @@ public enum OperatingSystem {
|
||||
}
|
||||
}
|
||||
} catch (UnsupportedCharsetException e) {
|
||||
e.printStackTrace();
|
||||
e.printStackTrace(System.err);
|
||||
}
|
||||
NATIVE_CHARSET = nativeCharset;
|
||||
|
||||
@@ -187,9 +193,24 @@ public enum OperatingSystem {
|
||||
SYSTEM_BUILD_NUMBER = -1;
|
||||
}
|
||||
|
||||
TOTAL_MEMORY = getPhysicalMemoryStatus()
|
||||
.map(physicalMemoryStatus -> (int) (physicalMemoryStatus.getTotal() / 1024 / 1024))
|
||||
.orElse(1024);
|
||||
Map<String, String> osRelease = Collections.emptyMap();
|
||||
if (CURRENT_OS == LINUX || CURRENT_OS == FREEBSD) {
|
||||
Path osReleaseFile = Paths.get("/etc/os-release");
|
||||
if (Files.exists(osReleaseFile)) {
|
||||
try {
|
||||
osRelease = KeyValuePairProperties.load(osReleaseFile);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace(System.err);
|
||||
}
|
||||
}
|
||||
}
|
||||
OS_RELEASE_NAME = osRelease.get("NAME");
|
||||
OS_RELEASE_PRETTY_NAME = osRelease.get("PRETTY_NAME");
|
||||
|
||||
PhysicalMemoryStatus physicalMemoryStatus = getPhysicalMemoryStatus();
|
||||
TOTAL_MEMORY = physicalMemoryStatus != PhysicalMemoryStatus.INVALID
|
||||
? (int) (physicalMemoryStatus.getTotal() / 1024 / 1024)
|
||||
: 1024;
|
||||
|
||||
SUGGESTED_MEMORY = TOTAL_MEMORY >= 32768 ? 8192 : (int) (Math.round(1.0 * TOTAL_MEMORY / 4.0 / 128.0) * 128);
|
||||
|
||||
@@ -256,7 +277,7 @@ public enum OperatingSystem {
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public static Optional<PhysicalMemoryStatus> getPhysicalMemoryStatus() {
|
||||
public static PhysicalMemoryStatus getPhysicalMemoryStatus() {
|
||||
if (CURRENT_OS == LINUX) {
|
||||
try {
|
||||
long free = 0, available = 0, total = 0;
|
||||
@@ -277,10 +298,10 @@ public enum OperatingSystem {
|
||||
}
|
||||
}
|
||||
if (total > 0) {
|
||||
return Optional.of(new PhysicalMemoryStatus(total, available > 0 ? available : free));
|
||||
return new PhysicalMemoryStatus(total, available > 0 ? available : free);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
e.printStackTrace(System.err);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -290,11 +311,11 @@ public enum OperatingSystem {
|
||||
com.sun.management.OperatingSystemMXBean sunBean =
|
||||
(com.sun.management.OperatingSystemMXBean)
|
||||
java.lang.management.ManagementFactory.getOperatingSystemMXBean();
|
||||
return Optional.of(new PhysicalMemoryStatus(sunBean.getTotalPhysicalMemorySize(), sunBean.getFreePhysicalMemorySize()));
|
||||
return new PhysicalMemoryStatus(sunBean.getTotalPhysicalMemorySize(), sunBean.getFreePhysicalMemorySize());
|
||||
}
|
||||
} catch (NoClassDefFoundError ignored) {
|
||||
}
|
||||
return Optional.empty();
|
||||
return PhysicalMemoryStatus.INVALID;
|
||||
}
|
||||
|
||||
@SuppressWarnings("removal")
|
||||
|
||||
Reference in New Issue
Block a user