KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > MyUtility


1 /*
2  * Copyright (C) 2004 Sun Microsystems, Inc. All rights reserved. Use is
3  * subject to license terms.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the Lesser GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA.
19  */

20
21 /**
22  * JDIC API demo class.
23  * <p>
24  * The utility class convers the length to KB numbers.
25  */

26 import java.io.File JavaDoc;
27 import java.util.Vector JavaDoc;
28 class MyUtility {
29     private static String JavaDoc osName = System.getProperty("os.name").toLowerCase();
30     private static File JavaDoc roots[] = null;
31     public static String JavaDoc length2KB(long length) {
32         long KB = 1024;
33         long MB = 1024 * 1024;
34
35         if (length == 0) {
36             return String.valueOf("0 KB ");
37         }
38
39         String JavaDoc kbStr = "";
40         long mbCount = length / MB;
41
42         long kbCount = ((length - mbCount * MB) + 1023) / KB;
43
44         if (mbCount != 0) {
45             kbStr += String.valueOf(mbCount + ",");
46         }
47
48         if (kbCount == 0) {
49             if (mbCount == 0) {
50                 kbStr += String.valueOf("0 KB ");
51             } else {
52                 kbStr += String.valueOf("000 KB ");
53             }
54         } else {
55             kbStr += String.valueOf(kbCount + " KB ");
56         }
57
58         return kbStr;
59     }
60     
61     static class FileSystemRoot extends File JavaDoc {
62         public FileSystemRoot(File JavaDoc f) {
63             super(f, "");
64         }
65         
66         public FileSystemRoot(String JavaDoc s) {
67             super(s);
68         }
69         
70         public boolean isDirectory() {
71             return true;
72         }
73     }
74     
75     public static File JavaDoc[] getRoots() {
76         if (roots == null) {
77             constructRoots();
78         }
79         return roots;
80     }
81     
82     private static void constructRoots() {
83         if (osName.startsWith("windows")) {
84             Vector JavaDoc rootsVector = new Vector JavaDoc();
85         
86             // Create the A: drive whether it is mounted or not
87
FileSystemRoot floppy = new FileSystemRoot("A" + ":" + "\\");
88             rootsVector.addElement(floppy);
89
90             // Run through all possible mount points and check
91
// for their existance.
92
for (char c = 'C'; c <= 'Z'; c++) {
93                 char device[] = {c, ':', '\\'};
94                 String JavaDoc deviceName = new String JavaDoc(device);
95                 File JavaDoc deviceFile = new FileSystemRoot(deviceName);
96                 if (deviceFile != null && deviceFile.exists()) {
97                     rootsVector.addElement(deviceFile);
98                 }
99             }
100             roots = new File JavaDoc[rootsVector.size()];
101             rootsVector.copyInto(roots);
102         } else {
103             roots = File.listRoots();
104         }
105     }
106 }
107
Popular Tags