01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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:
18: package java.util.zip;
19:
20: /**
21: * The CRC32 class is used to compute a CRC32 Checksum from a set of data.
22: */
23: public class CRC32 implements java.util.zip.Checksum {
24:
25: private long crc = 0L;
26:
27: long tbytes = 0L;
28:
29: /**
30: * Returns the CRC32 Checksum for all input received.
31: *
32: * @return The checksum for this instance
33: */
34: public long getValue() {
35: return crc;
36: }
37:
38: /**
39: * Returns the CRC32 checksum to it initial state.
40: */
41: public void reset() {
42: tbytes = crc = 0;
43:
44: }
45:
46: /**
47: * Updates this Checksum with value val
48: */
49: public void update(int val) {
50: crc = updateByteImpl((byte) val, crc);
51: }
52:
53: /**
54: * Updates this Checksum with the bytes contained in buffer buf.
55: *
56: * @param buf
57: * Buffer to update Checksum
58: */
59: public void update(byte[] buf) {
60: update(buf, 0, buf.length);
61: }
62:
63: /**
64: * Updates this Checksum with nbytes of data from buffer buf, starting at
65: * offset off.
66: *
67: * @param buf
68: * Buffer to update Checksum
69: * @param off
70: * Offset in buf to obtain data from
71: * @param nbytes
72: * Number of bytes to read from buf
73: */
74: public void update(byte[] buf, int off, int nbytes) {
75: // avoid int overflow, check null buf
76: if (off <= buf.length && nbytes >= 0 && off >= 0
77: && buf.length - off >= nbytes) {
78: tbytes += nbytes;
79: crc = updateImpl(buf, off, nbytes, crc);
80: } else {
81: throw new ArrayIndexOutOfBoundsException();
82: }
83: }
84:
85: private native long updateImpl(byte[] buf, int off, int nbytes,
86: long crc1);
87:
88: private native long updateByteImpl(byte val, long crc1);
89: }
|