001: // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
002:
003: package jodd.util;
004:
005: /**
006: * Various system utilities.
007: */
008: public class SystemUtil {
009: /**
010: * Calculates and returns java system timer resolution in miliseconds.
011: * Resolution of a timer depends on platform and java version.
012: */
013: public static double systemTimerResolution() {
014: long t1, t2;
015: int sumres = 0;
016: //noinspection CallToSystemGC
017: System.gc();
018: int loops = 20;
019: for (int i = 0; i < loops; ++i) {
020: t1 = System.currentTimeMillis();
021: while (true) {
022: t2 = System.currentTimeMillis();
023: if (t2 != t1) {
024: sumres += (int) (t2 - t1);
025: break;
026: }
027: }
028: }
029: return (sumres / (double) loops);
030: }
031:
032: // ---------------------------------------------------------------- properties
033:
034: public static final String SYS_USER_DIR = "user.dir";
035: public static final String SYS_JAVA_HOME = "java.home";
036: public static final String SYS_TEMP_DIR = "java.io.tmpdir";
037: public static final String SYS_OS_NAME = "os.name";
038: public static final String SYS_OS_VERSION = "os.version";
039: public static final String SYS_JAVA_VERSION = "java.version";
040: public static final String SYS_JAVA_VENDOR = "java.vendor";
041: public static final String SYS_JAVA_CLASSPATH = "java.class.path";
042: public static final String SYS_PATH_SEPARATOR = "path.separator";
043:
044: /**
045: * Returns current working folder.
046: */
047: public static String getUserDir() {
048: return System.getProperty(SYS_USER_DIR);
049: }
050:
051: public static String getJavaJreHome() {
052: return System.getProperty(SYS_JAVA_HOME);
053: }
054:
055: /**
056: * Returns JAVA_HOME which is not equals to "java.home" property
057: * since it points to JAVA_HOME/jre folder.
058: */
059: public static String getJavaHome() {
060: String home = System.getProperty(SYS_JAVA_HOME);
061: if (home == null) {
062: return null;
063: }
064: int i = home.lastIndexOf('\\');
065: int j = home.lastIndexOf('/');
066: if (j > i) {
067: i = j;
068: }
069: return home.substring(0, i);
070: }
071:
072: public static String getTempDir() {
073: return System.getProperty(SYS_TEMP_DIR);
074: }
075:
076: public static String getOsName() {
077: return System.getProperty(SYS_OS_NAME);
078: }
079:
080: public static String getOsVersion() {
081: return System.getProperty(SYS_OS_VERSION);
082: }
083:
084: public static String getJavaVersion() {
085: return System.getProperty(SYS_JAVA_VERSION);
086: }
087:
088: public static String getJavaVendor() {
089: return System.getProperty(SYS_JAVA_VENDOR);
090: }
091:
092: public static String getClassPath() {
093: return System.getProperty(SYS_JAVA_CLASSPATH);
094: }
095:
096: public static String getPathSeparator() {
097: return System.getProperty(SYS_PATH_SEPARATOR);
098: }
099:
100: // ---------------------------------------------------------------- memory
101:
102: /**
103: * Returns amount of total memory in bytes.
104: */
105: public static long totalMemory() {
106: return Runtime.getRuntime().totalMemory();
107: }
108:
109: /**
110: * Returns amount of free memory in bytes.
111: */
112: public static long freeMemory() {
113: return Runtime.getRuntime().freeMemory();
114: }
115:
116: /**
117: * Returns percents of free memory.
118: */
119: public static double freeMemoryPercents() {
120: Runtime runtime = Runtime.getRuntime();
121: return ((double) runtime.freeMemory() / runtime.totalMemory()) * 100.0;
122: }
123:
124: /**
125: * Returns amount of used memory in bytes.
126: */
127: public static long usedMemory() {
128: return Runtime.getRuntime().totalMemory()
129: - Runtime.getRuntime().freeMemory();
130: }
131:
132: /**
133: * Returns percents of used memory.
134: */
135: public static double usedMemoryPercents() {
136: Runtime runtime = Runtime.getRuntime();
137: long totalMemory = runtime.totalMemory();
138: return ((double) (totalMemory - runtime.freeMemory()) / totalMemory) * 100.0;
139: }
140:
141: }
|