Calculating the checksum of a Byte Array by using CRC32

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 CRC32.
CRC32: 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.
byte: 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.
ByteArray: It is a Wrapper of array of Bytes,
extending class Object.
To calculate the Checksum of the Byte Array by using
CRC32 we first need to create a class ChecksumByteArrayCRC32. 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 CRC32
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.
CRC32: This class extends Object and
implements Checksum interface. It is a class of java.util.zip
package. This class computes the CRC32 checksum of the stream.
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.
Return type is of long primitive type.
The code of the program is given below:
import java.util.zip.CheckedInputStream;
import java.util.zip.CRC32;
import java.io.*;
public class ChecksumByteArrayCRC{
public static void main(String[] args){
try{
String string = new String("A" + "\u00ea" + "\u00f1" + "\u00fc" + "C");
System.out.println("The data size is " + string.length());
byte buffer[] = string.getBytes();
ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
CheckedInputStream cis = new CheckedInputStream(bais, new CRC32());
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 that the size of data is
5 and the size of the data is also 5, so only one packet is generated and the
checksum is generated for that packet. The size of the array can be decrease or
increase according to the needs of the programmer. If the size of the data is
greater than 5 the number of packets will also increase each having its own
checksum value.
C:\java>java ChecksumByteArrayCRC
The data size is 5
The value of checksum is 4188635332
C:\java> |
The output of the program if the data size is greater
than 5.
C:\java>java ChecksumByteArrayCRC
The data size is 7
The value of checksum is 4188635332
The value of checksum is 2856980254
C:\java> |
Download this
example.

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