+ * Implementor notes: this should've been an internal API, but we can't change it now since it's not within the internal + * subpackage. + * * @deprecated this class is deprecated and subject to removal. */ @Deprecated -@SdkInternalApi -public class Crc32CChecksum implements SdkChecksum { - - private Checksum crc32c; - private Checksum lastMarkedCrc32C; - private final boolean isCrtBasedChecksum; +@SdkProtectedApi +public final class Crc32CChecksum extends LegacyDelegatingChecksum { - /** - * Creates CRT Based Crc32C checksum if Crt classpath for Crc32c is loaded, else create Sdk Implemented Crc32c - */ public Crc32CChecksum() { - crc32c = CrtBasedChecksumProvider.createCrc32C(); - isCrtBasedChecksum = crc32c != null; - if (!isCrtBasedChecksum) { - crc32c = SdkCrc32C.create(); - } - } - - @Override - public byte[] getChecksumBytes() { - return Arrays.copyOfRange(longToByte(crc32c.getValue()), 4, 8); - } - - - @Override - public void mark(int readLimit) { - this.lastMarkedCrc32C = cloneChecksum(crc32c); - } - - @Override - public void update(int b) { - crc32c.update(b); - } - - @Override - public void update(byte[] b, int off, int len) { - crc32c.update(b, off, len); - } - - @Override - public long getValue() { - return crc32c.getValue(); - } - - @Override - public void reset() { - if (lastMarkedCrc32C == null) { - crc32c.reset(); - } else { - crc32c = cloneChecksum(lastMarkedCrc32C); - } - } - - - private Checksum cloneChecksum(Checksum checksum) { - if (isCrtBasedChecksum) { - try { - Method method = checksum.getClass().getDeclaredMethod("clone"); - return (Checksum) method.invoke(checksum); - } catch (ReflectiveOperationException e) { - throw new IllegalStateException("Could not clone checksum class " + checksum.getClass(), e); - } - } else { - return (Checksum) ((SdkCrc32C) checksum).clone(); - - } + super(DefaultChecksumAlgorithm.CRC32C); } } \ No newline at end of file diff --git a/core/sdk-core/src/main/java/software/amazon/awssdk/core/checksums/Crc32Checksum.java b/core/sdk-core/src/main/java/software/amazon/awssdk/core/checksums/Crc32Checksum.java index e0c6f9237f8d..7123a240c0fc 100644 --- a/core/sdk-core/src/main/java/software/amazon/awssdk/core/checksums/Crc32Checksum.java +++ b/core/sdk-core/src/main/java/software/amazon/awssdk/core/checksums/Crc32Checksum.java @@ -15,82 +15,24 @@ package software.amazon.awssdk.core.checksums; -import static software.amazon.awssdk.core.internal.util.HttpChecksumUtils.longToByte; - -import java.lang.reflect.Method; -import java.util.Arrays; -import java.util.zip.Checksum; -import software.amazon.awssdk.annotations.SdkInternalApi; -import software.amazon.awssdk.core.internal.checksums.factory.CrtBasedChecksumProvider; -import software.amazon.awssdk.core.internal.checksums.factory.SdkCrc32; +import software.amazon.awssdk.annotations.SdkProtectedApi; +import software.amazon.awssdk.checksums.DefaultChecksumAlgorithm; +import software.amazon.awssdk.core.internal.checksums.LegacyDelegatingChecksum; /** * Implementation of {@link SdkChecksum} to calculate an CRC32 checksum. + * + *
+ * Implementor notes: this should've been an internal API, but we can't change it now since it's not within the internal + * subpackage. + * * @deprecated this class is deprecated and subject to removal. */ @Deprecated -@SdkInternalApi -public class Crc32Checksum implements SdkChecksum { +@SdkProtectedApi +public final class Crc32Checksum extends LegacyDelegatingChecksum { - private Checksum crc32; - private Checksum lastMarkedCrc32; - private final boolean isCrtBasedChecksum; - - /** - * Creates CRT Based Crc32 checksum if Crt classpath for Crc32 is loaded, else create Sdk Implemented Crc32. - */ public Crc32Checksum() { - crc32 = CrtBasedChecksumProvider.createCrc32(); - isCrtBasedChecksum = crc32 != null; - if (!isCrtBasedChecksum) { - crc32 = SdkCrc32.create(); - } - } - - @Override - public byte[] getChecksumBytes() { - return Arrays.copyOfRange(longToByte(crc32.getValue()), 4, 8); - } - - @Override - public void mark(int readLimit) { - this.lastMarkedCrc32 = cloneChecksum(crc32); - } - - @Override - public void update(int b) { - crc32.update(b); - } - - @Override - public void update(byte[] b, int off, int len) { - crc32.update(b, off, len); - } - - @Override - public long getValue() { - return crc32.getValue(); - } - - @Override - public void reset() { - if ((lastMarkedCrc32 == null)) { - crc32.reset(); - } else { - crc32 = cloneChecksum(lastMarkedCrc32); - } - } - - private Checksum cloneChecksum(Checksum checksum) { - if (isCrtBasedChecksum) { - try { - Method method = checksum.getClass().getDeclaredMethod("clone"); - return (Checksum) method.invoke(checksum); - } catch (ReflectiveOperationException e) { - throw new IllegalStateException("Could not clone checksum class " + checksum.getClass(), e); - } - } else { - return (Checksum) ((SdkCrc32) checksum).clone(); - } + super(DefaultChecksumAlgorithm.CRC32); } } \ No newline at end of file diff --git a/core/sdk-core/src/main/java/software/amazon/awssdk/core/checksums/Md5Checksum.java b/core/sdk-core/src/main/java/software/amazon/awssdk/core/checksums/Md5Checksum.java index f690adb70440..e3fbb2a9c672 100644 --- a/core/sdk-core/src/main/java/software/amazon/awssdk/core/checksums/Md5Checksum.java +++ b/core/sdk-core/src/main/java/software/amazon/awssdk/core/checksums/Md5Checksum.java @@ -15,72 +15,24 @@ package software.amazon.awssdk.core.checksums; -import java.security.MessageDigest; -import software.amazon.awssdk.annotations.SdkInternalApi; +import software.amazon.awssdk.annotations.SdkProtectedApi; +import software.amazon.awssdk.checksums.DefaultChecksumAlgorithm; +import software.amazon.awssdk.core.internal.checksums.LegacyDelegatingChecksum; /** * Implementation of {@link SdkChecksum} to calculate an MD5 checksum. + * + *
+ * Implementor notes: this should've been an internal API, but we can't change it now since it's not within the internal + * subpackage. + * * @deprecated this class is deprecated and subject to removal. */ @Deprecated -@SdkInternalApi -public class Md5Checksum implements SdkChecksum { - - private MessageDigest digest; - - private MessageDigest digestLastMarked; +@SdkProtectedApi +public final class Md5Checksum extends LegacyDelegatingChecksum { public Md5Checksum() { - this.digest = getDigest(); - } - - @Override - public void update(int b) { - digest.update((byte) b); - } - - @Override - public void update(byte[] b, int off, int len) { - digest.update(b, off, len); - } - - @Override - public long getValue() { - throw new UnsupportedOperationException("Use getChecksumBytes() instead."); - } - - @Override - public void reset() { - digest = (digestLastMarked == null) - // This is necessary so that should there be a reset without a - // preceding mark, the MD5 would still be computed correctly. - ? getDigest() - : cloneFrom(digestLastMarked); - } - - private MessageDigest getDigest() { - try { - return MessageDigest.getInstance("MD5"); - } catch (Exception e) { - throw new IllegalStateException("Unexpected error creating MD5 checksum", e); - } - } - - @Override - public byte[] getChecksumBytes() { - return digest.digest(); - } - - @Override - public void mark(int readLimit) { - digestLastMarked = cloneFrom(digest); - } - - private MessageDigest cloneFrom(MessageDigest from) { - try { - return (MessageDigest) from.clone(); - } catch (CloneNotSupportedException e) { // should never occur - throw new IllegalStateException("unexpected", e); - } + super(DefaultChecksumAlgorithm.MD5); } } diff --git a/core/sdk-core/src/main/java/software/amazon/awssdk/core/checksums/SdkChecksum.java b/core/sdk-core/src/main/java/software/amazon/awssdk/core/checksums/SdkChecksum.java index 574e55d51a4d..77df8a1ce43b 100644 --- a/core/sdk-core/src/main/java/software/amazon/awssdk/core/checksums/SdkChecksum.java +++ b/core/sdk-core/src/main/java/software/amazon/awssdk/core/checksums/SdkChecksum.java @@ -43,8 +43,6 @@ public interface SdkChecksum extends Checksum { */ void mark(int readLimit); - - /** * Gets the Checksum based on the required Algorithm. * Instances for CRC32C, CRC32 Algorithm will be added from CRT Java library once they are available in release. diff --git a/core/sdk-core/src/main/java/software/amazon/awssdk/core/checksums/Sha1Checksum.java b/core/sdk-core/src/main/java/software/amazon/awssdk/core/checksums/Sha1Checksum.java index 962796836d02..7aa5ceb8b2cf 100644 --- a/core/sdk-core/src/main/java/software/amazon/awssdk/core/checksums/Sha1Checksum.java +++ b/core/sdk-core/src/main/java/software/amazon/awssdk/core/checksums/Sha1Checksum.java @@ -15,72 +15,24 @@ package software.amazon.awssdk.core.checksums; -import java.security.MessageDigest; import software.amazon.awssdk.annotations.SdkInternalApi; +import software.amazon.awssdk.checksums.DefaultChecksumAlgorithm; +import software.amazon.awssdk.core.internal.checksums.LegacyDelegatingChecksum; /** - * Implementation of {@link SdkChecksum} to calculate an Sha-1 checksum. + * Implementation of {@link SdkChecksum} to calculate a Sha-1 checksum. + * + *
+ * Implementor notes: this should've been an internal API, but we can't change it now since it's not within the internal + * subpackage. + * * @deprecated this class is deprecated and subject to removal. */ @Deprecated @SdkInternalApi -public class Sha1Checksum implements SdkChecksum { - - private MessageDigest digest; - - private MessageDigest digestLastMarked; +public final class Sha1Checksum extends LegacyDelegatingChecksum { public Sha1Checksum() { - this.digest = getDigest(); - } - - @Override - public void update(int b) { - digest.update((byte) b); - } - - @Override - public void update(byte[] b, int off, int len) { - digest.update(b, off, len); - } - - @Override - public long getValue() { - throw new UnsupportedOperationException("Use getChecksumBytes() instead."); - } - - @Override - public void reset() { - digest = (digestLastMarked == null) - // This is necessary so that should there be a reset without a - // preceding mark, the Sha-1 would still be computed correctly. - ? getDigest() - : cloneFrom(digestLastMarked); - } - - private MessageDigest getDigest() { - try { - return MessageDigest.getInstance("SHA-1"); - } catch (Exception e) { - throw new IllegalStateException("Unexpected error creating SHA-1 checksum", e); - } - } - - @Override - public byte[] getChecksumBytes() { - return digest.digest(); - } - - @Override - public void mark(int readLimit) { - digestLastMarked = cloneFrom(digest); - } - - private MessageDigest cloneFrom(MessageDigest from) { - try { - return (MessageDigest) from.clone(); - } catch (CloneNotSupportedException e) { // should never occur - throw new IllegalStateException("unexpected", e); - } + super(DefaultChecksumAlgorithm.SHA1); } } diff --git a/core/sdk-core/src/main/java/software/amazon/awssdk/core/checksums/Sha256Checksum.java b/core/sdk-core/src/main/java/software/amazon/awssdk/core/checksums/Sha256Checksum.java index b002cd0ad777..085cc315262b 100644 --- a/core/sdk-core/src/main/java/software/amazon/awssdk/core/checksums/Sha256Checksum.java +++ b/core/sdk-core/src/main/java/software/amazon/awssdk/core/checksums/Sha256Checksum.java @@ -15,72 +15,24 @@ package software.amazon.awssdk.core.checksums; -import java.security.MessageDigest; -import software.amazon.awssdk.annotations.SdkInternalApi; +import software.amazon.awssdk.annotations.SdkProtectedApi; +import software.amazon.awssdk.checksums.DefaultChecksumAlgorithm; +import software.amazon.awssdk.core.internal.checksums.LegacyDelegatingChecksum; /** - * Implementation of {@link SdkChecksum} to calculate an Sha-256 Checksum. + * Implementation of {@link SdkChecksum} to calculate a Sha-256 Checksum. + * + *
+ * Implementor notes: this should've been an internal API, but we can't change it now since it's not within the internal
+ * subpackage.
+ *
* @deprecated this class is deprecated and subject to removal.
*/
@Deprecated
-@SdkInternalApi
-public class Sha256Checksum implements SdkChecksum {
-
- private MessageDigest digest;
-
- private MessageDigest digestLastMarked;
+@SdkProtectedApi
+public final class Sha256Checksum extends LegacyDelegatingChecksum {
public Sha256Checksum() {
- this.digest = getDigest();
- }
-
- @Override
- public void update(int b) {
- digest.update((byte) b);
- }
-
- @Override
- public void update(byte[] b, int off, int len) {
- digest.update(b, off, len);
- }
-
- @Override
- public long getValue() {
- throw new UnsupportedOperationException("Use getChecksumBytes() instead.");
- }
-
- @Override
- public void reset() {
- digest = (digestLastMarked == null)
- // This is necessary so that should there be a reset without a
- // preceding mark, the Sha-256 would still be computed correctly.
- ? getDigest()
- : cloneFrom(digestLastMarked);
- }
-
- private MessageDigest getDigest() {
- try {
- return MessageDigest.getInstance("SHA-256");
- } catch (Exception e) {
- throw new IllegalStateException("Unexpected error creating SHA-256 checksum", e);
- }
- }
-
- @Override
- public byte[] getChecksumBytes() {
- return digest.digest();
- }
-
- @Override
- public void mark(int readLimit) {
- digestLastMarked = cloneFrom(digest);
- }
-
- private MessageDigest cloneFrom(MessageDigest from) {
- try {
- return (MessageDigest) from.clone();
- } catch (CloneNotSupportedException e) { // should never occur
- throw new IllegalStateException("unexpected", e);
- }
+ super(DefaultChecksumAlgorithm.SHA256);
}
}
diff --git a/core/sdk-core/src/main/java/software/amazon/awssdk/core/internal/checksums/LegacyDelegatingChecksum.java b/core/sdk-core/src/main/java/software/amazon/awssdk/core/internal/checksums/LegacyDelegatingChecksum.java
new file mode 100644
index 000000000000..36e1e7dca2be
--- /dev/null
+++ b/core/sdk-core/src/main/java/software/amazon/awssdk/core/internal/checksums/LegacyDelegatingChecksum.java
@@ -0,0 +1,62 @@
+/*
+ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License").
+ * You may not use this file except in compliance with the License.
+ * A copy of the License is located at
+ *
+ * http://aws.amazon.com/apache2.0
+ *
+ * or in the "license" file accompanying this file. This file is distributed
+ * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
+ * express or implied. See the License for the specific language governing
+ * permissions and limitations under the License.
+ */
+
+package software.amazon.awssdk.core.internal.checksums;
+
+import software.amazon.awssdk.annotations.SdkInternalApi;
+import software.amazon.awssdk.checksums.spi.ChecksumAlgorithm;
+import software.amazon.awssdk.core.checksums.SdkChecksum;
+
+/**
+ * A delegating checksum class to delegate to new checksum implementation for legacy checksum implementations
+ */
+@SdkInternalApi
+public abstract class LegacyDelegatingChecksum implements SdkChecksum {
+ private software.amazon.awssdk.checksums.SdkChecksum delegate;
+
+ protected LegacyDelegatingChecksum(ChecksumAlgorithm checksumAlgorithm) {
+ this.delegate = software.amazon.awssdk.checksums.SdkChecksum.forAlgorithm(checksumAlgorithm);
+ }
+
+ @Override
+ public byte[] getChecksumBytes() {
+ return delegate.getChecksumBytes();
+ }
+
+ @Override
+ public void mark(int readLimit) {
+ delegate.mark(readLimit);
+ }
+
+ @Override
+ public void update(int b) {
+ delegate.update(b);
+ }
+
+ @Override
+ public void update(byte[] b, int off, int len) {
+ delegate.update(b, off, len);
+ }
+
+ @Override
+ public long getValue() {
+ return delegate.getValue();
+ }
+
+ @Override
+ public void reset() {
+ delegate.reset();
+ }
+}
diff --git a/core/sdk-core/src/main/java/software/amazon/awssdk/core/internal/checksums/factory/CrtBasedChecksumProvider.java b/core/sdk-core/src/main/java/software/amazon/awssdk/core/internal/checksums/factory/CrtBasedChecksumProvider.java
deleted file mode 100644
index c685ae32b4ed..000000000000
--- a/core/sdk-core/src/main/java/software/amazon/awssdk/core/internal/checksums/factory/CrtBasedChecksumProvider.java
+++ /dev/null
@@ -1,71 +0,0 @@
-/*
- * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
- *
- * Licensed under the Apache License, Version 2.0 (the "License").
- * You may not use this file except in compliance with the License.
- * A copy of the License is located at
- *
- * http://aws.amazon.com/apache2.0
- *
- * or in the "license" file accompanying this file. This file is distributed
- * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
- * express or implied. See the License for the specific language governing
- * permissions and limitations under the License.
- */
-
-package software.amazon.awssdk.core.internal.checksums.factory;
-
-import java.util.Optional;
-import java.util.zip.Checksum;
-import software.amazon.awssdk.annotations.SdkInternalApi;
-import software.amazon.awssdk.core.internal.util.ClassLoaderHelper;
-import software.amazon.awssdk.utils.Lazy;
-import software.amazon.awssdk.utils.Logger;
-
-
-/**
- * Class to load the Crt based checksum from aws-crt-java library if it is present in class path.
- */
-@SdkInternalApi
-public final class CrtBasedChecksumProvider {
-
- public static final Logger LOG = Logger.loggerFor(CrtBasedChecksumProvider.class);
- private static final String CRT_CLASSPATH_FOR_CRC32C = "software.amazon.awssdk.crt.checksums.CRC32C";
- private static final String CRT_CLASSPATH_FOR_CRC32 = "software.amazon.awssdk.crt.checksums.CRC32";
- private static final Lazy