Share on Google+Share on Google+

Calculating the checksum of a file/Adler32

A Checksum is used for error checking while transferring a file.

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 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 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:


public class ChecksumAdler32 {
  public void checkSum(String file){
  FileInputStream fis = null;
  CheckedInputStream cis = null;
  Adler32 adler = null;
  long sizeOfFile = 0;
  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 ");
  byte[] buffer = new byte[1024];
  //can change the size according to needs  
  long checksum = cis.getChecksum().getValue();
  System.out.println(checksum + " " + sizeOfFile + " " + file);
  catch(IOException e){
  System.out.println("The exception has been thrown:" + e);
  public static void main(String[] args) {
  ChecksumAdler32 adler = new ChecksumAdler32();
  if(args.length!= 1){
  System.out.println("Please enter the valid file name : " );

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


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.


Posted on: June 7, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Calculating the checksum of a file/Adler32   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image