emul/mini/src/main/java/java/util/zip/CRC32.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 30 Jan 2013 14:03:49 +0100
branchemul
changeset 611 9839e9a75bcf
parent 609 48ef38e9677e
child 640 693745d01b55
permissions -rw-r--r--
Implementation of ZipInputStream
jaroslav@609
     1
/*
jaroslav@609
     2
 * Copyright (c) 1996, 2005, Oracle and/or its affiliates. All rights reserved.
jaroslav@609
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@609
     4
 *
jaroslav@609
     5
 * This code is free software; you can redistribute it and/or modify it
jaroslav@609
     6
 * under the terms of the GNU General Public License version 2 only, as
jaroslav@609
     7
 * published by the Free Software Foundation.  Oracle designates this
jaroslav@609
     8
 * particular file as subject to the "Classpath" exception as provided
jaroslav@609
     9
 * by Oracle in the LICENSE file that accompanied this code.
jaroslav@609
    10
 *
jaroslav@609
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@609
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@609
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jaroslav@609
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@609
    15
 * accompanied this code).
jaroslav@609
    16
 *
jaroslav@609
    17
 * You should have received a copy of the GNU General Public License version
jaroslav@609
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@609
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@609
    20
 *
jaroslav@609
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@609
    22
 * or visit www.oracle.com if you need additional information or have any
jaroslav@609
    23
 * questions.
jaroslav@609
    24
 */
jaroslav@609
    25
jaroslav@609
    26
package java.util.zip;
jaroslav@609
    27
jaroslav@609
    28
/**
jaroslav@609
    29
 * A class that can be used to compute the CRC-32 of a data stream.
jaroslav@609
    30
 *
jaroslav@609
    31
 * @see         Checksum
jaroslav@609
    32
 * @author      David Connelly
jaroslav@609
    33
 */
jaroslav@609
    34
public
jaroslav@609
    35
class CRC32 implements Checksum {
jaroslav@611
    36
    private int crc = 0xFFFFFFFF;
jaroslav@609
    37
jaroslav@609
    38
    /**
jaroslav@609
    39
     * Creates a new CRC32 object.
jaroslav@609
    40
     */
jaroslav@609
    41
    public CRC32() {
jaroslav@609
    42
    }
jaroslav@609
    43
jaroslav@609
    44
jaroslav@609
    45
    /**
jaroslav@609
    46
     * Updates the CRC-32 checksum with the specified byte (the low
jaroslav@609
    47
     * eight bits of the argument b).
jaroslav@609
    48
     *
jaroslav@609
    49
     * @param b the byte to update the checksum with
jaroslav@609
    50
     */
jaroslav@609
    51
    public void update(int b) {
jaroslav@611
    52
        byte[] arr = { (byte)b };
jaroslav@611
    53
        update(arr);
jaroslav@609
    54
    }
jaroslav@609
    55
jaroslav@609
    56
    /**
jaroslav@609
    57
     * Updates the CRC-32 checksum with the specified array of bytes.
jaroslav@609
    58
     */
jaroslav@609
    59
    public void update(byte[] b, int off, int len) {
jaroslav@609
    60
        if (b == null) {
jaroslav@609
    61
            throw new NullPointerException();
jaroslav@609
    62
        }
jaroslav@609
    63
        if (off < 0 || len < 0 || off > b.length - len) {
jaroslav@609
    64
            throw new ArrayIndexOutOfBoundsException();
jaroslav@609
    65
        }
jaroslav@609
    66
        crc = updateBytes(crc, b, off, len);
jaroslav@609
    67
    }
jaroslav@609
    68
jaroslav@609
    69
    /**
jaroslav@609
    70
     * Updates the CRC-32 checksum with the specified array of bytes.
jaroslav@609
    71
     *
jaroslav@609
    72
     * @param b the array of bytes to update the checksum with
jaroslav@609
    73
     */
jaroslav@609
    74
    public void update(byte[] b) {
jaroslav@609
    75
        crc = updateBytes(crc, b, 0, b.length);
jaroslav@609
    76
    }
jaroslav@609
    77
jaroslav@609
    78
    /**
jaroslav@609
    79
     * Resets CRC-32 to initial value.
jaroslav@609
    80
     */
jaroslav@609
    81
    public void reset() {
jaroslav@609
    82
        crc = 0;
jaroslav@609
    83
    }
jaroslav@609
    84
jaroslav@609
    85
    /**
jaroslav@609
    86
     * Returns CRC-32 value.
jaroslav@609
    87
     */
jaroslav@609
    88
    public long getValue() {
jaroslav@609
    89
        return (long)crc & 0xffffffffL;
jaroslav@609
    90
    }
jaroslav@609
    91
jaroslav@611
    92
    // XXX: taken from 
jaroslav@611
    93
    // http://introcs.cs.princeton.edu/java/51data/CRC32.java.html
jaroslav@611
    94
    private static int updateBytes(int crc, byte[] arr, int off, int len) {
jaroslav@611
    95
        int poly = 0xEDB88320;   // reverse polynomial
jaroslav@611
    96
jaroslav@611
    97
        while (len-- > 0) {
jaroslav@611
    98
            byte b = arr[off++];
jaroslav@611
    99
            int temp = (crc ^ b) & 0xff;
jaroslav@611
   100
jaroslav@611
   101
            // read 8 bits one at a time
jaroslav@611
   102
            for (int i = 0; i < 8; i++) {
jaroslav@611
   103
                if ((temp & 1) == 1) {
jaroslav@611
   104
                    temp = (temp >>> 1) ^ poly;
jaroslav@611
   105
                } else {
jaroslav@611
   106
                    temp = (temp >>> 1);
jaroslav@611
   107
                }
jaroslav@611
   108
            }
jaroslav@611
   109
            crc = (crc >>> 8) ^ temp;
jaroslav@611
   110
        }
jaroslav@611
   111
        return crc ^ 0xffffffff;
jaroslav@611
   112
    }
jaroslav@609
   113
}