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.
CRC: CRC means Cyclic Redundancy Check. 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. CRC performs some type of calculation before transmitting the data and send the result to the other end. The other end repeats the same operation before accepting the data. If both the devices get the result same, it means the transmission is error free.
To make a program on Checksum first of all make a class
named CheckSumCRC32. 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 CRC32 and pass the instance of
FileInputStream, CRC32 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, the size of the array is 100, 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.
CRC32: It is a class of java.util.zip package. It implements Checksum. This class is used to calculate the CRC-32 of the stream.
length(): It is a method of File class. It returns the length of the file.
read(): It reads a byte.
getChecksum(): It returns the Checksum.
getValue(): It is a method of Checksum interface. It returns the checksum value.
The code of the program is given below.
The output of the program is given below.
In the output we can see that first we have
created a file javalearner.txt in the java folder and pass some information in
it. We can see that for each packet a checksum is calculated. The size of each
packet is 100 bytes. It can be changed. The output will show 100 checksum value
as 10 packets are generated. In the output firstly we are displaying the
checksum of the packet, secondly the size of the file and lastly the name of the
566064419 958 javalearner.txt
3839930655 958 javalearner.txt
3892475745 958 javalearner.txt
3357861592 958 javalearner.txt
4227549807 958 javalearner.txt
459090179 958 javalearner.txt
3273102972 958 javalearner.txt
1119235310 958 javalearner.txt
2705113445 958 javalearner.txt
695211703 958 javalearner.txt
Rs. 20,000 US$ 300
Today: Rs. 10,000 US$150
Course Duration: 30 hrs
Posted on: December 19, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles