01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: package java.util.zip;
19:
20: /**
21: * The Adler32 class is used to compute the Adler32 Checksum from a set of data.
22: */
23: public class Adler32 implements java.util.zip.Checksum {
24:
25: private long adler = 1;
26:
27: /**
28: * Returns the Adler32 checksum for all input received
29: *
30: * @return The checksum for this instance
31: */
32: public long getValue() {
33: return adler;
34: }
35:
36: /**
37: * Reset this instance to its initial checksum
38: */
39: public void reset() {
40: adler = 1;
41: }
42:
43: /**
44: * Update this Adler32 checksum using val.
45: *
46: * @param i
47: * byte to update checksum with
48: */
49: public void update(int i) {
50: adler = updateByteImpl(i, adler);
51: }
52:
53: /**
54: * Update this Adler32 checksum using the contents of buf.
55: *
56: * @param buf
57: * bytes to update checksum with
58: */
59: public void update(byte[] buf) {
60: update(buf, 0, buf.length);
61: }
62:
63: /**
64: * Update this Adler32 checksum with the contents of buf, starting from
65: * offset and using nbytes of data.
66: *
67: * @param buf
68: * buffer to obtain dat from
69: * @param off
70: * offset i buf to copy from
71: * @param nbytes
72: * number of bytes from buf to use
73: */
74: public void update(byte[] buf, int off, int nbytes) {
75: // avoid int overflow, check null buf
76: if (off <= buf.length && nbytes >= 0 && off >= 0
77: && buf.length - off >= nbytes) {
78: adler = updateImpl(buf, off, nbytes, adler);
79: } else {
80: throw new ArrayIndexOutOfBoundsException();
81: }
82: }
83:
84: private native long updateImpl(byte[] buf, int off, int nbytes,
85: long adler1);
86:
87: private native long updateByteImpl(int val, long adler1);
88: }
|