1 70 package com.ivata.groupware.web.format; 71 72 import com.ivata.mask.util.StringHandling; 73 import com.ivata.mask.web.format.HTMLFormat; 74 75 76 86 public class ByteSizeFormat implements HTMLFormat { 87 93 static final int kiloSize = 1024; 94 98 static final int megaSize = 1048576; 99 103 private int decimals = 1; 104 108 private String byteUnits = ""; 109 113 private String kiloUnits = "k"; 114 118 private String megaUnits = "M"; 119 122 private char decimalCharacter = '.'; 123 124 133 public String format(final String byteTextParam) { 134 Integer bytesInteger = StringHandling.integerValue(byteTextParam); 136 int bytes; 137 String byteText = byteTextParam; 138 if(bytesInteger == null) { 139 bytes = 0; 140 } else { 141 bytes = bytesInteger.intValue(); 142 } 143 if(bytes < kiloSize) { 145 byteText = bytes + byteUnits; 146 } else { 147 int rounding = 10; 149 for(int exp = 0; exp < decimals; ++exp) { 150 rounding *= 10; 151 } 152 long newValue = (long) bytes * rounding; 153 String units; 154 if(bytes < megaSize) { 156 newValue /= kiloSize; 157 units = kiloUnits; 158 } else { 159 newValue /= megaSize; 160 units = megaUnits; 161 } 162 newValue += 5; 165 newValue /= 10; 166 167 String noDecimal = Long.toString(newValue); 169 int length = noDecimal.length(); 170 byteText = noDecimal.substring(0, length - decimals) 171 + decimalCharacter 172 + noDecimal.substring(length - decimals) 173 + units; 174 } 175 return byteText; 176 } 177 178 184 public final int getDecimals() { 185 return decimals; 186 } 187 188 194 public final void setDecimals(final int decimals) { 195 this.decimals = decimals; 196 } 197 198 204 public final String getByteUnits() { 205 return byteUnits; 206 } 207 208 215 public final void setByteUnits(final String byteUnits) { 216 this.byteUnits = byteUnits; 217 } 218 219 225 public final String getKiloUnits() { 226 return kiloUnits; 227 } 228 229 236 public final void setKiloUnits(final String kiloUnits) { 237 this.kiloUnits = kiloUnits; 238 } 239 240 246 public final String getMegaUnits() { 247 return megaUnits; 248 } 249 250 257 public final void setMegaUnits(final String megaUnits) { 258 this.megaUnits = megaUnits; 259 } 260 261 266 public final char getDecimalCharacter() { 267 return decimalCharacter; 268 } 269 270 275 public final void setDecimalCharacter(final char decimalCharacter) { 276 this.decimalCharacter = decimalCharacter; 277 } 278 } 279 | Popular Tags |