Skip to content

Commit 21479fe

Browse files
authored
Duplicate code reduction part 2: clean up checksum classes (#6484)
* Duplicate code reduction part 2: clean up checksum classes * Fix build
1 parent 8c81f83 commit 21479fe

File tree

14 files changed

+122
-1832
lines changed

14 files changed

+122
-1832
lines changed

build-tools/src/main/resources/software/amazon/awssdk/spotbugs-suppressions.xml

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -302,14 +302,12 @@
302302
</Match>
303303

304304
<!-- Retrieves and updates crc value in update() -->
305-
<Match>
306-
<Or>
307-
<Class name="software.amazon.awssdk.checksums.internal.SdkCrc32CChecksum"/>
308-
<Class name="software.amazon.awssdk.checksums.internal.SdkCrc32Checksum"/>
309-
<Class name="software.amazon.awssdk.core.internal.checksums.factory.SdkCrc32C"/>
310-
<Class name="software.amazon.awssdk.core.internal.checksums.factory.SdkCrc32"/>
311-
</Or>
312-
<Bug pattern="SA_FIELD_SELF_ASSIGNMENT"/>
305+
<Match>
306+
<Or>
307+
<Class name="software.amazon.awssdk.checksums.internal.SdkCrc32CChecksum"/>
308+
<Class name="software.amazon.awssdk.checksums.internal.SdkCrc32Checksum"/>
309+
</Or>
310+
<Bug pattern="SA_FIELD_SELF_ASSIGNMENT"/>
313311
</Match>
314312

315313
<!-- Suppress existing blocking call. -->

core/auth-crt/src/test/java/software/amazon/awssdk/authcrt/signer/internal/checksum/CrtBasedChecksumTest.java

Lines changed: 0 additions & 127 deletions
This file was deleted.

core/sdk-core/src/main/java/software/amazon/awssdk/core/checksums/Crc32CChecksum.java

Lines changed: 10 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -15,85 +15,23 @@
1515

1616
package software.amazon.awssdk.core.checksums;
1717

18-
import static software.amazon.awssdk.core.internal.util.HttpChecksumUtils.longToByte;
19-
20-
import java.lang.reflect.Method;
21-
import java.util.Arrays;
22-
import java.util.zip.Checksum;
23-
import software.amazon.awssdk.annotations.SdkInternalApi;
24-
import software.amazon.awssdk.core.internal.checksums.factory.CrtBasedChecksumProvider;
25-
import software.amazon.awssdk.core.internal.checksums.factory.SdkCrc32C;
18+
import software.amazon.awssdk.annotations.SdkProtectedApi;
19+
import software.amazon.awssdk.checksums.DefaultChecksumAlgorithm;
20+
import software.amazon.awssdk.core.internal.checksums.LegacyDelegatingChecksum;
2621

2722
/**
2823
* Implementation of {@link SdkChecksum} to calculate an CRC32C checksum.
24+
* <p>
25+
* Implementor notes: this should've been an internal API, but we can't change it now since it's not within the internal
26+
* subpackage.
27+
*
2928
* @deprecated this class is deprecated and subject to removal.
3029
*/
3130
@Deprecated
32-
@SdkInternalApi
33-
public class Crc32CChecksum implements SdkChecksum {
34-
35-
private Checksum crc32c;
36-
private Checksum lastMarkedCrc32C;
37-
private final boolean isCrtBasedChecksum;
31+
@SdkProtectedApi
32+
public class Crc32CChecksum extends LegacyDelegatingChecksum {
3833

39-
/**
40-
* Creates CRT Based Crc32C checksum if Crt classpath for Crc32c is loaded, else create Sdk Implemented Crc32c
41-
*/
4234
public Crc32CChecksum() {
43-
crc32c = CrtBasedChecksumProvider.createCrc32C();
44-
isCrtBasedChecksum = crc32c != null;
45-
if (!isCrtBasedChecksum) {
46-
crc32c = SdkCrc32C.create();
47-
}
48-
}
49-
50-
@Override
51-
public byte[] getChecksumBytes() {
52-
return Arrays.copyOfRange(longToByte(crc32c.getValue()), 4, 8);
53-
}
54-
55-
56-
@Override
57-
public void mark(int readLimit) {
58-
this.lastMarkedCrc32C = cloneChecksum(crc32c);
59-
}
60-
61-
@Override
62-
public void update(int b) {
63-
crc32c.update(b);
64-
}
65-
66-
@Override
67-
public void update(byte[] b, int off, int len) {
68-
crc32c.update(b, off, len);
69-
}
70-
71-
@Override
72-
public long getValue() {
73-
return crc32c.getValue();
74-
}
75-
76-
@Override
77-
public void reset() {
78-
if (lastMarkedCrc32C == null) {
79-
crc32c.reset();
80-
} else {
81-
crc32c = cloneChecksum(lastMarkedCrc32C);
82-
}
83-
}
84-
85-
86-
private Checksum cloneChecksum(Checksum checksum) {
87-
if (isCrtBasedChecksum) {
88-
try {
89-
Method method = checksum.getClass().getDeclaredMethod("clone");
90-
return (Checksum) method.invoke(checksum);
91-
} catch (ReflectiveOperationException e) {
92-
throw new IllegalStateException("Could not clone checksum class " + checksum.getClass(), e);
93-
}
94-
} else {
95-
return (Checksum) ((SdkCrc32C) checksum).clone();
96-
97-
}
35+
super(DefaultChecksumAlgorithm.CRC32C);
9836
}
9937
}

core/sdk-core/src/main/java/software/amazon/awssdk/core/checksums/Crc32Checksum.java

Lines changed: 11 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -15,82 +15,24 @@
1515

1616
package software.amazon.awssdk.core.checksums;
1717

18-
import static software.amazon.awssdk.core.internal.util.HttpChecksumUtils.longToByte;
19-
20-
import java.lang.reflect.Method;
21-
import java.util.Arrays;
22-
import java.util.zip.Checksum;
23-
import software.amazon.awssdk.annotations.SdkInternalApi;
24-
import software.amazon.awssdk.core.internal.checksums.factory.CrtBasedChecksumProvider;
25-
import software.amazon.awssdk.core.internal.checksums.factory.SdkCrc32;
18+
import software.amazon.awssdk.annotations.SdkProtectedApi;
19+
import software.amazon.awssdk.checksums.DefaultChecksumAlgorithm;
20+
import software.amazon.awssdk.core.internal.checksums.LegacyDelegatingChecksum;
2621

2722
/**
2823
* Implementation of {@link SdkChecksum} to calculate an CRC32 checksum.
24+
*
25+
* <p>
26+
* Implementor notes: this should've been an internal API, but we can't change it now since it's not within the internal
27+
* subpackage.
28+
*
2929
* @deprecated this class is deprecated and subject to removal.
3030
*/
3131
@Deprecated
32-
@SdkInternalApi
33-
public class Crc32Checksum implements SdkChecksum {
32+
@SdkProtectedApi
33+
public class Crc32Checksum extends LegacyDelegatingChecksum {
3434

35-
private Checksum crc32;
36-
private Checksum lastMarkedCrc32;
37-
private final boolean isCrtBasedChecksum;
38-
39-
/**
40-
* Creates CRT Based Crc32 checksum if Crt classpath for Crc32 is loaded, else create Sdk Implemented Crc32.
41-
*/
4235
public Crc32Checksum() {
43-
crc32 = CrtBasedChecksumProvider.createCrc32();
44-
isCrtBasedChecksum = crc32 != null;
45-
if (!isCrtBasedChecksum) {
46-
crc32 = SdkCrc32.create();
47-
}
48-
}
49-
50-
@Override
51-
public byte[] getChecksumBytes() {
52-
return Arrays.copyOfRange(longToByte(crc32.getValue()), 4, 8);
53-
}
54-
55-
@Override
56-
public void mark(int readLimit) {
57-
this.lastMarkedCrc32 = cloneChecksum(crc32);
58-
}
59-
60-
@Override
61-
public void update(int b) {
62-
crc32.update(b);
63-
}
64-
65-
@Override
66-
public void update(byte[] b, int off, int len) {
67-
crc32.update(b, off, len);
68-
}
69-
70-
@Override
71-
public long getValue() {
72-
return crc32.getValue();
73-
}
74-
75-
@Override
76-
public void reset() {
77-
if ((lastMarkedCrc32 == null)) {
78-
crc32.reset();
79-
} else {
80-
crc32 = cloneChecksum(lastMarkedCrc32);
81-
}
82-
}
83-
84-
private Checksum cloneChecksum(Checksum checksum) {
85-
if (isCrtBasedChecksum) {
86-
try {
87-
Method method = checksum.getClass().getDeclaredMethod("clone");
88-
return (Checksum) method.invoke(checksum);
89-
} catch (ReflectiveOperationException e) {
90-
throw new IllegalStateException("Could not clone checksum class " + checksum.getClass(), e);
91-
}
92-
} else {
93-
return (Checksum) ((SdkCrc32) checksum).clone();
94-
}
36+
super(DefaultChecksumAlgorithm.CRC32);
9537
}
9638
}

0 commit comments

Comments
 (0)