View Javadoc

1   /*
2   Copyright (C) 2000 - 2007 Grid Systems, S.A.
3   
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License, version 2, as
6   published by the Free Software Foundation.
7   
8   This program is distributed in the hope that it will be useful,
9   but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12  
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */
17  
18  package com.gridsystems.systemutils;
19  
20  import java.io.File;
21  
22  /**
23   * Provides information of the disk units.
24   *
25   * @author XMAS
26   * @author SJM
27   */
28  public final class SystemUtils {
29  
30    /**
31     * Constructor.
32     */
33    private SystemUtils() {
34    }
35  
36    /**
37     * Gets the free disk space in the partition/unit to which the specified path belongs.
38     *
39     * @param path path to a file in the partition/unit whose space is requested.
40     * <br>WARNING: The path must point to an existing file. In Windows
41     * systems, it must point to a folder/directory, not to a regular file. To
42     * avoid cross-platform issues, it is advised to always use a path to a
43     * directory
44     *
45     * @return the free space in bytes, rounded down. 0 in case of error.
46     */
47    public static long getFreeDiskSpace(String path) {
48      return (JNISystemUtils.getFreeDiskSpace(path) * 1024 * 1024);
49    }
50  
51    /**
52     * Gets the total disk space in the partition/unit to which the specified path belongs.
53     *
54     * @param path path to a file in the partition/unit whose space is requested.
55     * <br>WARNING: The path must point to an existing file. In Windows
56     * systems, it must point to a folder/directory, not to a regular file. To
57     * avoid cross-platform issues, it is advised to always use a path to a
58     * directory
59     *
60     * @return the total disk space in bytes, rounded down. 0 in case of error.
61     */
62    public static long getTotalDiskSpace(String path) {
63      return (JNISystemUtils.getTotalDiskSpace(path) * 1024 * 1024);
64    }
65  
66    /**
67     * Obtain Mac Address of local machine.
68     *
69     * @return MacAddress of local machine.
70     * If String starts with 'Error:',
71     *          then the description of error is included in this string.
72     * @throws Exception if error
73     */
74    public static String getLocalMacAddress() throws Exception {
75      return JNISystemUtils.getLocalMacAddress();
76    }
77  
78    /**
79     * Test program.
80     *
81     * Shows the total and free disk spaces in the working directory.
82     *
83     * @param args Unused.
84     */
85    public static void main(String[] args) {
86      try {
87        String str = new File(".").getAbsolutePath();
88        System.out.println("Free Disk: " + SystemUtils.getFreeDiskSpace(str) + " bytes");
89        System.out.println("Total Disk: " + SystemUtils.getTotalDiskSpace(str) + " bytes");
90        System.out.println("Mac Address: " + SystemUtils.getLocalMacAddress());
91        System.exit(0);
92      } catch (Exception ex) {
93        ex.printStackTrace();
94      }
95    }
96  
97  }