1 17 package org.alfresco.filesys.util; 18 19 25 public class MemorySize 26 { 27 29 public static final long KILOBYTE = 1024L; 30 public static final long MEGABYTE = 1024L * KILOBYTE; 31 public static final long GIGABYTE = 1024L * MEGABYTE; 32 public static final long TERABYTE = 1024L * GIGABYTE; 33 34 41 public static final int getByteValueInt(String memSize) 42 { 43 return (int) (getByteValue(memSize) & 0xFFFFFFFFL); 44 } 45 46 53 public static final long getByteValue(String memSize) 54 { 55 56 58 if (memSize == null || memSize.length() == 0) 59 return -1L; 60 61 63 String sizeStr = memSize.toUpperCase(); 64 long mult = 1; 65 long val = 0; 66 67 if (sizeStr.endsWith("K")) 68 { 69 70 72 mult = KILOBYTE; 73 val = getValue(sizeStr); 74 } 75 else if (sizeStr.endsWith("M")) 76 { 77 78 80 mult = MEGABYTE; 81 val = getValue(sizeStr); 82 } 83 else if (sizeStr.endsWith("G")) 84 { 85 86 88 mult = GIGABYTE; 89 val = getValue(sizeStr); 90 } 91 else if (sizeStr.endsWith("T")) 92 { 93 94 96 mult = TERABYTE; 97 val = getValue(sizeStr); 98 } 99 else 100 { 101 102 104 val = Long.valueOf(sizeStr).longValue(); 105 } 106 107 109 return val * mult; 110 } 111 112 119 private final static long getValue(String val) 120 { 121 122 124 String sizStr = val.substring(0, val.length() - 1); 125 return Long.valueOf(sizStr).longValue(); 126 } 127 128 134 public final static String asKilobyteString(long val) 135 { 136 137 139 long mbVal = val / KILOBYTE; 140 return "" + mbVal + "Kb"; 141 } 142 143 149 public final static String asMegabyteString(long val) 150 { 151 152 154 long mbVal = val / MEGABYTE; 155 return "" + mbVal + "Mb"; 156 } 157 158 164 public final static String asGigabyteString(long val) 165 { 166 167 169 long mbVal = val / GIGABYTE; 170 return "" + mbVal + "Gb"; 171 } 172 173 179 public final static String asTerabyteString(long val) 180 { 181 182 184 long mbVal = val / TERABYTE; 185 return "" + mbVal + "Tb"; 186 } 187 188 194 public final static String asScaledString(long val) 195 { 196 197 199 String ret = null; 200 201 if (val < (KILOBYTE * 2L)) 202 ret = Long.toString(val); 203 else if (val < (MEGABYTE * 2L)) 204 ret = asKilobyteString(val); 205 else if (val < (GIGABYTE * 2L)) 206 ret = asMegabyteString(val); 207 else if (val < (TERABYTE * 2L)) 208 ret = asGigabyteString(val); 209 else 210 ret = asTerabyteString(val); 211 212 return ret; 213 } 214 } 215 | Popular Tags |