com.javaranch.common
Class CRC

java.lang.Object
  |
  +--java.util.zip.CRC32
        |
        +--com.javaranch.common.CRC
All Implemented Interfaces:
java.util.zip.Checksum

public class CRC
extends java.util.zip.CRC32

A class that makes it easier to create CRC codes.

This class inherits java.util.zip.CRC32 - so the real power is there. This class replaces update(int) because the CRC32.update(int) considers only the first byte. This class considers all four bytes of the int.

Further, this class adds update(String), update(byte), update(char), update(long), update(double), update(float) and update(boolean) for convenience.

This class also adds 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();
           }

       }

    
- - - - - - - - - - - - - - - - -

Copyright (c) 1998-2004 Paul Wheaton

You are welcome to do whatever you want to with this source file provided that you maintain this comment fragment (between the dashed lines). Modify it, change the package name, change the class name ... personal or business use ... sell it, share it ... add a copyright for the portions you add ...

My goal in giving this away and maintaining the copyright is to hopefully direct developers back to JavaRanch.

The original source can be found at JavaRanch

- - - - - - - - - - - - - - - - -

Author:
Paul Wheaton

Constructor Summary
CRC()
           
 
Method Summary
 int getInt31()
          get the resulting CRC value as an unsigned 31 bit value.
static int quick(java.lang.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(java.lang.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 java.util.zip.Checksum
Overrides:
update in class java.util.zip.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(java.lang.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(java.lang.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).



Copyright ©2004 Paul Wheaton All Rights Reserved