1 11 12 package org.jivesoftware.util; 13 14 import java.text.DecimalFormat ; 15 import java.text.FieldPosition ; 16 import java.text.Format ; 17 import java.text.ParsePosition ; 18 19 25 public class ByteFormat extends Format { 26 27 30 public String format(long bytes) { 31 return format(new Long (bytes)); 32 } 33 34 37 public String formatKB(long kilobytes) { 38 return format(new Long (kilobytes * 1024)); 39 } 40 41 49 public StringBuffer format(Object obj, StringBuffer buf, FieldPosition pos) { 50 if (obj instanceof Long ) { 51 long numBytes = ((Long )obj).longValue(); 52 if (numBytes < 1024 * 1024) { 53 DecimalFormat formatter = new DecimalFormat ("#,##0.0"); 54 buf.append(formatter.format((double)numBytes / 1024.0)).append(" K"); 55 } 56 else { 57 DecimalFormat formatter = new DecimalFormat ("#,##0.0"); 58 buf.append(formatter.format((double)numBytes / (1024.0 * 1024.0))).append(" MB"); 59 } 60 } 61 return buf; 62 } 63 64 71 public Object parseObject(String source, ParsePosition pos) { 72 return null; 73 } 74 } | Popular Tags |