
clime.messadmin.utils.BytesFormat Maven / Gradle / Ivy
The newest version!
/**
*
*/
package clime.messadmin.utils;
import java.text.FieldPosition;
import java.text.NumberFormat;
import java.text.ParsePosition;
import java.util.Locale;
/**
* Formats a number of bytes, adding a suffix
* @author Cédrik LIME
*/
public class BytesFormat extends NumberFormat {
protected static final int DEFAULT_FRACTIONS_DIGITS = 2;
protected static final double KB = 1024;
protected static final double MB = KB * 1024;
protected static final double GB = MB * 1024;
protected static final double TB = GB * 1024;
protected static final String B_STR = "B";
protected static final String KB_STR = "KB";
protected static final String MB_STR = "MB";
protected static final String GB_STR = "GB";
protected static final String TB_STR = "TB";
protected String B_suffix;
protected String KB_suffix;
protected String MB_suffix;
protected String GB_suffix;
protected String TB_suffix;
protected Locale locale;
/**
*
*/
public BytesFormat(Locale locale, String separator) {
super();
this.locale = locale;
if (separator == null) {
separator = "";
}
B_suffix = separator + B_STR;
KB_suffix = separator + KB_STR;
MB_suffix = separator + MB_STR;
GB_suffix = separator + GB_STR;
TB_suffix = separator + TB_STR;
}
public static BytesFormat getBytesInstance(String separator) {
return new BytesFormat(Locale.getDefault(), separator); // super-class is not thread-safe
}
public static BytesFormat getBytesInstance(Locale locale, String separator) {
return new BytesFormat(locale, separator); // super-class is not thread-safe
}
/**
* {@inheritDoc}
*/
public StringBuffer format(double number, StringBuffer toAppendTo, FieldPosition pos) {
if (number > Long.MAX_VALUE) {
throw new IllegalArgumentException("Can not format double numbers greater than Long.MAX_VALUE");//$NON-NLS-1$
}
return format((long)number, toAppendTo, pos);
}
/**
* {@inheritDoc}
*/
public StringBuffer format(long number, StringBuffer toAppendTo, FieldPosition pos) {
bytesToHumanString(number, DEFAULT_FRACTIONS_DIGITS, toAppendTo);
return toAppendTo;
}
/**
* {@inheritDoc}
*/
public Number parse(String text, ParsePosition parsePosition) {
throw new UnsupportedOperationException();
}
/**
* Smart formatter for bytes
* @param bytes
* @param toAppendTo buffer where to put the formated, human-readable String
*/
protected void bytesToHumanString(long bytes, int fractionsDigits, StringBuffer toAppendTo) {
if (bytes < 0) {
toAppendTo.append(bytes);
return;
}
double result = bytes;
String suffix = "";//$NON-NLS-1$
// Could use a ChoiceFormat() + MessageFormat() instead,
// but this is faster
if (bytes < KB) {
result = bytes;
suffix = B_suffix;
} else if (bytes >= KB && bytes < MB) {
result = round(bytes / KB, fractionsDigits);
suffix = KB_suffix;
} else if (bytes >= MB && bytes < GB) {
result = round(bytes / MB, fractionsDigits);
suffix = MB_suffix;
} else if (bytes >= GB && bytes < TB) {
result = round(bytes / GB, fractionsDigits);
suffix = GB_suffix;
} else {
result = round(bytes / TB, fractionsDigits);
suffix = TB_suffix;
}
NumberFormat nf = NumberFormat.getNumberInstance(locale);
nf.setMinimumFractionDigits(0);
nf.setMaximumFractionDigits(fractionsDigits);
nf.setGroupingUsed(super.isGroupingUsed());
toAppendTo.append(nf.format(result)).append(suffix);
}
private double round(double value, int fractionsDigits) {
double powValue = Math.pow(10, fractionsDigits);
return Math.round(value * powValue) / powValue;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy