1717
1818import java .util .zip .Checksum ;
1919import software .amazon .awssdk .annotations .SdkInternalApi ;
20+ import software .amazon .awssdk .checksums .DefaultChecksumAlgorithm ;
2021import software .amazon .awssdk .checksums .SdkChecksum ;
22+ import software .amazon .awssdk .checksums .spi .ChecksumAlgorithm ;
23+ import software .amazon .awssdk .crt .checksums .CRC32C ;
24+ import software .amazon .awssdk .crt .checksums .CRC64NVME ;
25+ import software .amazon .awssdk .utils .ClassLoaderHelper ;
26+ import software .amazon .awssdk .utils .Lazy ;
2127
2228/**
23- * Utility class providing implementations of CRC checksums, specifically CRC32C and CRC64NVME .
29+ * Utility class providing implementations of checksums.
2430 *
2531 * <p>Supports the following implementations for CRC32C:</p>
2632 * <ul>
2935 * <li>SDK-based CRC32C (fallback)</li>
3036 * </ul>
3137 *
32- * <p>Only supports CRT-based implementation for CRC64NVME (using AWS CRT library).</p>
38+ * <p>Supports CRT-based implementations for CRC64NVME and XXHASH algorithms (using AWS CRT library).</p>
3339 *
3440 * <p>For internal use only ({@link SdkInternalApi}).</p>
3541 */
3642@ SdkInternalApi
37- public final class CrcChecksumProvider {
38-
43+ public final class ChecksumProvider {
3944
4045 // Class paths for different CRC32C implementations
4146 private static final String CRT_CRC32C_CLASS_PATH = "software.amazon.awssdk.crt.checksums.CRC32C" ;
4247 private static final String JAVA_CRC32C_CLASS_PATH = "java.util.zip.CRC32C" ;
4348 private static final ConstructorCache CONSTRUCTOR_CACHE = new ConstructorCache ();
4449 private static final String CRT_CRC64NVME_PATH = "software.amazon.awssdk.crt.checksums.CRC64NVME" ;
50+ private static final String CRT_XXHASH_PATH = "software.amazon.awssdk.crt.checksums.XXHash" ;
4551 private static final String CRT_MODULE = "software.amazon.awssdk.crt:aws-crt" ;
4652
53+ private static Lazy <Boolean > isXxHashAvailable = checkCrtAvailability (CRT_XXHASH_PATH );
54+ private static Lazy <Boolean > isCrc64NvmeAvailable = checkCrtAvailability (CRT_CRC64NVME_PATH );
55+ private static Lazy <Boolean > isCrc32CAvailable = checkCrtAvailability (CRT_CRC32C_CLASS_PATH );
56+
4757 // Private constructor to prevent instantiation
48- private CrcChecksumProvider () {
58+ private ChecksumProvider () {
59+ }
60+
61+ private static Lazy <Boolean > checkCrtAvailability (String fqcn ) {
62+ return new Lazy <>(() -> {
63+ try {
64+ ClassLoaderHelper .loadClass (fqcn , false );
65+ } catch (ClassNotFoundException e ) {
66+ return false ;
67+ }
68+ return true ;
69+ });
4970 }
5071
5172 /**
@@ -74,14 +95,12 @@ public static SdkChecksum crc32cImplementation() {
7495 }
7596
7697 static SdkChecksum createCrtCrc32C () {
77- return CONSTRUCTOR_CACHE .getConstructor (CRT_CRC32C_CLASS_PATH ).map (constructor -> {
78- try {
79- Checksum checksumInstance = (Checksum ) constructor .newInstance ();
80- return new CrcCloneOnMarkChecksum (checksumInstance );
81- } catch (ReflectiveOperationException e ) {
82- throw new IllegalStateException ("Failed to instantiate " + CRT_CRC32C_CLASS_PATH , e );
83- }
84- }).orElse (null );
98+ if (!isCrc32CAvailable .getValue ()) {
99+ return null ;
100+ }
101+
102+ Checksum checksumInstance = new CRC32C ();
103+ return new CrcCloneOnMarkChecksum (checksumInstance );
85104 }
86105
87106 /**
@@ -96,16 +115,57 @@ static SdkChecksum createCrtCrc32C() {
96115 * @throws RuntimeException if the `CRC64NVME` implementation is not available.
97116 */
98117 static SdkChecksum crc64NvmeCrtImplementation () {
99- return CONSTRUCTOR_CACHE .getConstructor (CRT_CRC64NVME_PATH ).map (constructor -> {
100- try {
101- Checksum checksumInstance = (Checksum ) constructor .newInstance ();
102- return new CrcCloneOnMarkChecksum (checksumInstance );
103- } catch (ReflectiveOperationException e ) {
104- throw new IllegalStateException ("Failed to instantiate " + CRT_CRC32C_CLASS_PATH , e );
105- }
106- }).orElseThrow (() -> new RuntimeException (
107- "Could not load " + CRT_CRC64NVME_PATH + ". Add dependency on '" + CRT_MODULE
108- + "' module to enable CRC64NVME feature." ));
118+
119+ if (!isCrc64NvmeAvailable .getValue ()) {
120+ throw new RuntimeException (
121+ "Could not load " + CRT_CRC64NVME_PATH + ". Add dependency on '" + CRT_MODULE
122+ + "' module to enable CRC64NVME feature." );
123+ }
124+
125+ return new CrcCloneOnMarkChecksum (new CRC64NVME ());
126+ }
127+
128+ /**
129+ * Creates an instance of the CRT-based XXHASH64 checksum using AWS's CRT library.
130+ *
131+ * @return An {@link SdkChecksum} instance for XXHASH64.
132+ * @throws IllegalStateException if instantiation fails.
133+ * @throws RuntimeException if the CRT implementation is not available.
134+ */
135+ public static SdkChecksum xxHash64CrtImplementation () {
136+ return crtXxHash (DefaultChecksumAlgorithm .XXHASH64 );
137+ }
138+
139+ /**
140+ * Creates an instance of the CRT-based XXHASH3 checksum using AWS's CRT library.
141+ *
142+ * @return An {@link SdkChecksum} instance for XXHASH3.
143+ * @throws IllegalStateException if instantiation fails.
144+ * @throws RuntimeException if the CRT implementation is not available.
145+ */
146+ public static SdkChecksum xxHash3CrtImplementation () {
147+ return crtXxHash (DefaultChecksumAlgorithm .XXHASH3 );
148+ }
149+
150+ /**
151+ * Creates an instance of the CRT-based XXHASH128 checksum using AWS's CRT library.
152+ *
153+ * @return An {@link SdkChecksum} instance for XXHASH128.
154+ * @throws IllegalStateException if instantiation fails.
155+ * @throws RuntimeException if the CRT implementation is not available.
156+ */
157+ public static SdkChecksum xxHash128CrtImplementation () {
158+ return crtXxHash (DefaultChecksumAlgorithm .XXHASH128 );
159+ }
160+
161+ static SdkChecksum crtXxHash (ChecksumAlgorithm algorithm ) {
162+ if (!isXxHashAvailable .getValue ()) {
163+ throw new RuntimeException (
164+ "Could not load " + CRT_XXHASH_PATH + ". Add dependency on '" + CRT_MODULE
165+ + "' module." );
166+ }
167+
168+ return new XxHashChecksum (algorithm );
109169 }
110170
111171 static SdkChecksum createJavaCrc32C () {
@@ -117,5 +177,4 @@ static SdkChecksum createJavaCrc32C() {
117177 }
118178 }).orElse (null );
119179 }
120-
121180}
0 commit comments