Centralize CRC32 computation
[controller.git] / atomix-storage / src / main / java / io / atomix / storage / journal / SegmentEntry.java
index be6c6ba831dd27e1af054cacc9eed407f7e297e7..432f9b9dec0395385c8e930c7eae3bbd51307ab2 100644 (file)
 package io.atomix.storage.journal;
 
 import java.nio.ByteBuffer;
+import java.util.zip.CRC32;
+import org.eclipse.jdt.annotation.NonNull;
 
 /**
  * An {@link Indexed} entry read from {@link JournalSegment}.
  *
- * @param checksum The CRC32 checksum of data
+ * @param checksum The {@link CRC32} checksum of data
  * @param bytes Entry bytes
  */
-record SegmentEntry(int checksum, ByteBuffer bytes) {
+record SegmentEntry(int checksum, @NonNull ByteBuffer bytes) {
     /**
      * The size of the header, comprising of:
      * <ul>
@@ -38,4 +40,16 @@ record SegmentEntry(int checksum, ByteBuffer bytes) {
             throw new IllegalArgumentException("Invalid entry bytes " + bytes);
         }
     }
+
+    /**
+     * Compute the {@link CRC32} checksum of a buffer. Note that the buffer will be consumed during this process.
+     *
+     * @param bytes buffer to checksum
+     * @return the checksum
+     */
+    static int computeChecksum(final ByteBuffer bytes) {
+        final var crc32 = new CRC32();
+        crc32.update(bytes);
+        return (int) crc32.getValue();
+    }
 }