Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Calculating the Checksum of the file/CRC32 
 

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.

 

Calculating the Checksum of the file/CRC32

                         

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

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

public class ChecksumCRC32 {
  public void checkSum(String file){
    try{
      FileInputStream fis = null;
      CheckedInputStream cis = null;
      CRC32 crc = null;
      long sizeOfFile = 0;
      try{
        fis = new FileInputStream(file);  
        crc = new CRC32();
        cis = new CheckedInputStream(fis, crc);
        sizeOfFile = new File(file).length();
       }
      catch (Exception e){
        System.out.println("File  Not found ");
        System.exit(1);
      }
      byte[] buffer = new byte[100];
      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) {
    ChecksumCRC32 crc = new ChecksumCRC32();
    if(args.length!= 1){
      System.out.println("Please enter the valid file name : " );
    }
    else{
      crc.checkSum(args[0]);
    }
  }
}

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 file. 

C:\java>java ChecksumCRC32  javalearner.txt
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

Download this program.

                         

» View all related tutorials
Related Tags: c file array class list ui lists method get name using this oo root example where to exam drive store

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

5 comments so far (
post your own) View All Comments Latest 10 Comments:

i want an example of a small project

Posted by Balambiga on Tuesday, 09.16.08 @ 16:32pm | #79848

hi sir i need an example of entering the name,age and selecting city and software and ok and cancel buttons please make it soon

Posted by swetha on Tuesday, 09.16.08 @ 09:46am | #79783


i need a java applet code to generate the folling output
enter the Name:
enter the city:
select city:
ok and cancel button

Posted by swetha on Tuesday, 09.16.08 @ 09:43am | #79782

hi sir i need an example of entering the name,age and selecting city and software and ok and cancel buttons please make it soon

Posted by swetha on Tuesday, 09.16.08 @ 09:40am | #79781

I want Example of calculating checksum from a file bytes using CRC32

Posted by chandan ghag on Friday, 09.12.08 @ 20:07pm | #78933

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.