KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > util > SystemUtil


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.util;
4
5 /**
6  * Various system utilities.
7  */

8 public class SystemUtil {
9     /**
10      * Calculates and returns java system timer resolution in miliseconds.
11      * Resolution of a timer depends on platform and java version.
12      */

13     public static double systemTimerResolution() {
14         long t1, t2;
15         int sumres = 0;
16         //noinspection CallToSystemGC
17
System.gc();
18         int loops = 20;
19         for (int i = 0; i < loops; ++i) {
20             t1 = System.currentTimeMillis();
21             while (true) {
22                 t2 = System.currentTimeMillis();
23                 if (t2 != t1) {
24                     sumres += (int) (t2 - t1);
25                     break;
26                 }
27             }
28         }
29         return (sumres / (double) loops);
30     }
31
32     // ---------------------------------------------------------------- properties
33

34     public static final String JavaDoc SYS_USER_DIR = "user.dir";
35     public static final String JavaDoc SYS_JAVA_HOME = "java.home";
36     public static final String JavaDoc SYS_TEMP_DIR = "java.io.tmpdir";
37     public static final String JavaDoc SYS_OS_NAME = "os.name";
38     public static final String JavaDoc SYS_OS_VERSION = "os.version";
39     public static final String JavaDoc SYS_JAVA_VERSION = "java.version";
40     public static final String JavaDoc SYS_JAVA_VENDOR = "java.vendor";
41     public static final String JavaDoc SYS_JAVA_CLASSPATH = "java.class.path";
42     public static final String JavaDoc SYS_PATH_SEPARATOR = "path.separator";
43
44     /**
45      * Returns current working folder.
46      */

47     public static String JavaDoc getUserDir() {
48         return System.getProperty(SYS_USER_DIR);
49     }
50
51     public static String JavaDoc getJavaJreHome() {
52         return System.getProperty(SYS_JAVA_HOME);
53     }
54
55     /**
56      * Returns JAVA_HOME which is not equals to "java.home" property
57      * since it points to JAVA_HOME/jre folder.
58      */

59     public static String JavaDoc getJavaHome() {
60         String JavaDoc home = System.getProperty(SYS_JAVA_HOME);
61         if (home == null) {
62             return null;
63         }
64         int i = home.lastIndexOf('\\');
65         int j = home.lastIndexOf('/');
66         if (j > i) {
67             i = j;
68         }
69         return home.substring(0, i);
70     }
71
72     public static String JavaDoc getTempDir() {
73         return System.getProperty(SYS_TEMP_DIR);
74     }
75
76     public static String JavaDoc getOsName() {
77         return System.getProperty(SYS_OS_NAME);
78     }
79
80     public static String JavaDoc getOsVersion() {
81         return System.getProperty(SYS_OS_VERSION);
82     }
83
84     public static String JavaDoc getJavaVersion() {
85         return System.getProperty(SYS_JAVA_VERSION);
86     }
87
88     public static String JavaDoc getJavaVendor() {
89         return System.getProperty(SYS_JAVA_VENDOR);
90     }
91
92     public static String JavaDoc getClassPath() {
93         return System.getProperty(SYS_JAVA_CLASSPATH);
94     }
95
96     public static String JavaDoc getPathSeparator() {
97         return System.getProperty(SYS_PATH_SEPARATOR);
98     }
99
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 JavaDoc 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() - Runtime.getRuntime().freeMemory();
129     }
130
131     /**
132      * Returns percents of used memory.
133      */

134     public static double usedMemoryPercents() {
135         Runtime JavaDoc runtime = Runtime.getRuntime();
136         long totalMemory = runtime.totalMemory();
137         return ((double) (totalMemory - runtime.freeMemory()) / totalMemory) * 100.0;
138     }
139
140
141 }
142
Popular Tags