Many time we need to display uploaded /downloaded
document in readable format for end user For that create a static utility class
and stack it with such methods
Before this I checked for "Apache Commons" they have FileUtils , FileNameUtils etc but no FileSizeUtil .
I suggest that they should consider it to include this utility with more methods as required to operate on file size .Following class can be made more generic by passing argument for units like 1KB =1024 Byte or 1 KB = 1000 Byte .
/**
* @author NILESH SALPE
* Generic class for usable static methods
*
*/
public class SalpeFileSizeUtil
{
//Initialize variables
public static final String _TB = "TB";
public static final String _GB = "GB";
public static final String _MB = "MB";
public static final String _KB = "KB";
public static final String _B = "B";
private static final long KB = 1024;
private static final long MB = KB * KB;
private static final long GB = MB * KB;
private static final long TB = GB * KB;
private static final String _NUMBER_FORMAT = "#,###.###";
private static final LinkedHashMap<String, Integer> lookUpMap = new LinkedHashMap<String, Integer>()
{
{
put(_B, Integer.valueOf(0));
put(_KB, Integer.valueOf(1));
put(_MB, Integer.valueOf(2));
put(_GB, Integer.valueOf(3));
put(_TB, Integer.valueOf(4));
}
};
private static final double[][] loolUp = new double[lookUpMap.size()][lookUpMap
.size()];
private static boolean lookUpLoad = false;
// load look up table on class load
static
{
double outBase = 1;
for (int i = 0; i < loolUp.length; i++)
{
double base = Math.pow((double) 1024, ((double) 1 / (double) 3));
int innerPow = 0;
for (int j = 0; j < loolUp.length; j++)
{
loolUp[i][j] = Math.pow(base, innerPow) / outBase;
innerPow = innerPow + 3;
}
outBase = (double) 1 / ((double) outBase * (double) 1024);
}
}
/*Convert bytes in readable format like if
/* input is 1024 it will return String 1 KB
*/
public static String formatSize(final double valueInByte)
{
final double valueInByteAbs = Math.abs(valueInByte);
final long[] dividers = new long[]
{ TB, GB, MB, KB, 1 };
final String[] units = new String[]
{ _TB, _GB, _MB, _KB, _B };
if (0 <= valueInByte && valueInByte < 1)
{
return new DecimalFormat(_NUMBER_FORMAT).format(valueInByte) + " "
+ units[4];
}
String result = null;
for (int i = 0; i < dividers.length; i++)
{
final long divider = dividers[i];
if (valueInByteAbs >= divider)
{
result = format(valueInByte, divider, units[i]);
break;
}
}
return result;
}
private static String format(final double value, final long divider,
final String unit)
{
final double result = divider > 1 ? (double) value / (double) divider
: (double) value;
return new DecimalFormat(_NUMBER_FORMAT).format(result) + " " + unit;
}
//Convert one type of size in another
public static double convert(double quotaByte, String from, String to)
{
return loolUp[lookUpMap.get(from).intValue()][lookUpMap.get(to).intValue()];
}
// Test main
public static void main(String[] args)
{
System.out.println(convert(100,SalpeFileSizeUtil .
_KB,
SalpeFileSizeUtil .
_MB));
}
}
No comments:
Post a Comment