1 4 package org.openedit.links; 5 6 import java.io.File ; 7 import java.math.BigDecimal ; 8 import java.util.HashMap ; 9 import java.util.Map ; 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 21 public class FileSize 22 { 23 26 protected Map fieldMapOfSizesInBytes; 27 protected PageManager fieldPageManager; 28 protected File fieldRoot; 29 30 public String inEnglish(Long 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 bd = new BigDecimal (ks); 46 bd = bd.setScale(2,BigDecimal.ROUND_UP); 47 ks = bd.doubleValue(); 48 49 return ks + " MB"; 50 51 } 52 53 } 55 public String inEnglish(String inPath) throws OpenEditException 56 { 57 Long i = getSize(inPath); 58 return inEnglish(i); 59 } 60 61 public String stringToEnglish(String size) throws OpenEditException 62 { 63 long i = Long.parseLong(size); 64 return inEnglish(new Long (i)); 65 } 66 67 public String inKs(String inPath) throws OpenEditException 68 { 69 Long i = getSize(inPath); 71 if ( i == null) 72 { 73 return ""; 74 } 75 double ks = (double)i.intValue()/1024D; 76 81 String value = String.valueOf(Math.round(ks)); 82 87 return value; 88 } 89 public String inBytes( String inPath ) throws OpenEditException 90 { 91 Long i = getSize(inPath); 92 if( i == null) 93 { 94 return "missing"; 95 } 96 return i.toString(); 97 } 98 public Long getSize(String inPath) throws OpenEditException 99 { 100 Long i = (Long )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 root = new File ( getRoot(), page.getContentItem().getPath() ); 110 length = FileUtils.sizeOfDirectory(root); 111 } 112 else 113 { 114 length = page.getContentItem().getLength(); 115 } 116 i = new Long (length); 117 getMapOfSizesInBytes().put(inPath,i); 118 } 119 } 120 return i; 121 } 122 123 protected Map getMapOfSizesInBytes() 124 { 125 if ( fieldMapOfSizesInBytes == null) 126 { 127 fieldMapOfSizesInBytes = new HashMap (); 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 getRoot() 141 { 142 return fieldRoot; 143 } 144 145 protected void setRoot(File inRoot) 146 { 147 fieldRoot = inRoot; 148 } 149 } 150 | Popular Tags |