Tuesday, June 5, 2007

Java Utils

Some of my past works include the use of various JDK API:


Adler32 API Usage:
What is this API used for ?
Checksum is the answer. To compute the Adler-32 checksum of a data stream.
We need this in order to know if a data stream has changed in time.
Set the checksum and then read the file again to see if the checksum has changed. if so , meaning file has changed.
Check the code here.


String fileContents = null;
long adlerCheckSumValue = 0l;
// read the file contents into fileContents as a string.
Adler32 adlerCheckSum = new Adler32();
checkSum.update(fileContents.getBytes());
adlerCheckSumValue = adlerCheckSum .getValue();

OR

//get the check sum value from the given input stream.

CheckedInputStream cis = new CheckedInputStream(inputStream, adlerCheckSum );
while (cis.read() != -1) {
//Do nothing here
}

adlerCheckSumValue = adlerCheckSum .getValue();







Simple log4j logging configuration for simple utilities:
The Pattern logs this way :

[ INFO ]:: 2007-06-26 17:42:42,001:: com.xxx.xxx.xx.XXXXX.main(XXXXX.java:147):: This is the log message...


String pattern = "[ %p ]";
pattern += ":: %d";
pattern += ":: %l";
pattern += ":: %m %n";

PatternLayout layout = new PatternLayout(pattern);
FileAppender appender = new FileAppender(layout, filePath);
Logger logger = Logger.getLogger(theClass);
logger.addAppender(appender);

filepath is the absolute path with log file name.
theClass is the class object.