KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > util > zip > Adler32


1 /*
2  * @(#)Adler32.java 1.29 05/08/09
3  *
4  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
5  * SUN 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 Adler-32 checksum of a data
12  * stream. An Adler-32 checksum is almost as reliable as a CRC-32 but
13  * can be computed much faster.
14  *
15  * @see Checksum
16  * @version 1.29, 08/09/05
17  * @author David Connelly
18  */

19 public
20 class Adler32 implements Checksum JavaDoc {
21     private int adler = 1;
22
23     /**
24      * Creates a new Adler32 object.
25      */

26     public Adler32() {
27     }
28    
29
30     /**
31      * Updates checksum with specified byte.
32      *
33      * @param b an array of bytes
34      */

35     public void update(int b) {
36     adler = update(adler, b);
37     }
38
39     /**
40      * Updates checksum with specified array of bytes.
41      */

42     public void update(byte[] b, int off, int len) {
43     if (b == null) {
44         throw new NullPointerException JavaDoc();
45     }
46         if (off < 0 || len < 0 || off > b.length - len) {
47         throw new ArrayIndexOutOfBoundsException JavaDoc();
48     }
49     adler = updateBytes(adler, b, off, len);
50     }
51
52     /**
53      * Updates checksum with specified array of bytes.
54      */

55     public void update(byte[] b) {
56     adler = updateBytes(adler, b, 0, b.length);
57     }
58
59     /**
60      * Resets checksum to initial value.
61      */

62     public void reset() {
63     adler = 1;
64     }
65
66     /**
67      * Returns checksum value.
68      */

69     public long getValue() {
70     return (long)adler & 0xffffffffL;
71     }
72
73     private native static int update(int adler, int b);
74     private native static int updateBytes(int adler, byte[] b, int off,
75                       int len);
76 }
77
Popular Tags