1 /*
2  * Copyright (c) 1996, 2015, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package java.util.zip;
27 
28 import java.nio.ByteBuffer;
29 import sun.nio.ch.DirectBuffer;
30 
31 import jdk.internal.HotSpotIntrinsicCandidate;
32 
33 /**
34  * A class that can be used to compute the Adler-32 checksum of a data
35  * stream. An Adler-32 checksum is almost as reliable as a CRC-32 but
36  * can be computed much faster.
37  *
38  * <p> Passing a {@code null} argument to a method in this class will cause
39  * a {@link NullPointerException} to be thrown.</p>
40  *
41  * @author      David Connelly
42  * @since 1.1
43  */
44 public
45 class Adler32 implements Checksum {
46 
47     private int adler = 1;
48 
49     /**
50      * Creates a new Adler32 object.
51      */
Adler32()52     public Adler32() {
53     }
54 
55     /**
56      * Updates the checksum with the specified byte (the low eight
57      * bits of the argument b).
58      */
59     @Override
update(int b)60     public void update(int b) {
61         adler = update(adler, b);
62     }
63 
64     /**
65      * Updates the checksum with the specified array of bytes.
66      *
67      * @throws ArrayIndexOutOfBoundsException
68      *         if {@code off} is negative, or {@code len} is negative, or
69      *         {@code off+len} is negative or greater than the length of
70      *         the array {@code b}.
71      */
72     @Override
update(byte[] b, int off, int len)73     public void update(byte[] b, int off, int len) {
74         if (b == null) {
75             throw new NullPointerException();
76         }
77         if (off < 0 || len < 0 || off > b.length - len) {
78             throw new ArrayIndexOutOfBoundsException();
79         }
80         adler = updateBytes(adler, b, off, len);
81     }
82 
83     /**
84      * Updates the checksum with the bytes from the specified buffer.
85      *
86      * The checksum is updated with the remaining bytes in the buffer, starting
87      * at the buffer's position. Upon return, the buffer's position will be
88      * updated to its limit; its limit will not have been changed.
89      *
90      * @since 1.8
91      */
92     @Override
update(ByteBuffer buffer)93     public void update(ByteBuffer buffer) {
94         int pos = buffer.position();
95         int limit = buffer.limit();
96         assert (pos <= limit);
97         int rem = limit - pos;
98         if (rem <= 0)
99             return;
100         if (buffer instanceof DirectBuffer) {
101             adler = updateByteBuffer(adler, ((DirectBuffer)buffer).address(), pos, rem);
102         } else if (buffer.hasArray()) {
103             adler = updateBytes(adler, buffer.array(), pos + buffer.arrayOffset(), rem);
104         } else {
105             byte[] b = new byte[Math.min(buffer.remaining(), 4096)];
106             while (buffer.hasRemaining()) {
107                 int length = Math.min(buffer.remaining(), b.length);
108                 buffer.get(b, 0, length);
109                 update(b, 0, length);
110             }
111         }
112         buffer.position(limit);
113     }
114 
115     /**
116      * Resets the checksum to initial value.
117      */
118     @Override
reset()119     public void reset() {
120         adler = 1;
121     }
122 
123     /**
124      * Returns the checksum value.
125      */
126     @Override
getValue()127     public long getValue() {
128         return (long)adler & 0xffffffffL;
129     }
130 
update(int adler, int b)131     private static native int update(int adler, int b);
132 
133     @HotSpotIntrinsicCandidate
updateBytes(int adler, byte[] b, int off, int len)134     private static native int updateBytes(int adler, byte[] b, int off,
135                                           int len);
136     @HotSpotIntrinsicCandidate
updateByteBuffer(int adler, long addr, int off, int len)137     private static native int updateByteBuffer(int adler, long addr,
138                                                int off, int len);
139 
140     static {
ZipUtils.loadLibrary()141         ZipUtils.loadLibrary();
142     }
143 }
144