|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--java.util.zip.CRC32 | +--com.javaranch.common.CRC
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
- - - - - - - - - - - - - - - - -
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 |
public CRC()
Method Detail |
public void update(int i)
update
in interface java.util.zip.Checksum
update
in class java.util.zip.CRC32
i
- an integer to work into the CRC code. All four bytes are worked in.public void update(byte b)
b
- a byte to work into the CRC code. All eight bits are worked in.public void update(char c)
c
- a char to work into the CRC code. Both bytes are worked in.public void update(long i)
i
- a long integer to work into the CRC code. All eight bytes are worked in.public void update(double d)
d
- a double precision float to work into the CRC code. All eight bytes are worked in.public void update(float f)
f
- a floating point value to work into the CRC code. All four bytes are worked in.public void update(boolean b)
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.public void update(java.lang.String s)
s
- a string to work into the CRC code. Two bytes for every character is worked in.
The length is not directly worked in.public int getInt31()
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().
public static int quick(java.lang.String s)
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |