1
2
3
4 package joeq.ClassLib.Common.java.util.zip;
5
6 /***
7 * Adler32
8 *
9 * @author John Whaley <jwhaley@alum.mit.edu>
10 * @version $Id: Adler32.java 1451 2004-03-09 06:27:08Z jwhaley $
11 */
12 public class Adler32 {
13
14 /*** largest prime smaller than 65536 */
15 private static final int BASE = 65521;
16
17 private int checksum;
18
19
20
21
22
23
24 /***
25 * Creates a new instance of the <code>Adler32</code> class.
26 * The checksum starts off with a value of 1.
27 */
28 public Adler32 ()
29 {
30 reset();
31 }
32
33 /***
34 * Resets the Adler32 checksum to the initial value.
35 */
36 public void reset ()
37 {
38 checksum = 1;
39 }
40
41 /***
42 * Updates the checksum with the byte b.
43 *
44 * @param bval the data value to add. The high byte of the int is ignored.
45 */
46 public void update (int bval)
47 {
48
49
50 int s1 = checksum & 0xffff;
51 int s2 = checksum >>> 16;
52
53 s1 = (s1 + (bval & 0xFF)) % BASE;
54 s2 = (s1 + s2) % BASE;
55
56 checksum = (s2 << 16) + s1;
57 }
58
59 /***
60 * Updates the checksum with the bytes taken from the array.
61 *
62 * @param buffer an array of bytes
63 */
64 public void update (byte[] buffer)
65 {
66 update(buffer, 0, buffer.length);
67 }
68
69 /***
70 * Updates the checksum with the bytes taken from the array.
71 *
72 * @param buf an array of bytes
73 * @param off the start of the data used for this update
74 * @param len the number of bytes to use for this update
75 */
76 public void update (byte[] buf, int off, int len)
77 {
78
79 int s1 = checksum & 0xffff;
80 int s2 = checksum >>> 16;
81
82 while (len > 0)
83 {
84
85
86
87 int n = 3800;
88 if (n > len)
89 n = len;
90 len -= n;
91 while (--n >= 0)
92 {
93 s1 = s1 + (buf[off++] & 0xFF);
94 s2 = s2 + s1;
95 }
96 s1 %= BASE;
97 s2 %= BASE;
98 }
99
100
101
102
103
104
105
106
107
108
109 checksum = (s2 << 16) | s1;
110 }
111
112 /***
113 * Returns the Adler32 data checksum computed so far.
114 */
115 public long getValue()
116 {
117 return (long) checksum & 0xffffffffL;
118 }
119 }