2 ******************************************************************************
3 * Copyright (C) 2003-2010, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 ******************************************************************************
8 package com.ibm.icu.dev.tool.localeconverter;
10 import java.math.BigInteger;
13 * The code is from http://www.theorem.com/java/CRC32.java
14 * Calculates the CRC32 - 32 bit Cyclical Redundancy Check
15 * <P> This check is used in numerous systems to verify the integrity
16 * of information. It's also used as a hashing function. Unlike a regular
17 * checksum, it's sensitive to the order of the characters.
18 * It produces a 32 bit
20 * @author Michael Lecuyer (mjl@theorem.com)
21 * @version 1.1 August 11, 1998
24 /* ICU is not endian portable, because ICU data generated on big endian machines can be
25 * ported to big endian machines but not to little endian machines and vice versa. The
26 * conversion is not portable across platforms with different endianess.
29 public class CalculateCRC32 {
30 static int CRCTable[];
33 static void buildCRCTable() {
34 final int CRC32_POLYNOMIAL = 0xEDB88320;
37 CRCTable = new int[256];
39 for (i = 0; i <= 255; i++) {
41 for (j = 8; j > 0; j--) {
43 crc = (crc >>> 1) ^ CRC32_POLYNOMIAL;
52 public static int computeCRC32(String buffer) {
53 return computeCRC32(buffer, 0xFFFFFFFF);
56 public static int computeCRC32(byte buffer[]) {
57 return computeCRC32(buffer, 0xFFFFFFFF);
60 public static int computeCRC32(String buffer, int crc){
61 return computeCRC32(buffer.getBytes(), crc);
64 public static int computeCRC32(byte buffer[], int crc) {
65 return computeCRC32(buffer, 0, buffer.length, crc);
68 public static int computeCRC32(byte buffer[], int start, int count, int lastcrc){
75 temp1 = cachedCRC >>> 8;
77 temp2 = CRCTable[(cachedCRC ^s) & 0xFF];
78 cachedCRC = temp1 ^ temp2;
83 public byte [] toBytes() {
84 return new BigInteger(new Integer(cachedCRC).toString()).toByteArray();