Wednesday, January 19, 2011

File size conversions utility B, MB, KB, GB, TB etc and get file size in readable format


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));
}
}

Monday, January 10, 2011

TPTP and Static Code review Tools for eclipse


In software development process code review is overhead because it may be emanated from coders' low skill level , vague requirement , introduction of new to technology , bad old code , bad processes .Often the onus of code reviews comes on shoulder of team lead.There are two of code reviews like manual - peer review, external code review etc and automated / static code review .

Static code review can ensure good coding practices like style , indentation , unused methods , unused variables recursive calls etc .Too much rely on it may cause to ignore pitfalls due to thread safety , memory leakage , inefficient loops etc .

As there are many free plug-ins we can rely on them so that coders comply for usual Java rules .But too much automation always leads to problem like ignoring serious flaws in code - design pattern ,concurrent modification etc as mentioned .

Commit in memory that automation of every thing is always bad , for example street collapsed due to automation freak managers who contributed to automatic artificial intelligence for trading went wrong badly spiraling in utter disaster .

So automated code review is just to ensure good practices in JAVA it can not become replacement for manual code review .

Ultimately coding is art and if coder is not good at it , then automated tool can not do anything for serious errors in design patterns , memory leakage etc .



NOTE : If you are in firewall enabled network don't forget proxy setting for that

Windows >> General >> Network Connection >> fill the setting , give login password as required in the network

For installing plugin through update manger .


Help >> Install New Software .. >> click on "Add" >> Copy paste the URL given in following table >> next things are intuitive

Must have eclipse(helios plugin ) plugins


Feature

Description

Reference Site


Eclipse Update Site

Find Bug

Static code analysis

http://andrei.gmxhome.de/findbugs/index.html


http://findbugs.cs.umd.edu/eclipse

PMD

Programming Mistake Detector

http://pmd.sourceforge.net/integrations.html


http://pmd.sf.net/eclipse

Check Style

Coding standard analysis

http://checkstyle.sourceforge.net/


http://eclipse-cs.sourceforge.net/update/

ReviewClipse

Making comment for code review

http://www.inso.tuwien.ac.at/projects/reviewclipse/installation/


http://www.inso.tuwien.ac.at/projects/reviewclipse/update-site

Jdepend

Package dependency analysis

http://www.clarkware.com/software/JDepend.html


http://andrei.gmxhome.de/eclipse/

Metrics

Code Complexity monitoring

http://metrics.sourceforge.net/


http://metrics.sourceforge.net/update

Memory Analsysis

Performance analysis
Memory leakage , time execution analysis etc.

http://www.eclipse.org/tptp/


Eclipse Helios >>Help > Install New Software >> Test and Performance >>TPTP update


courtesy : NILESH SALPE