A Checksum is used for error checking while transferring a file. Data flow across the network in the form of packets. So, checksum is a computed value that is dependent on the contents of a file. For each packet the computed checksum will be different. This computed value is transmitted with the packet when it is transmitted. The receiving system checks the checksum and on the basis of checksum it receives and rejects the packet. It is mainly used where it becomes necessary to check the packets before accepting it.
Adler32: It is a error checking technique used to check the accuracy of data while transmitting it to the other end. Errors are checked while the data is transferring. This scheme is as reliable as CRC32, but it is much faster to compute.
To make a program on Checksum first of all make a class
named ChecksumAdler32. Inside the class declare one method checkSum(String file)
which will take a value of a file which has to pass at run time. Now make a
object of FileInputStream, CheckedInputStream and Adler32 and pass the instance of
FileInputStream, Adler32 into the constructor of CheckedInputStream
calculate the size of the file call the method length() of File class. We have
define a array of type byte, we are taking the size of the array (1024), i.e. the size of each
packet. The Checksum for each packet will be generated randomly by the
CheckedInputStream class. It returns the long data type. Now define a main
method inside which we will call checkSum() method which will give us the
checksum, size of the file and name of the file.
To achieve the desired result we have used the following classes and methods.
FileInputStream: It is a class of java.io package. It extends InputStream class. It is used for reading in byte form.
CheckedInputStream: It is a input stream that keeps the checksum. Its constructor use two parameter, first is InputStream and second is Checksum.
Adler32: It is a class of java.util.zip package. It implements Checksum. This class is used to calculate the Adler-32 of the stream.
length(): It is a method of File class. It returns the length of the file.
read(): It is used for reading a byte.
getChecksum(): It returns the Checksum.
getValue(): It is a method of Checksum interface. It returns the checksum value.
Code of the program is given below:
The output of the program is given below:
Create one txt file for which you need to
calculate the checksum in the root directory.
974346075 958 hello.txt
We have create a txt file name hello in the root directory. If the size of the text file is less than or equals to 1024 bytes then only one packet will be generated. If the size of the file is greater than 1024 byte than the more than one packet will be generated each having different checksum value. The output will show the checksum value, the size of the file and the name of the file.
4146884724 1109 hello.txt
1926860616 1109 hello.txt
As the size of the file is greater than 1024 so, it is making more than one packet.
We have 1000s of tutorials on our website. Search Tutorials tutorials on our website.
Posted on: June 7, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles