1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18package java.util.zip;
19
20
21/**
22 * The CRC32 class is used to compute a CRC32 checksum from data provided as
23 * input value.
24 *
25 * @since Android 1.0
26 */
27public class CRC32 implements java.util.zip.Checksum {
28
29    private long crc = 0L;
30
31    long tbytes = 0L;
32
33    /**
34     * Returns the CRC32 checksum for all input received.
35     *
36     * @return The checksum for this instance.
37     * @since Android 1.0
38     */
39    public long getValue() {
40        return crc;
41    }
42
43    /**
44     * Resets the CRC32 checksum to it initial state.
45     *
46     * @since Android 1.0
47     */
48    public void reset() {
49        tbytes = crc = 0;
50
51    }
52
53    /**
54     * Updates this checksum with the byte value provided as integer.
55     *
56     * @param val
57     *            represents the byte to update the checksum.
58     * @since Android 1.0
59     */
60    public void update(int val) {
61        crc = updateByteImpl((byte) val, crc);
62    }
63
64    /**
65     * Updates this checksum with the bytes contained in buffer {@code buf}.
66     *
67     * @param buf
68     *            the buffer holding the data to update the checksum with.
69     * @since Android 1.0
70     */
71    public void update(byte[] buf) {
72        update(buf, 0, buf.length);
73    }
74
75    /**
76     * Updates this checksum with n bytes of data obtained from buffer {@code
77     * buf}, starting at offset {@code off}.
78     *
79     * @param buf
80     *            the buffer to update the checksum.
81     * @param off
82     *            the offset in {@code buf} to obtain data from.
83     * @param nbytes
84     *            the number of bytes to read from {@code buf}.
85     * @since Android 1.0
86     */
87    public void update(byte[] buf, int off, int nbytes) {
88        // avoid int overflow, check null buf
89        if (off <= buf.length && nbytes >= 0 && off >= 0
90                && buf.length - off >= nbytes) {
91            tbytes += nbytes;
92            crc = updateImpl(buf, off, nbytes, crc);
93        } else {
94            throw new ArrayIndexOutOfBoundsException();
95        }
96    }
97
98    private native long updateImpl(byte[] buf, int off, int nbytes, long crc1);
99
100    private native long updateByteImpl(byte val, long crc1);
101}
102