public class CRC extends Object
Using src is easy. Here is an example of calculating CCITT crc in one call using canonical approach.
String data = "123456789";
long ccittCrc = CRC.calculateCRC(CRC.Parameters.CCITT, data.getBytes());
System.out.printf("CRC is 0x%04X\n", ccittCrc); // prints "CRC is 0x29B1"
For larger data, table driven implementation is faster. Here is how to use it.
String data = "123456789";
CRC tableDriven = new CRC(CRC.Parameters.XMODEM);
long xmodemCrc = tableDriven.calculateCRC(data.getBytes());
System.out.printf("CRC is 0x%04X\n", xmodemCrc); // prints "CRC is 0x31C3"
You can also reuse CRC object instance for another crc calculation.
Given that the only state for a CRC calculation is the "intermediate value" and it is stored in your code, you can even use same CRC instance to calculate CRC of multiple data sets in parallel. And if data is too big, you may feed it in chunks
long curValue = tableDriven.init(); // initialize intermediate value
curValue = tableDriven.update(curValue, "123456789".getBytes()); // feed first chunk
curValue = tableDriven.update(curValue, "01234567890".getBytes()); // feed next chunk
long xmodemCrc2 = tableDriven.finalCRC(curValue); // gets CRC of whole data ("12345678901234567890")
System.out.printf("CRC is 0x%04X\n", xmodemCrc2); // prints "CRC is 0x2C89"
| Modifier and Type | Class and Description |
|---|---|
static class |
CRC.Algorithm
Parameters represents set of parameters defining a particular CRC algorithm.
|
static class |
CRC.Parameters |
| Constructor and Description |
|---|
CRC(CRC.Algorithm algorithm)
Constructs a new CRC processor for table based CRC calculations.
|
CRC(CRC.Parameters crcParams) |
| Modifier and Type | Method and Description |
|---|---|
long |
calculateCRC(byte[] data)
A convenience method allowing to calculate CRC in one call.
|
static long |
calculateCRC(CRC.Algorithm algorithm,
byte[] data) |
static long |
calculateCRC(CRC.Algorithm algorithm,
byte[] data,
int offset,
int length) |
static long |
calculateCRC(CRC.Parameters crcParams,
byte[] data) |
static long |
calculateCRC(CRC.Parameters crcParams,
byte[] data,
int offset,
int length)
This method implements simple straight forward bit by bit calculation.
|
long |
finalCRC(long curValue)
This method should be called to retrieve actual CRC for the data processed so far.
|
short |
finalCRC16(long curValue)
Is a convenience method to spare end users from explicit type conversion every time this package is used.
|
int |
finalCRC32(long curValue)
Is a convenience method to spare end users from explicit type conversion every time this package is used.
|
byte |
finalCRC8(long curValue)
Is a convenience method to spare end users from explicit type conversion every time this package is used.
|
long |
init()
Returns initial value for this CRC intermediate value
This method is used when starting a new iterative CRC calculation (using init, update
and finalCRC methods, possibly supplying data in chunks).
|
long |
update(long curValue,
byte[] chunk)
A convenience method for feeding a complete byte array of data.
|
long |
update(long curValue,
byte[] chunk,
int offset,
int length)
This method is used to feed data when performing iterative CRC calculation (using init, update
and finalCRC methods, possibly supplying data in chunks).
|
public CRC(CRC.Algorithm algorithm)
algorithm - CRC algorithm parametersRuntimeException - if CRC sum width is not divisible by 8public CRC(CRC.Parameters crcParams)
public static long calculateCRC(CRC.Algorithm algorithm, byte[] data)
public static long calculateCRC(CRC.Algorithm algorithm, byte[] data, int offset, int length)
public static long calculateCRC(CRC.Parameters crcParams, byte[] data)
public static long calculateCRC(CRC.Parameters crcParams, byte[] data, int offset, int length)
Note: this implementation follows section 8 ("A Straightforward CRC Implementation") of Ross N. Williams paper as even though final/sample implementation of this algorithm provided near the end of that paper (and followed by most other implementations) is a bit faster, it does not work for polynomials shorter then 8 bits.
crcParams - CRC algorithm parametersdata - data for the CRC calculationpublic long init()
public long update(long curValue,
byte[] chunk,
int offset,
int length)
curValue - CRC intermediate value so farchunk - data chunk to b processed by this calloffset - is 0-based offset of the data to be processed in the array suppliedlength - indicates number of bytes to be processed.public long update(long curValue,
byte[] chunk)
curValue - CRC intermediate value so farchunk - data chunk to b processed by this callpublic long finalCRC(long curValue)
curValue - CRC intermediate value so farpublic long calculateCRC(byte[] data)
data - is data to calculate CRC onpublic byte finalCRC8(long curValue)
curValue - current intermediate crc state valueRuntimeException - if crc being calculated is not 8-bitpublic short finalCRC16(long curValue)
curValue - current intermediate crc state valueRuntimeException - if crc being calculated is not 16-bitpublic int finalCRC32(long curValue)
curValue - current intermediate crc state valueRuntimeException - if crc being calculated is not 32-bitCopyright © 2018. All rights reserved.