001: /*
002: * @(#)Adler32.java 1.31 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 Adler-32 checksum of a data
032: * stream. An Adler-32 checksum is almost as reliable as a CRC-32 but
033: * can be computed much faster.
034: *
035: * @see Checksum
036: * @version 1.23, 05/03/00
037: * @author David Connelly
038: */
039: public class Adler32 implements Checksum {
040: private int adler = 1;
041:
042: /*
043: * Loads the ZLIB library.
044: */
045: static {
046: java.security.AccessController
047: .doPrivileged(new sun.security.action.LoadLibraryAction(
048: "zip"));
049: }
050:
051: /**
052: * Creates a new Adler32 object.
053: */
054: public Adler32() {
055: }
056:
057: /**
058: * Updates checksum with specified byte.
059: *
060: * @param b an array of bytes
061: */
062: public void update(int b) {
063: adler = update(adler, b);
064: }
065:
066: /**
067: * Updates checksum 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: adler = updateBytes(adler, b, off, len);
077: }
078:
079: /**
080: * Updates checksum with specified array of bytes.
081: */
082: public void update(byte[] b) {
083: adler = updateBytes(adler, b, 0, b.length);
084: }
085:
086: /**
087: * Resets checksum to initial value.
088: */
089: public void reset() {
090: adler = 1;
091: }
092:
093: /**
094: * Returns checksum value.
095: */
096: public long getValue() {
097: return (long) adler & 0xffffffffL;
098: }
099:
100: private native static int update(int adler, int b);
101:
102: private native static int updateBytes(int adler, byte[] b, int off,
103: int len);
104: }
|