Skip to content

Commit 1b8ab7b

Browse files
committed
Remove Java 8 code for CRC calculation
References #663
1 parent bd0e096 commit 1b8ab7b

File tree

2 files changed

+7
-91
lines changed

2 files changed

+7
-91
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2020-2023 Broadcom. All Rights Reserved.
1+
// Copyright (c) 2020-2024 Broadcom. All Rights Reserved.
22
// The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
33
//
44
// This software, the RabbitMQ Stream Java client library, is dual-licensed under the
@@ -16,117 +16,33 @@
1616

1717
import com.rabbitmq.stream.ChunkChecksum;
1818
import com.rabbitmq.stream.ChunkChecksumValidationException;
19-
import com.rabbitmq.stream.StreamException;
2019
import io.netty.buffer.ByteBuf;
21-
import io.netty.util.ByteProcessor;
22-
import java.lang.reflect.InvocationTargetException;
23-
import java.lang.reflect.Method;
24-
import java.nio.ByteBuffer;
2520
import java.util.function.Supplier;
2621
import java.util.zip.CRC32;
2722
import java.util.zip.Checksum;
28-
import org.slf4j.Logger;
29-
import org.slf4j.LoggerFactory;
3023

3124
class JdkChunkChecksum implements ChunkChecksum {
3225

33-
static final ChunkChecksum CRC32_SINGLETON;
34-
private static final Logger LOGGER = LoggerFactory.getLogger(JdkChunkChecksum.class);
3526
private static final Supplier<Checksum> CRC32_SUPPLIER = CRC32::new;
36-
37-
static {
38-
if (isChecksumUpdateByteBufferAvailable()) {
39-
LOGGER.debug("Checksum#update(ByteBuffer) method available, using it for direct buffers");
40-
CRC32_SINGLETON = new ByteBufferDirectByteBufChecksum(CRC32_SUPPLIER);
41-
} else {
42-
LOGGER.debug(
43-
"Checksum#update(ByteBuffer) method not available, using byte-by-byte CRC calculation for direct buffers");
44-
CRC32_SINGLETON = new JdkChunkChecksum(CRC32_SUPPLIER);
45-
}
46-
}
27+
static final ChunkChecksum CRC32_SINGLETON = new JdkChunkChecksum(CRC32_SUPPLIER);
4728

4829
private final Supplier<Checksum> checksumSupplier;
4930

50-
JdkChunkChecksum() {
51-
this(CRC32_SUPPLIER);
52-
}
53-
5431
JdkChunkChecksum(Supplier<Checksum> checksumSupplier) {
5532
this.checksumSupplier = checksumSupplier;
5633
}
5734

58-
private static boolean isChecksumUpdateByteBufferAvailable() {
59-
try {
60-
Checksum.class.getDeclaredMethod("update", ByteBuffer.class);
61-
return true;
62-
} catch (Exception e) {
63-
return false;
64-
}
65-
}
66-
6735
@Override
6836
public void checksum(ByteBuf byteBuf, long dataLength, long expected) {
6937
Checksum checksum = checksumSupplier.get();
7038
if (byteBuf.hasArray()) {
7139
checksum.update(
7240
byteBuf.array(), byteBuf.arrayOffset() + byteBuf.readerIndex(), byteBuf.readableBytes());
7341
} else {
74-
byteBuf.forEachByte(
75-
byteBuf.readerIndex(), byteBuf.readableBytes(), new UpdateProcessor(checksum));
42+
checksum.update(byteBuf.nioBuffer(byteBuf.readerIndex(), byteBuf.readableBytes()));
7643
}
7744
if (expected != checksum.getValue()) {
7845
throw new ChunkChecksumValidationException(expected, checksum.getValue());
7946
}
8047
}
81-
82-
private static final class ByteBufferDirectByteBufChecksum implements ChunkChecksum {
83-
84-
private final Supplier<Checksum> checksumSupplier;
85-
private final Method updateMethod;
86-
87-
private ByteBufferDirectByteBufChecksum(Supplier<Checksum> checksumSupplier) {
88-
this.checksumSupplier = checksumSupplier;
89-
try {
90-
this.updateMethod = Checksum.class.getDeclaredMethod("update", ByteBuffer.class);
91-
} catch (NoSuchMethodException e) {
92-
throw new StreamException("Error while looking up Checksum#update(ByteBuffer) method", e);
93-
}
94-
}
95-
96-
@Override
97-
public void checksum(ByteBuf byteBuf, long dataLength, long expected) {
98-
Checksum checksum = checksumSupplier.get();
99-
if (byteBuf.hasArray()) {
100-
checksum.update(
101-
byteBuf.array(),
102-
byteBuf.arrayOffset() + byteBuf.readerIndex(),
103-
byteBuf.readableBytes());
104-
} else {
105-
try {
106-
this.updateMethod.invoke(
107-
checksum, byteBuf.nioBuffer(byteBuf.readerIndex(), byteBuf.readableBytes()));
108-
} catch (IllegalAccessException | InvocationTargetException e) {
109-
throw new StreamException("Error while calculating CRC", e);
110-
}
111-
}
112-
if (expected != checksum.getValue()) {
113-
throw new ChunkChecksumValidationException(expected, checksum.getValue());
114-
}
115-
}
116-
}
117-
118-
private static class UpdateProcessor implements ByteProcessor {
119-
120-
private final Checksum checksum;
121-
122-
private UpdateProcessor(Checksum checksum) {
123-
this.checksum = checksum;
124-
}
125-
126-
@Override
127-
public boolean process(byte value) {
128-
checksum.update(value);
129-
return true;
130-
}
131-
}
13248
}

src/test/java/com/rabbitmq/stream/impl/JdkChunkChecksumTest.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2020-2023 Broadcom. All Rights Reserved.
1+
// Copyright (c) 2020-2024 Broadcom. All Rights Reserved.
22
// The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
33
//
44
// This software, the RabbitMQ Stream Java client library, is dual-licensed under the
@@ -35,10 +35,10 @@ public class JdkChunkChecksumTest {
3535

3636
static Charset UTF8 = StandardCharsets.UTF_8;
3737
static Map<String, Supplier<Checksum>> CHECKSUMS =
38-
new HashMap<String, Supplier<Checksum>>() {
38+
new HashMap<>() {
3939
{
40-
put("crc32", () -> new CRC32());
41-
put("adler32", () -> new Adler32());
40+
put("crc32", CRC32::new);
41+
put("adler32", Adler32::new);
4242
}
4343
};
4444

0 commit comments

Comments
 (0)