CRC check utility library In the field of data storage and data communication, in order to ensure the accuracy of data, error detection methods have to be used. The following article mainly introduces you to the cyclic redundancy check in Java ( Friends who need it can refer to the relevant information on the implementation of CRC32). Let’s take a look below.
Preface
This article mainly introduces to you the relevant content about Java's implementation of cyclic redundancy check (CRC32), and shares it with you. Reference study, not much to say below, let’s take a look at the detailed introduction.
Introduction to CRC32
CRC check utility library In the field of data storage and data communication, in order to ensure the accuracy of the data, it has to be used means of error detection. Among many error detection methods, CRC is the most famous one. The full name of CRC is cyclic redundancy check.
CRC32 has extremely strong error detection capabilities, low overhead, and is easy to implement with encoders and detection circuits. Judging from its error detection capabilities, the probability of errors it cannot detect is only less than 0.0047%. In terms of performance and cost, it is far superior to parity check and arithmetic sum check. Therefore, in the fields of data storage and data communication, CRC is everywhere: the FCS (Frame Error Detection Sequence) of the famous communication protocol X.25 uses CRC-CCITT, compression tool software such as ARJ and LHA use CRC32, and disk The drive uses CRC16 for reading and writing, and common image storage formats such as GIF and TIFF also use CRC as an error detection method.
CRC implementation
##
package com.jianggujin.codec; import java.io.IOException; import java.io.InputStream; import java.util.zip.CRC32; /** * CRC32 * * @author jianggujin * */ public class HQCRC32 { private static HQCRC32 crc32 = new HQCRC32(); public static HQCRC32 getInstance() { return crc32; } private HQCRC32() { } private static final int STREAM_BUFFER_LENGTH = 1024; public long encrypt(byte[] data) { CRC32 crc32 = new CRC32(); crc32.update(data); return crc32.getValue(); } public long encrypt(InputStream data) throws IOException { final byte[] buffer = new byte[STREAM_BUFFER_LENGTH]; int read = data.read(buffer, 0, STREAM_BUFFER_LENGTH); CRC32 crc32 = new CRC32(); while (read > -1) { crc32.update(buffer, 0, read); read = data.read(buffer, 0, STREAM_BUFFER_LENGTH); } return crc32.getValue(); } }
Test code:
import org.junit.Test; import com.jianggujin.codec.HQCRC32; public class CRC32Test { HQCRC32 crc32 = HQCRC32.getInstance(); @Test public void encode() { byte[] data = "jianggujin".getBytes(); long result = crc32.encrypt(data); System.err.println(result); } }
Test results:
Summary
The above is the detailed content of Introduction to the implementation of cyclic redundancy check in Java. For more information, please follow other related articles on the PHP Chinese website!