Java Checksum Calculate checksum(byte[] buf, int off, int len)

Here you can find the source of checksum(byte[] buf, int off, int len)

Description

checksum

License

Apache License

Declaration

public final static int checksum(byte[] buf, int off, int len) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2013 Zhang Zhuo(william@TinyGameX.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License./*ww  w .  j a v  a2s . co m*/
 *******************************************************************************/

public class Main {
    private static int[] crc_t;

    public final static int checksum(byte[] buf, int off, int len) {
        return update(buf, off, len) ^ 0xFFFFFFFF;
    }

    private final static int update(byte[] buf, int off, int len) {
        int c = 0xFFFFFFFF;
        int n;
        if (crc_t == null)
            mk();
        for (n = off; n < len + off; n++) {
            c = crc_t[(c ^ buf[n]) & 0xFF] ^ (c >>> 8);
        }
        return c;
    }

    private final static void mk() {
        int c, k;
        if (crc_t == null)
            crc_t = new int[256];
        for (int n = 0; n < 256; n++) {
            c = n;
            for (k = 0; k < 8; k++)
                c = (c & 1) == 1 ? 0xEDB88320 ^ (c >>> 1) : c >>> 1;
            crc_t[n] = c;
        }
    }
}

Related

  1. checkSum(boolean[] a)
  2. checkSum(byte abyte0[])
  3. checksum(byte current, final byte[] data, final int offset, final int length)
  4. checkSum(byte value)
  5. checkSum(byte[] b, int offset, int length)
  6. checksum(byte[] data)
  7. checksum(byte[] message, int offset, int count)
  8. checksum(File file)
  9. checksum(File file)