All Downloads are FREE. Search and download functionalities are using the official Maven repository.

clime.messadmin.utils.BytesFormat Maven / Gradle / Ivy

Go to download

Notification system and Session administration for J2EE Web Applications

There is a newer version: 4.1.1
Show 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 = 1;

	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 Locale locale;

	/**
	 * 
	 */
	public BytesFormat(Locale locale) {
		super();
		this.locale = locale;
	}

	public static BytesFormat getBytesInstance() {
		return new BytesFormat(Locale.getDefault()); // super-class is not thread-safe
	}
	public static BytesFormat getBytesInstance(Locale locale) {
		return new BytesFormat(locale); // 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");
		}
		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 = "";
		// Could use a ChoiceFormat() + MessageFormat() instead,
		// but this is faster
		if (bytes < KB) {
			result = bytes;
			suffix = B_STR;
		} else if (bytes >= KB && bytes < MB) {
			result = round(bytes / KB, fractionsDigits);
			suffix = KB_STR;
		} else if (bytes >= MB && bytes < GB) {
			result = round(bytes / MB, fractionsDigits);
			suffix = MB_STR;
		} else if (bytes >= GB && bytes < TB) {
			result = round(bytes / GB, fractionsDigits);
			suffix = GB_STR;
		} else {
			result = round(bytes / TB, fractionsDigits);
			suffix = TB_STR;
		}
		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 - 2024 Weber Informatics LLC | Privacy Policy