KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openedit > links > FileSize


1 /*
2  * Created on Feb 6, 2005
3  */

4 package org.openedit.links;
5
6 import java.io.File JavaDoc;
7 import java.math.BigDecimal JavaDoc;
8 import java.util.HashMap JavaDoc;
9 import java.util.Map JavaDoc;
10
11 import org.apache.commons.io.FileUtils;
12
13 import com.openedit.OpenEditException;
14 import com.openedit.page.Page;
15 import com.openedit.page.manage.PageManager;
16
17 /**
18  * @author cburkey
19  *
20  */

21 public class FileSize
22 {
23     //all size stores in 16 bit bytes
24
//a 1K is 1024 bytes
25

26     protected Map JavaDoc fieldMapOfSizesInBytes;
27     protected PageManager fieldPageManager;
28     protected File JavaDoc fieldRoot;
29     
30     public String JavaDoc inEnglish(Long JavaDoc inNum) throws OpenEditException
31     {
32         if ( inNum == null)
33         {
34             return "";
35         }
36         
37         if ( inNum.longValue() < 1024000)
38         {
39             return inNum.toString() + " bytes";
40         }
41         else
42         {
43             double ks = (double)inNum.doubleValue()/1024000D;
44
45             BigDecimal JavaDoc bd = new BigDecimal JavaDoc(ks);
46             bd = bd.setScale(2,BigDecimal.ROUND_UP);
47             ks = bd.doubleValue();
48             
49             return ks + " MB";
50             
51         }
52         
53         //return FileUtils.byteCountToDisplaySize(i.longValue());
54
}
55     public String JavaDoc inEnglish(String JavaDoc inPath) throws OpenEditException
56     {
57         Long JavaDoc i = getSize(inPath);
58         return inEnglish(i);
59     }
60     
61     public String JavaDoc stringToEnglish(String JavaDoc size) throws OpenEditException
62     {
63         long i = Long.parseLong(size);
64         return inEnglish(new Long JavaDoc(i));
65     }
66     
67     public String JavaDoc inKs(String JavaDoc inPath) throws OpenEditException
68     {
69         //look up this file and check the size
70
Long JavaDoc i = getSize(inPath);
71         if ( i == null)
72         {
73             return "";
74         }
75         double ks = (double)i.intValue()/1024D;
76         //need to trim this off
77
/* ks = ks*100;
78         ks = Math.round(ks);
79         ks = ks/100;
80 */

81         String JavaDoc value = String.valueOf(Math.round(ks));
82 /* if ( value.indexOf(".") > 2)
83         {
84             value = value.substring(0,value.indexOf(".") + 2);
85         }
86         */

87         return value;
88     }
89     public String JavaDoc inBytes( String JavaDoc inPath ) throws OpenEditException
90     {
91         Long JavaDoc i = getSize(inPath);
92         if( i == null)
93         {
94             return "missing";
95         }
96         return i.toString();
97     }
98     public Long JavaDoc getSize(String JavaDoc inPath) throws OpenEditException
99     {
100         Long JavaDoc i = (Long JavaDoc)getMapOfSizesInBytes().get(inPath);
101         if( i == null)
102         {
103             Page page = getPageManager().getPage(inPath);
104             if ( page.exists())
105             {
106                 long length = 0;
107                 if( page.isFolder() )
108                 {
109                     File JavaDoc root = new File JavaDoc( getRoot(), page.getContentItem().getPath() );
110                     length = FileUtils.sizeOfDirectory(root);
111                 }
112                 else
113                 {
114                     length = page.getContentItem().getLength();
115                 }
116                 i = new Long JavaDoc(length);
117                 getMapOfSizesInBytes().put(inPath,i);
118             }
119         }
120         return i;
121     }
122     
123     protected Map JavaDoc getMapOfSizesInBytes()
124     {
125         if ( fieldMapOfSizesInBytes == null)
126         {
127             fieldMapOfSizesInBytes = new HashMap JavaDoc();
128         }
129         return fieldMapOfSizesInBytes;
130     }
131     protected PageManager getPageManager()
132     {
133         return fieldPageManager;
134     }
135     public void setPageManager(PageManager inPageManager)
136     {
137         fieldPageManager = inPageManager;
138     }
139
140     protected File JavaDoc getRoot()
141     {
142         return fieldRoot;
143     }
144
145     protected void setRoot(File JavaDoc inRoot)
146     {
147         fieldRoot = inRoot;
148     }
149 }
150
Popular Tags