类 CRC
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"
-
嵌套类概要
嵌套类修饰符和类型类说明static enumParameters represents set of parameters defining a particular CRC algorithm.static class -
构造器概要
构造器构造器说明CRC(CRC.Algorithm algorithm) Constructs a new CRC processor for table based CRC calculations.CRC(CRC.Parameters crcParams) -
方法概要
修饰符和类型方法说明static longcalculateCRC(CRC.Algorithm algorithm, byte[] data) static longcalculateCRC(CRC.Algorithm algorithm, byte[] data, int offset, int length) static longcalculateCRC(CRC.Parameters crcParams, byte[] data) static longcalculateCRC(CRC.Parameters crcParams, byte[] data, int offset, int length) This method implements simple straight forward bit by bit calculation.longfinalCRC()This method should be called to retrieve actual CRC for the data processed so far.shortIs a convenience method to spare end users from explicit type conversion every time this package is used.intIs a convenience method to spare end users from explicit type conversion every time this package is used.byteIs a convenience method to spare end users from explicit type conversion every time this package is used.longinit()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).longupdate(byte[] chunk) A convenience method for feeding a complete byte array of data.longupdate(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).
-
构造器详细资料
-
CRC
Constructs a new CRC processor for table based CRC calculations. Underneath, it just calls finalCRC() method.- 参数:
algorithm- CRC algorithm parameters- 抛出:
RuntimeException- if CRC sum width is not divisible by 8
-
CRC
-
-
方法详细资料
-
calculateCRC
-
calculateCRC
-
calculateCRC
-
calculateCRC
This method implements simple straight forward bit by bit calculation. It is relatively slow for large amounts of data, but does not require any preparation steps. As a result, it might be faster in some cases then building a table required for faster calculation.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 calculation- 返回:
- the CRC value of the data provided
-
init
public 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).- 返回:
- initial value for this CRC intermediate value
-
update
public long update(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). It can be called multiple times per CRC calculation to feed data to be processed in chunks.- 参数:
chunk- 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.- 返回:
- updated intermediate value for this CRC
-
update
public long update(byte[] chunk) A convenience method for feeding a complete byte array of data.- 参数:
chunk- data chunk to b processed by this call- 返回:
- updated intermediate value for this CRC
-
finalCRC
public long finalCRC()This method should be called to retrieve actual CRC for the data processed so far.- 返回:
- calculated CRC
-
finalCRC8
public byte finalCRC8()Is a convenience method to spare end users from explicit type conversion every time this package is used. Underneath, it just calls finalCRC() method.- 返回:
- the final CRC value
- 抛出:
RuntimeException- if crc being calculated is not 8-bit
-
finalCRC16
public short finalCRC16()Is a convenience method to spare end users from explicit type conversion every time this package is used. Underneath, it just calls finalCRC() method.- 返回:
- the final CRC value
- 抛出:
RuntimeException- if crc being calculated is not 16-bit
-
finalCRC32
public int finalCRC32()Is a convenience method to spare end users from explicit type conversion every time this package is used. Underneath, it just calls finalCRC() method.- 返回:
- the final CRC value
- 抛出:
RuntimeException- if crc being calculated is not 32-bit
-