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