• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 1996, 2020, 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.lang.ref.Reference;
29 import java.nio.ByteBuffer;
30 import java.util.Objects;
31 
32 import sun.nio.ch.DirectBuffer;
33 import jdk.internal.vm.annotation.IntrinsicCandidate;
34 
35 /**
36  * A class that can be used to compute the CRC-32 of a data stream.
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 class CRC32 implements Checksum {
45     private int crc;
46 
47     /**
48      * Creates a new CRC32 object.
49      */
CRC32()50     public CRC32() {
51     }
52 
53 
54     /**
55      * Updates the CRC-32 checksum with the specified byte (the low
56      * eight bits of the argument b).
57      */
58     @Override
update(int b)59     public void update(int b) {
60         crc = update(crc, b);
61     }
62 
63     /**
64      * Updates the CRC-32 checksum with the specified array of bytes.
65      *
66      * @throws ArrayIndexOutOfBoundsException
67      *         if {@code off} is negative, or {@code len} is negative, or
68      *         {@code off+len} is negative or greater than the length of
69      *         the array {@code b}.
70      */
71     @Override
update(byte[] b, int off, int len)72     public void update(byte[] b, int off, int len) {
73         if (b == null) {
74             throw new NullPointerException();
75         }
76         if (off < 0 || len < 0 || off > b.length - len) {
77             throw new ArrayIndexOutOfBoundsException();
78         }
79         crc = updateBytes(crc, b, off, len);
80     }
81 
82     // Android-changed: method kept during jdk17u update for compatibility.
83     /**
84      * Updates the CRC-32 checksum with the specified array of bytes.
85      *
86      * @param b the array of bytes to update the checksum with
87      */
88     @Override
update(byte[] b)89     public void update(byte[] b) {
90         crc = updateBytes(crc, b, 0, b.length);
91     }
92 
93     /**
94      * Updates the CRC-32 checksum with the bytes from the specified buffer.
95      *
96      * The checksum is updated with the remaining bytes in the buffer, starting
97      * at the buffer's position. Upon return, the buffer's position will be
98      * updated to its limit; its limit will not have been changed.
99      *
100      * @since 1.8
101      */
102     @Override
update(ByteBuffer buffer)103     public void update(ByteBuffer buffer) {
104         int pos = buffer.position();
105         int limit = buffer.limit();
106         assert (pos <= limit);
107         int rem = limit - pos;
108         if (rem <= 0)
109             return;
110         if (buffer.isDirect()) {
111             try {
112                 crc = updateByteBuffer(crc, ((DirectBuffer)buffer).address(), pos, rem);
113             } finally {
114                 Reference.reachabilityFence(buffer);
115             }
116         } else if (buffer.hasArray()) {
117             crc = updateBytes(crc, buffer.array(), pos + buffer.arrayOffset(), rem);
118         } else {
119             byte[] b = new byte[Math.min(buffer.remaining(), 4096)];
120             while (buffer.hasRemaining()) {
121                 int length = Math.min(buffer.remaining(), b.length);
122                 buffer.get(b, 0, length);
123                 update(b, 0, length);
124             }
125         }
126         buffer.position(limit);
127     }
128 
129     /**
130      * Resets CRC-32 to initial value.
131      */
132     @Override
reset()133     public void reset() {
134         crc = 0;
135     }
136 
137     /**
138      * Returns CRC-32 value.
139      */
140     @Override
getValue()141     public long getValue() {
142         return (long)crc & 0xffffffffL;
143     }
144 
145     @IntrinsicCandidate
update(int crc, int b)146     private static native int update(int crc, int b);
147 
updateBytes(int crc, byte[] b, int off, int len)148     private static int updateBytes(int crc, byte[] b, int off, int len) {
149         updateBytesCheck(b, off, len);
150         return updateBytes0(crc, b, off, len);
151     }
152 
153     @IntrinsicCandidate
updateBytes0(int crc, byte[] b, int off, int len)154     private static native int updateBytes0(int crc, byte[] b, int off, int len);
155 
updateBytesCheck(byte[] b, int off, int len)156     private static void updateBytesCheck(byte[] b, int off, int len) {
157         if (len <= 0) {
158             return;  // not an error because updateBytesImpl won't execute if len <= 0
159         }
160 
161         Objects.requireNonNull(b);
162 
163         if (off < 0 || off >= b.length) {
164             throw new ArrayIndexOutOfBoundsException(off);
165         }
166 
167         int endIndex = off + len - 1;
168         if (endIndex < 0 || endIndex >= b.length) {
169             throw new ArrayIndexOutOfBoundsException(endIndex);
170         }
171     }
172 
updateByteBuffer(int alder, long addr, int off, int len)173     private static int updateByteBuffer(int alder, long addr,
174                                         int off, int len) {
175         updateByteBufferCheck(addr);
176         return updateByteBuffer0(alder, addr, off, len);
177     }
178 
179     @IntrinsicCandidate
updateByteBuffer0(int alder, long addr, int off, int len)180     private static native int updateByteBuffer0(int alder, long addr,
181                                                 int off, int len);
182 
updateByteBufferCheck(long addr)183     private static void updateByteBufferCheck(long addr) {
184         // Performs only a null check because bounds checks
185         // are not easy to do on raw addresses.
186         if (addr == 0L) {
187             throw new NullPointerException();
188         }
189     }
190 }
191