程序包 org.coodex.util

类 CRC

java.lang.Object
org.coodex.util.CRC

public class CRC extends Object
This class provides utility functions for CRC calculation using either canonical straight forward approach or using "fast" table-driven implementation. Note, that even though table-driven implementation is much faster for processing large amounts of data and is commonly referred as fast algorithm, sometimes it might be quicker to calculate CRC using canonical algorithm then to prepare the table for table-driven implementation.

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 enum 
    Parameters 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 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
    This method should be called to retrieve actual CRC for the data processed so far.
    short
    Is a convenience method to spare end users from explicit type conversion every time this package is used.
    int
    Is a convenience method to spare end users from explicit type conversion every time this package is used.
    byte
    Is a convenience method to spare end users from explicit type conversion every time this package is used.
    long
    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(byte[] chunk)
    A convenience method for feeding a complete byte array of data.
    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).

    从类继承的方法 java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 构造器详细资料

    • CRC

      public CRC(CRC.Algorithm algorithm)
      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

      public CRC(CRC.Parameters crcParams)
  • 方法详细资料

    • calculateCRC

      public static long calculateCRC(CRC.Algorithm algorithm, byte[] data)
    • calculateCRC

      public static long calculateCRC(CRC.Algorithm algorithm, byte[] data, int offset, int length)
    • calculateCRC

      public static long calculateCRC(CRC.Parameters crcParams, byte[] data)
    • calculateCRC

      public static long calculateCRC(CRC.Parameters crcParams, byte[] data, int offset, int length)
      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 parameters
      data - 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 call
      offset - is 0-based offset of the data to be processed in the array supplied
      length - 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