MDi JLib
1.7.4

com.microdeveloper.data
Class CRC

java.lang.Object
  extended by java.util.zip.CRC32
      extended by com.microdeveloper.data.CRC
All Implemented Interfaces:
Checksum

public class CRC
extends CRC32

A class that makes it easier to create CRC codes.

This class replaces update(int) because the CRC32.update(int) considers only the first byte. This class also considers all four bytes of the int and adds update(String), update(byte), update(char), update(long), update(double), update(float), update(boolean), and getInt31() which returns a 31 bit unsigned value converted from CRC32.getValue(). The original CRC32.update(byte[]), CRC32.getValue() and CRC32.reset() methods are still available.

Example:

 

class Employee {

private String name ; private String ssn ; private int salary ;

public int hashCode() { CRC c = new CRC(); c.update( name ); c.update( ssn ); c.update( salary ); return c.getInt31(); }

}

Since:
1.7.4
Version:
1.0
Author:
MicroDeveloper, Inc.

Constructor Summary
CRC()
           
 
Method Summary
 int getInt31()
          get the resulting CRC value as an unsigned 31 bit value.
static int quick(String s)
          Get the CRC of a string.
 void update(boolean b)
          work a boolean into the existing CRC code.
 void update(byte b)
          work a byte into the existing CRC code.
 void update(char c)
          work a char into the existing CRC code.
 void update(double d)
          work a double into the existing CRC code.
 void update(float f)
          work a float into the existing CRC code.
 void update(int i)
          work an int into the existing CRC code.
 void update(long i)
          work a long into the existing CRC code.
 void update(String s)
          work a string into the existing CRC code.
 
Methods inherited from class java.util.zip.CRC32
getValue, reset, update, update
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CRC

public CRC()
Method Detail

update

public void update(int i)
work an int into the existing CRC code.

Specified by:
update in interface Checksum
Overrides:
update in class CRC32
Parameters:
i - an integer to work into the CRC code. All four bytes are worked in.


update

public void update(byte b)
work a byte into the existing CRC code.

Parameters:
b - a byte to work into the CRC code. All eight bits are worked in.


update

public void update(char c)
work a char into the existing CRC code.

Parameters:
c - a char to work into the CRC code. Both bytes are worked in.


update

public void update(long i)
work a long into the existing CRC code.

Parameters:
i - a long integer to work into the CRC code. All eight bytes are worked in.


update

public void update(double d)
work a double into the existing CRC code.

Parameters:
d - a double precision float to work into the CRC code. All eight bytes are worked in.


update

public void update(float f)
work a float into the existing CRC code.

Parameters:
f - a floating point value to work into the CRC code. All four bytes are worked in.


update

public void update(boolean b)
work a boolean into the existing CRC code.

Parameters:
b - a boolean to work into the CRC code. true is worked in as a byte of value 1 and false is worked in as a byte of value 0.


update

public void update(String s)
work a string into the existing CRC code.

Parameters:
s - a string to work into the CRC code. Two bytes for every character is worked in. The length is not directly worked in.


getInt31

public int getInt31()
get the resulting CRC value as an unsigned 31 bit value.

Use getValue() to get the real CRC value.

This is useful because it avoids the problems some code might have with a negative value. Plus, it makes for a clean and consistant conversion to an int as used for hashCode().

Returns:
an unsigned 31 bit CRC value (a 32 bit CRC with the sign bit chopped off).


quick

public static int quick(String s)
Get the CRC of a string.

Returns:
an unsigned 31 bit CRC value (a 32 bit CRC with the sign bit chopped off).


MDi JLib
1.7.4

Copyright©2001-2007 MicroDeveloper, Inc. All Rights Reserved.