2007-12-01 00:00:00 +00:00
|
|
|
/*
|
2010-05-25 15:58:33 -07:00
|
|
|
* Copyright (c) 1996, 2005, Oracle and/or its affiliates. All rights reserved.
|
2007-12-01 00:00:00 +00:00
|
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
|
|
*
|
|
|
|
* This code is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License version 2 only, as
|
2010-05-25 15:58:33 -07:00
|
|
|
* published by the Free Software Foundation. Oracle designates this
|
2007-12-01 00:00:00 +00:00
|
|
|
* particular file as subject to the "Classpath" exception as provided
|
2010-05-25 15:58:33 -07:00
|
|
|
* by Oracle in the LICENSE file that accompanied this code.
|
2007-12-01 00:00:00 +00:00
|
|
|
*
|
|
|
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* version 2 for more details (a copy is included in the LICENSE file that
|
|
|
|
* accompanied this code).
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License version
|
|
|
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
2010-05-25 15:58:33 -07:00
|
|
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
|
|
* or visit www.oracle.com if you need additional information or have any
|
|
|
|
* questions.
|
2007-12-01 00:00:00 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
package java.util.zip;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A class that can be used to compute the Adler-32 checksum of a data
|
|
|
|
* stream. An Adler-32 checksum is almost as reliable as a CRC-32 but
|
|
|
|
* can be computed much faster.
|
|
|
|
*
|
|
|
|
* @see Checksum
|
|
|
|
* @author David Connelly
|
|
|
|
*/
|
|
|
|
public
|
|
|
|
class Adler32 implements Checksum {
|
|
|
|
private int adler = 1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new Adler32 object.
|
|
|
|
*/
|
|
|
|
public Adler32() {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-06-29 19:57:58 -07:00
|
|
|
* Updates the checksum with the specified byte (the low eight
|
|
|
|
* bits of the argument b).
|
2007-12-01 00:00:00 +00:00
|
|
|
*
|
2009-06-29 19:57:58 -07:00
|
|
|
* @param b the byte to update the checksum with
|
2007-12-01 00:00:00 +00:00
|
|
|
*/
|
|
|
|
public void update(int b) {
|
|
|
|
adler = update(adler, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-06-29 19:57:58 -07:00
|
|
|
* Updates the checksum with the specified array of bytes.
|
2007-12-01 00:00:00 +00:00
|
|
|
*/
|
|
|
|
public void update(byte[] b, int off, int len) {
|
|
|
|
if (b == null) {
|
|
|
|
throw new NullPointerException();
|
|
|
|
}
|
|
|
|
if (off < 0 || len < 0 || off > b.length - len) {
|
|
|
|
throw new ArrayIndexOutOfBoundsException();
|
|
|
|
}
|
|
|
|
adler = updateBytes(adler, b, off, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-06-29 19:57:58 -07:00
|
|
|
* Updates the checksum with the specified array of bytes.
|
|
|
|
*
|
|
|
|
* @param b the byte array to update the checksum with
|
2007-12-01 00:00:00 +00:00
|
|
|
*/
|
|
|
|
public void update(byte[] b) {
|
|
|
|
adler = updateBytes(adler, b, 0, b.length);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-06-29 19:57:58 -07:00
|
|
|
* Resets the checksum to initial value.
|
2007-12-01 00:00:00 +00:00
|
|
|
*/
|
|
|
|
public void reset() {
|
|
|
|
adler = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-06-29 19:57:58 -07:00
|
|
|
* Returns the checksum value.
|
2007-12-01 00:00:00 +00:00
|
|
|
*/
|
|
|
|
public long getValue() {
|
|
|
|
return (long)adler & 0xffffffffL;
|
|
|
|
}
|
|
|
|
|
|
|
|
private native static int update(int adler, int b);
|
|
|
|
private native static int updateBytes(int adler, byte[] b, int off,
|
|
|
|
int len);
|
|
|
|
}
|