001: /*
002: * @(#)CRC32.java 1.35 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: package java.util.zip;
029:
030: /**
031: * A class that can be used to compute the CRC-32 of a data stream.
032: *
033: * @see Checksum
034: * @version 1.25, 05/03/00
035: * @author David Connelly
036: */
037: public class CRC32 implements Checksum {
038: private int crc;
039:
040: /*
041: * Loads the ZLIB library.
042: */
043: private static native void init();
044:
045: static {
046: java.security.AccessController
047: .doPrivileged(new sun.security.action.LoadLibraryAction(
048: "zip"));
049: /* Work-around for Symbian tool bug. No longer needed. */
050: init();
051: }
052:
053: /**
054: * Creates a new CRC32 object.
055: */
056: public CRC32() {
057: }
058:
059: /**
060: * Updates CRC-32 with specified byte.
061: */
062: public void update(int b) {
063: crc = update(crc, b);
064: }
065:
066: /**
067: * Updates CRC-32 with specified array of bytes.
068: */
069: public void update(byte[] b, int off, int len) {
070: if (b == null) {
071: throw new NullPointerException();
072: }
073: if (off < 0 || len < 0 || off > b.length - len) {
074: throw new ArrayIndexOutOfBoundsException();
075: }
076: crc = updateBytes(crc, b, off, len);
077: }
078:
079: /**
080: * Updates checksum with specified array of bytes.
081: *
082: * @param b the array of bytes to update the checksum with
083: */
084: public void update(byte[] b) {
085: crc = updateBytes(crc, b, 0, b.length);
086: }
087:
088: /**
089: * Resets CRC-32 to initial value.
090: */
091: public void reset() {
092: crc = 0;
093: }
094:
095: /**
096: * Returns CRC-32 value.
097: */
098: public long getValue() {
099: return (long) crc & 0xffffffffL;
100: }
101:
102: private native static int update(int crc, int b);
103:
104: private native static int updateBytes(int crc, byte[] b, int off,
105: int len);
106: }
|