Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML


 
  
 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 

 
Facing Programming Problem?
Ask Questions?, Browse Latest Questions, Question-Answer Guidelines
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

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.

                         

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 
Latest Searches:
login form swing
dojo dialog for onCanc
timer example in j2me
binary
html.ppt
jasper struts
Flash Tutorial : Parti
аŠ?а?аŠ?Å Â
make
B Tree
how to develop a sampl
jdbc with sql
code to store a string
html code for developi
code for changing pass
jdbc with oraclw sql
convert text file to i
.getCurrencyInstance
gui
bean:define tags
Photoshop Effects Part
edit table mysql jsp
tree
jasperreport
POI and excel
php oops
jsf cascade dropdown
create .csv file using
java method generics
Text Field Midlet Exam
how to read key value
maven
java combo box
TCP buffer
importing class in to
Boolean
spring internation
PHP5
scroll bar
registration connect t
ibatis sample
delete statement form
csv reader
hiding message box in
UML Overview
codes in applet
insert data to text fi
jasper report
Refresh a Web Page Usi
Collection Interface
super class
SWING TIME CLOCK
Linux Caixa M?????????
How togive jdbc:odbc c
ResourceBundle in ja
default constructor
html textarea
Action Errors
HTML Tables Advanced T
password java databa
arraylist in javascrip
Jini's relevance emerg
Aะà¸??ะàÂ
composite key in mysql
what is ResourceBundle
log4j.properties
sending a mail through
password and user name
????�ப�?????????
download from server u
Struts File Download E
Photoshop Effects Simp
t:panelGroup
The Struts ActionForm
java script
Photoshop Textures and
mutator method
save image to file
session validation in
populate drop down fro
Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

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