989a2049283db0ab337bdf833cefe1b160db1e89
1 /* Adler.java - Computes Adler32 data checksum of a data stream
2 Copyright (C) 1999, 2000 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 As a special exception, if you link this library with other files to
22 produce an executable, this library does not by itself cause the
23 resulting executable to be covered by the GNU General Public License.
24 This exception does not however invalidate any other reasons why the
25 executable file might be covered by the GNU General Public License. */
27 package java
.util
.zip
;
30 * Written using on-line Java Platform 1.2 API Specification, as well
31 * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998).
32 * The actual Adler32 algorithm is taken from RFC 1950.
33 * Status: Believed complete and correct.
37 * Computes Adler32 data checksum of a data stream.
38 * The actual Adler32 algorithm is described in RFC 1950
39 * (ZLIB Compressed Data Format Specification version 3.3).
40 * Can be used to get the CRC32 over a stream if used with checked input/output
43 * @see InflaterInputStream
44 * @see InflaterOutputStream
47 * @date April 6, 1999.
49 public class Adler32
implements Checksum
52 /** largest prime smaller than 65536 */
53 private static int BASE
= 65521;
59 * Creates an Adler32 data checksum.
67 * Resets the Adler32 data checksum as if no update was ever called.
69 public void reset () { s1
= 1; s2
= 0; }
72 * Adds one byte to the data checksum.
74 * @param bval the data value to add. The high byte of the int is ignored.
76 public void update (int bval
)
78 s1
= (s1
+ (bval
& 0xFF)) % BASE
;
79 s2
= (s1
+ s2
) % BASE
;
83 * Adds the complete byte array to the data checksum.
85 public void update (byte[] buffer
)
87 update(buffer
, 0, buffer
.length
);
91 * Adds the byte array to the data checksum.
93 * @param buf the buffer which contains the data
94 * @param off the offset in the buffer where the data starts
95 * @param len the length of the data
97 public void update (byte[] buf
, int off
, int len
)
103 // We can defer the modulo operation.
110 s1
= s1
+ (buf
[off
++] & 0xFF);
121 * Returns the Adler32 data checksum computed so far.
123 public long getValue()
125 return ((long) s2
<< 16) + s1
;