Class Crc16

java.lang.Object
org.apache.commons.codec.digest.Crc16
All Implemented Interfaces:
Checksum

public final class Crc16 extends Object implements Checksum
CRC-16 checksum implementations you can customize with a table and init value.

Since there are so many CRC-16 variants, we do not pick a default.

For example, to create a create a custom variant of CRC16-MODBUS with an init value of 0x0000, use:

 Checksum crc16 = CRC16.builder().setTable(CRC16.getModbusTable()).setInit(0x0000).get();
 
Since:
1.20.0
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final class 
    Builds Crc16 instances.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static final int[]
     
    private static final int
     
    private static final int[]
     
    private static final int
     
    private int
    CRC.
    private static final int[]
     
    private static final int
     
    private static final int
     
    private static final int[]
     
    private static final int
     
    private static final int
     
    private final int
     
    private static final int[]
     
    private static final int
     
    private static final int
     
    private static final int[]
     
    private static final int
     
    private static final int[]
     
    private static final int
     
    private static final int[]
     
    private static final int
     
    private final int[]
     
    private static final int[]
     
    private static final int
     
    private static final int
     
    private final int
     
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    Constructs a new instance.
  • Method Summary

    Modifier and Type
    Method
    Description
    static Crc16
    arc()
    Creates a new CRC16-CCITT Checksum.
    Creates a new builder.
    static Crc16
    Creates a new CRC16-CCITT Checksum.
    static Crc16
    dnp()
    Creates a new CRC16-DNP Checksum.
    static int[]
    Gets a copy of the CRC16-CCITT table.
    static int[]
    Gets a copy of the CRC16-CCITT table.
    static int[]
    Gets a copy of the CRC16-DNP table.
    static int[]
    Gets a copy of the CRC16-IBM-SDLC table.
    static int[]
    Gets a copy of the CRC16-MAXIM table.
    static int[]
    Gets a copy of the CRC16-MCRF4XX table.
    static int[]
    Gets a copy of the CRC16-MODBUS table.
    static int[]
    Gets a copy of the CRC16-NRSC-5 table.
    long
     
    static Crc16
    Creates a new CRC16-IBM-SDLC Checksum.
    static Crc16
    Creates a new instance for CRC16-MAXIM Checksum.
    static Crc16
    Creates a new instance for CRC16-MCRF4XX Checksum.
    static Crc16
    Creates a new instance for CRC16-MODBUS Checksum.
    static Crc16
    Creates a new instance for CRC16-NRSC-5 Checksum.
    void
     
     
    void
    update(byte[] b, int off, int len)
     
    void
    update(int b)
     
    static Crc16
    usb()
    Creates a new instance for CRC16-USB Checksum.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • ARC

      private static final int[] ARC
    • ARC_INIT

      private static final int ARC_INIT
      See Also:
    • CCITT

      private static final int[] CCITT
    • CCITT_INIT

      private static final int CCITT_INIT
      See Also:
    • DNP

      private static final int[] DNP
    • DNP_INIT

      private static final int DNP_INIT
      See Also:
    • DNP_XOROUT

      private static final int DNP_XOROUT
      See Also:
    • IBM_SDLC

      private static final int[] IBM_SDLC
    • IBM_SDLC_INIT

      private static final int IBM_SDLC_INIT
      See Also:
    • IBM_SDLC_XOROUT

      private static final int IBM_SDLC_XOROUT
      See Also:
    • MAXIM

      private static final int[] MAXIM
    • MAXIM_INIT

      private static final int MAXIM_INIT
      See Also:
    • MAXIM_XOROUT

      private static final int MAXIM_XOROUT
      See Also:
    • MCRF4XX

      private static final int[] MCRF4XX
    • MCRF4XX_INIT

      private static final int MCRF4XX_INIT
      See Also:
    • MODBUS

      private static final int[] MODBUS
    • MODBUS_INIT

      private static final int MODBUS_INIT
      See Also:
    • NRSC5

      private static final int[] NRSC5
    • NRSC5_INIT

      private static final int NRSC5_INIT
      See Also:
    • USB

      private static final int[] USB
    • USB_INIT

      private static final int USB_INIT
      See Also:
    • USB_XOROUT

      private static final int USB_XOROUT
      See Also:
    • crc

      private int crc
      CRC.
    • init

      private final int init
    • table

      private final int[] table
    • xorOut

      private final int xorOut
  • Constructor Details

    • Crc16

      private Crc16(Crc16.Builder builder)
      Constructs a new instance.
  • Method Details

    • arc

      public static Crc16 arc()
      Creates a new CRC16-CCITT Checksum.
      • The init value is 0x0000.
      • The XorOut value is 0x0000.

      Also known as:

      • CRC-16/ARC
      • ARC
      • CRC-16
      • CRC-16/LHA
      • CRC-IBM
      Returns:
      a new CRC16-CCITT Checksum.
    • builder

      public static Crc16.Builder builder()
      Creates a new builder.

      Since there are so many CRC-16 variants, we do not pick a default.

      Returns:
      a new builder.
    • ccitt

      public static Crc16 ccitt()
      Creates a new CRC16-CCITT Checksum.
      • The init value is 0x0000.
      • The XorOut value is 0x0000.

      Also known as:

      • CRC-16/KERMIT
      • CRC-16/BLUETOOTH
      • CRC-16/CCITT
      • CRC-16/CCITT-TRUE
      • CRC-16/V-41-LSB
      • CRC-CCITT
      • KERMIT
      Returns:
      a new CRC16-CCITT Checksum.
    • dnp

      public static Crc16 dnp()
      Creates a new CRC16-DNP Checksum.
      • The init value is 0x0000.
      • The XorOut value is 0xFFFF.
      Returns:
      a new CRC16-DNP Checksum.
    • getArcTable

      public static int[] getArcTable()
      Gets a copy of the CRC16-CCITT table.
      Returns:
      a copy of the CCRC16-CITT table.
    • getCcittTable

      public static int[] getCcittTable()
      Gets a copy of the CRC16-CCITT table.
      Returns:
      a copy of the CCRC16-CITT table.
    • getDnpTable

      public static int[] getDnpTable()
      Gets a copy of the CRC16-DNP table.
      Returns:
      a copy of the CCRC16-DNP table.
    • getIbmSdlcTable

      public static int[] getIbmSdlcTable()
      Gets a copy of the CRC16-IBM-SDLC table.
      Returns:
      a copy of the CRC16-IBM-SDLC table.
    • getMaximTable

      public static int[] getMaximTable()
      Gets a copy of the CRC16-MAXIM table.
      Returns:
      a copy of the CRC16-MAXIM table.
    • getMcrf4xxTable

      public static int[] getMcrf4xxTable()
      Gets a copy of the CRC16-MCRF4XX table.
      Returns:
      a copy of the CRC16-MCRF4XX table.
    • getModbusTable

      public static int[] getModbusTable()
      Gets a copy of the CRC16-MODBUS table.
      Returns:
      a copy of the CRC16-MODBUS table.
    • getNrsc5Table

      public static int[] getNrsc5Table()
      Gets a copy of the CRC16-NRSC-5 table.
      Returns:
      a copy of the CRC16-NRSC-5 table.
    • ibmSdlc

      public static Crc16 ibmSdlc()
      Creates a new CRC16-IBM-SDLC Checksum.
      • The init value is 0xFFFF.
      • The XorOut value is 0xFFFF.

      Also known as:

      • CRC-16/IBM-SDLC
      • CRC-16/ISO-HDLC
      • CRC-16/ISO-IEC-14443-3-B
      • CRC-16/X-25
      • CRC-B
      • X-25
      Returns:
      a new CRC16-IBM-SDLC Checksum.
    • maxim

      public static Crc16 maxim()
      Creates a new instance for CRC16-MAXIM Checksum.

      CRC-16 checksum implementation based on polynomial x<sup>16</spu> + x^15 + x^2 + 1 (0x8005).

      • The init value is 0xFFFF.
      • The XorOut value is 0xFFFF.

      Also known as:

      • CRC-16/MAXIM-DOW
      Returns:
      a new CRC16-MAXIM Checksum.
    • mcrf4xx

      public static Crc16 mcrf4xx()
      Creates a new instance for CRC16-MCRF4XX Checksum.
      • The init value is 0xFFFF.
      • The XorOut value is 0x0000.
      Returns:
      a new CRC16-MCRF4XX Checksum.
    • modbus

      public static Crc16 modbus()
      Creates a new instance for CRC16-MODBUS Checksum.

      CRC-16 checksum implementation based on polynomial x<sup>16</spu> + x^15 + x^2 + 1 (0x8005).

      • The init value is 0xFFFF.
      • The XorOut value is 0x0000.

      Also known as:

      • CRC-16/MODBUST
      • MODBUST
      Returns:
      a new CRC16-MODBUS Checksum.
    • nrsc5

      public static Crc16 nrsc5()
      Creates a new instance for CRC16-NRSC-5 Checksum.
      • The init value is 0xFFFF.
      • The XorOut value is 0x0000.
      Returns:
      a new CRC16-NRSC-5 Checksum.
    • usb

      public static Crc16 usb()
      Creates a new instance for CRC16-USB Checksum.
      • The init value is 0xFFFF.
      • The XorOut value is 0xFFFF.
      Returns:
      a new CRC16-USB Checksum.
    • getValue

      public long getValue()
      Specified by:
      getValue in interface Checksum
    • reset

      public void reset()
      Specified by:
      reset in interface Checksum
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • update

      public void update(byte[] b, int off, int len)
      Specified by:
      update in interface Checksum
    • update

      public void update(int b)
      Specified by:
      update in interface Checksum