OSDN Git Service

* All files: Updated copyright information.
[pf3gnuchains/gcc-fork.git] / libjava / java / util / zip / Adler32.java
1 /* Copyright (C) 1999  Free Software Foundation
2
3    This file is part of libgcj.
4
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
7 details.  */
8
9 package java.util.zip;
10
11 /**
12  * @author Per Bothner
13  * @date April 6, 1999.
14  */
15
16 /*
17  * Written using on-line Java Platform 1.2 API Specification, as well
18  * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998).
19  * The actual Adler32 algorithm is taken from RFC 1950.
20  * Status:  Believed complete and correct.
21  */
22
23 public class Adler32 implements Checksum
24 {
25   private static int BASE = 65521; /* largest prime smaller than 65536 */
26
27   int s1;
28   int s2;
29
30   public Adler32 ()
31   {
32     reset();
33   }
34
35   public void reset () { s1 = 1;  s2 = 0; }
36
37   public void update (int bval)
38   {
39     s1 = (s1 + (bval & 0xFF)) % BASE;
40     s2 = (s1 + s2) % BASE;
41   }
42
43   public void update (byte[] buffer)
44   {
45     update(buffer, 0, buffer.length);
46   }
47
48   public void update (byte[] buf, int off, int len)
49   {
50     int s1 = this.s1;
51     int s2 = this.s2;
52     while (len > 0)
53       {
54         // We can defer the modulo operation.
55         int n = 4000;
56         if (n > len)
57           n = len;
58         len -= n;
59         while (--n >= 0)
60           {
61             s1 = s1 + (buf[off++] & 0xFF);
62             s2 = s2 + s1;
63           }
64         s1 %= BASE;
65         s2 %= BASE;
66       }
67     this.s1 = s1;
68     this.s2 = s2;
69   }
70
71   public long getValue()
72   {
73     return ((long) s2 << 16) + s1;
74   }
75 }
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101