View Javadoc

1   // Adler32.java, created Mon Jul  8 11:14:45 2002 by joewhaley
2   // Copyright (C) 2001-3 John Whaley <jwhaley@alum.mit.edu>
3   // Licensed under the terms of the GNU LGPL; see COPYING for details.
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; //we do all in int.
18  
19    //Note that java doesn't have unsigned integers,
20    //so we have to be careful with what arithmetic 
21    //we do. We return the checksum as a long to 
22    //avoid sign confusion.
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; //Initialize to 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      //We could make a length 1 byte array and call update again, but I
49      //would rather not have that overhead
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      //(By Per Bothner)
79      int s1 = checksum & 0xffff;
80      int s2 = checksum >>> 16;
81  
82      while (len > 0)
83        {
84          // We can defer the modulo operation:
85          // s1 maximally grows from 65521 to 65521 + 255 * 3800
86          // s2 maximally grows by 3800 * median(s1) = 2090079800 < 2^31
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     /*Old implementation, borrowed from somewhere:
101     int n;
102     
103     while (len-- > 0) {
104 
105       s1 = (s1 + (bs[offset++] & 0xff)) % BASE; 
106       s2 = (s2 + s1) % BASE;
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 }