Calculating the checksum of a Byte Array by using Adler32

In this section, you will learn to calculating the checksum of a byte array by using Adler32.

Calculating the checksum of a Byte Array by using Adler32

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.