• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  * SPDX-License-Identifier: Apache-2.0.
4  */
5 package software.amazon.awssdk.crt.checksums;
6 
7 import software.amazon.awssdk.crt.CRT;
8 import java.util.zip.Checksum;
9 
10 /**
11  * CRT implementation of the Java Checksum interface for making CRC32 checksum calculations
12  */
13 public class CRC32 implements Checksum, Cloneable {
14     static {
CRT()15         new CRT();
16     };
17 
18     private int value = 0;
19 
20     /**
21      * Default constructor
22      */
CRC32()23     public CRC32() {
24     }
25 
CRC32(int value)26     private CRC32(int value) {
27         this.value = value;
28     }
29 
30     @Override
clone()31     public Object clone() {
32         return new CRC32(value);
33     }
34 
35     /**
36      * Returns the current checksum value.
37      *
38      * @return the current checksum value.
39      */
40     @Override
getValue()41     public long getValue() {
42         return (long) value & 0xffffffffL;
43     }
44 
45     /**
46      * Resets the checksum to its initial value.
47      */
48     @Override
reset()49     public void reset() {
50         value = 0;
51     }
52 
53     /**
54      * Updates the current checksum with the specified array of bytes.
55      *
56      * @param b the byte array to update the checksum with
57      * @param off the starting offset within b of the data to use
58      * @param len the number of bytes to use in the update
59      */
60     @Override
update(byte[] b, int off, int len)61     public void update(byte[] b, int off, int len) {
62         if (b == null) {
63             throw new NullPointerException();
64         }
65         if (off < 0 || len < 0 || off > b.length - len) {
66             throw new ArrayIndexOutOfBoundsException();
67         }
68         value = crc32(b, value, off, len);
69     }
70 
71     /**
72      * Updates the current checksum with the specified array of bytes.
73      *
74      * @param b the byte array to update the checksum with
75      */
update(byte[] b)76     public void update(byte[] b) {
77         update(b, 0, b.length);
78     }
79 
80     /**
81      * Updates the current checksum with the specified byte.
82      *
83      * @param b the byte to update the checksum with
84      */
85     @Override
update(int b)86     public void update(int b) {
87         if (b < 0 || b > 0xff) {
88             throw new IllegalArgumentException();
89         }
90         byte[] buf = { (byte) (b & 0x000000ff) };
91         this.update(buf);
92     }
93 
94     /*******************************************************************************
95      * native methods
96      ******************************************************************************/
crc32(byte[] input, int previous, int offset, int length)97     private static native int crc32(byte[] input, int previous, int offset, int length);;
98 }
99