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
 
 
Search All Tutorials
  

 
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
 
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

Calculating the checksum of a Byte Array by using Adler32

                         

A Checksum is used for error checking while transferring a file. We know that data flows 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. In this example we are calculating a value of a Byte Array using Adler32.

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.

The byte is an 8- bit signed primitive data type quantity. The minimum value the byte is -128 and maximum value is +128. It is signed because it can take both negative and positive value. The Byte class wraps a value of primitive type byte in an object. A object of type Byte contains a single field whose type is byte.

To calculate the Checksum of the Byte Array by using Adler32 we first need to create a class ChecksumByteArray. Inside the class declare a main method. Inside the main method make one object of String class and pass some information in it. We can see the size of a String by using the length method, so that it becomes easy to know how much packets will be generated. Use the method getBytes to get the bytes from the string and store it in an array of type byte. Now create a object of ByteArrayInputStream class and pass the array of type byte in the constructor of class ByteArrayInputStream. This class has an buffer that contains bytes that may be read from the stream. This class extends InputStream. Now create an object of class CheckedInputStream and pass the instances of ByteArrayInputStream class and Adler32 class in the constructor of CheckedInputStream class. Now create a new array of type byte, the array size we have  taken is 5, you can change it according to your own needs. Use the while loop to read the byte from the array. Store the checksum you get in the long primitive type.

In this example we have used following classes and methods.

ByteArrayInputStream: This class extends InputStream. This class has a byte buffer that reads from the stream. 

CheckedInputStream: This class extends FilterInputStream class. This class maintains a checksum of the data being read. 

Adler32: This class extends Object and implements Checksum interface. This class computes the Alder-32 checksum of the stream. It is computed much faster than CRC-32.

length(): It will return the size of the string. 

read(): It is a method of CheckedInputStream class. This method reads a byte.

getChecksum: It is a method of CheckedInputStream class, which returns the Checksum of the stream. 

getValue(): It returns the checksum value. It is of long type. 

The code of  the program is given below:

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

public class ChecksumByteArray{
  public static void main(String[] args){
    try
      String string = new String(
 
"A" "\u00ea" "\u00f1" "\u00fc" "C" "A" "\u00ea" "\u00f1");
      System.out.println("The data size is " + string.length())
      byte buffer[] = string.getBytes();
      ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
      CheckedInputStream cis = new CheckedInputStream(bais, new Adler32());
      byte readBuffer[] new byte[5];
      while (cis.read(readBuffer>= 0){
        long value = cis.getChecksum().getValue();
        System.out.println("The value of checksum is " + value);
      }
    }
    catch(Exception e){
      System.out.println("Exception has been caught" + e);
    }
  }
}

The output of the program is given below.

In the output we can see the number of packets generated and each packet will have its own checksum value. In the example, the number of packets generated are 2 as we have declared the size of the array as 5 and the size of the actual data is 8, so 2 packets will be generated each having its own checksum value. The size of the array can be set according to the needs of the programmer.

C:\java>java ChecksumByteArray
The data size is 8
The value of checksum is 167773020
The value of checksum is 396100984

C:\java>

Download this example.

                         

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

Current Comments

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

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.

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

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

Copyright © 2008. All rights reserved.