| CRC32.java |
1 /*
2 * %W% %E%
3 *
4 * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
5 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6 */
7
8 package java.util.zip;
9
10 /**
11 * A class that can be used to compute the CRC-32 of a data stream.
12 *
13 * @see Checksum
14 * @version %I%, %G%
15 * @author David Connelly
16 */
17 public
18 class CRC32 implements Checksum {
19 private int crc;
20
21 /**
22 * Creates a new CRC32 object.
23 */
24 public CRC32() {
25 }
26
27
28 /**
29 * Updates CRC-32 with specified byte.
30 */
31 public void update(int b) {
32 crc = update(crc, b);
33 }
34
35 /**
36 * Updates CRC-32 with specified array of bytes.
37 */
38 public void update(byte[] b, int off, int len) {
39 if (b == null) {
40 throw new NullPointerException();
41 }
42 if (off < 0 || len < 0 || off > b.length - len) {
43 throw new ArrayIndexOutOfBoundsException();
44 }
45 crc = updateBytes(crc, b, off, len);
46 }
47
48 /**
49 * Updates checksum with specified array of bytes.
50 *
51 * @param b the array of bytes to update the checksum with
52 */
53 public void update(byte[] b) {
54 crc = updateBytes(crc, b, 0, b.length);
55 }
56
57 /**
58 * Resets CRC-32 to initial value.
59 */
60 public void reset() {
61 crc = 0;
62 }
63
64 /**
65 * Returns CRC-32 value.
66 */
67 public long getValue() {
68 return (long)crc & 0xffffffffL;
69 }
70
71 private native static int update(int crc, int b);
72 private native static int updateBytes(int crc, byte[] b, int off, int len);
73 }
74