net.sf.jzeno.util
Class CRC32

java.lang.Object
  extended by net.sf.jzeno.util.CRC32

public class CRC32
extends java.lang.Object

Calculates the CRC32 - 32 bit Cyclical Redundancy Check

This check is used in numerous systems to verify the integrity of information. It's also used as a hashing function. Unlike a regular checksum, it's sensitive to the order of the characters. It produces a 32 bit (Java int.

This Java programme was translated from a C version I had written.

This software is in the public domain.

When calculating the CRC32 over a number of strings or byte arrays the previously calculated CRC is passed to the next call. In this way the CRC is built up over a number of items, including a mix of strings and byte arrays.

 CRC32 crc = new CRC32();
 int crcCalc = crc.crc32("Hello World");
 crcCalc = crc.crc32("How are you?", crcCalc);
 crcCalc = crc.crc32("I'm feeling really good, how about you?", crcCalc);
 
The line int crcCalc = crc.crc32("Hello World"); is equivalent to int crcCalc = crc.crc32("Hello World", -1);. When starting a new CRC calculation the "previous crc" is set to 0xFFFFFFFF (or -1).

The table only needs to be built once. You may use it to generate many CRC's.


Constructor Summary
CRC32()
          Constructor constructs the lookup table.
 
Method Summary
 int crc32(byte[] buffer)
          Convenience method for generating a CRC from a byte array.
 int crc32(byte[] buffer, int crc)
          Convenience method for generating a CRC from a series of byte arrays.
 int crc32(byte[] buffer, int start, int count, int lastcrc)
          General CRC generation function.
 int crc32(java.lang.String buffer)
          Convenience mithod for generating a CRC from a single String.
 int crc32(java.lang.String buffer, int crc)
          Convenience method for generating a CRC from a series of String's.
static void main(java.lang.String[] args)
          Tests CRC32.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CRC32

public CRC32()
Constructor constructs the lookup table.

Method Detail

main

public static void main(java.lang.String[] args)
Tests CRC32.
Eg: java CRC32 "Howdy, I'm A Cowboy"

Parameters:
args - the string used to calculate the CRC32

crc32

public int crc32(java.lang.String buffer)
Convenience mithod for generating a CRC from a single String.

Parameters:
buffer - string to generate the CRC32
Returns:
32 bit CRC

crc32

public int crc32(byte[] buffer)
Convenience method for generating a CRC from a byte array.

Parameters:
buffer - byte array to generate the CRC32
Returns:
32 bit CRC

crc32

public int crc32(java.lang.String buffer,
                 int crc)
Convenience method for generating a CRC from a series of String's.

Parameters:
buffer - string to generate the CRC32
crc - previously generated CRC32.
Returns:
32 bit CRC

crc32

public int crc32(byte[] buffer,
                 int crc)
Convenience method for generating a CRC from a series of byte arrays.

Parameters:
buffer - byte array to generate the CRC32
crc - previously generated CRC32.
Returns:
32 bit CRC

crc32

public int crc32(byte[] buffer,
                 int start,
                 int count,
                 int lastcrc)
General CRC generation function.

Parameters:
buffer - byte array to generate the CRC32
start - byte start position
count - number of byte's to include in CRC calculation
crc - previously generated CRC32.
Returns:
32 bit CRC