[an error occurred while processing this directive]

Calculating the checksum of a file/Adler32

                         

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 class. To 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:

import java.io.*;
import java.util.zip.CheckedInputStream;
import java.util.zip.Adler32;

public class ChecksumAdler32 {
  public void checkSum(String file){
    try{
      FileInputStream fis = null;
      CheckedInputStream cis = null;
      Adler32 adler = null;
      long sizeOfFile = 0;
      try{
        fis = new FileInputStream(file);  
        adler = new Adler32();
        cis = new CheckedInputStream(fis, adler);
        sizeOfFile = new File(file).length();
       }
      catch (Exception e){
        System.out.println("File  Not found ");
        System.exit(1);
      }
      byte[] buffer = new byte[1024];
      //can change the size according to needs      
     
while(cis.read(buffer)>=0){
        long checksum = cis.getChecksum().getValue();
        System.out.println(checksum + " " + sizeOfFile + " " + file);
      }
    }
    catch(IOException e){
      System.out.println("The exception has been thrown:" + e);
      System.exit(1);
    }
  }
  public static void main(String[] args) {
    ChecksumAdler32 adler = new ChecksumAdler32();
    if(args.length!= 1){
      System.out.println("Please enter the valid file name : " );
    }
    else{
      adler.checkSum(args[0]);
    }
  }
}

The output of the program is given below:

Create one txt file for which you need to calculate the checksum in the root directory.

C:\java>java   ChecksumAdler32   hello.txt
974346075   958    hello.txt

C:\java>

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

C:\java>java ChecksumAdler32  hello.txt
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.

Download this program.

                         

[an error occurred while processing this directive]