JAVA cyclic redundancy check (CRC32) __java

Source: Internet
Author: User
Tags crc32
CRC32 Introduction

CRC Verification Utility Library in the field of data storage and data communication, in order to ensure the correct data, we have to use the means of error detection. CRC is one of the most famous in many error detection methods. CRC is the full name of the cyclic redundancy check.
CRC32 Error detection ability is very strong, small overhead, easy to use encoder and circuit implementation. Judging from its ability to detect errors, it can not find the probability of error is only less than 0.0047%. Both performance and cost considerations are far superior to parity and arithmetic and checksum. Thus, in the field of data storage and data communication, CRC is everywhere: the famous Communication protocol X.25 FCS (frame error sequence) using the Crc-ccitt,arj, LHA and other compression tools software used is CRC32, disk drives read and write using the CRC16, General image Storage Format GIF, TIFF, etc. also use CRC as a means of error detection. 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:
724585211

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.