Java : CRC32C with Examples

CRC32C (Java SE 18 & JDK 18) API Examples.
You will find code examples on most CRC32C methods.


Summary

A class that can be used to compute the CRC-32C of a data stream.

Class diagram

final byte[] b = "abcd".getBytes();
System.out.println(Arrays.toString(b)); // [97, 98, 99, 100]

final var checksum = new CRC32C();

checksum.update(b);
System.out.printf("%x%n", checksum.getValue()); // 92c80a31

Constructors

CRC32C ()

Creates a new CRC32C object.

final var checksum = new CRC32C();
System.out.printf("%x%n", checksum.getValue()); // 0

checksum.update(123);
System.out.printf("%x%n", checksum.getValue()); // ba6cac67

Methods

long getValue ()

Returns CRC-32C value.

final var b = "abcd".getBytes();
System.out.println(Arrays.toString(b)); // [97, 98, 99, 100]

final var checksum = new CRC32C();

checksum.update(b);
System.out.printf("%x%n", checksum.getValue()); // 92c80a31
final var checksum = new CRC32C();

checksum.update(97);
System.out.printf("%x%n", checksum.getValue()); // c1d04330

checksum.update(98);
System.out.printf("%x%n", checksum.getValue()); // e2a22936

checksum.update(99);
System.out.printf("%x%n", checksum.getValue()); // 364b3fb7

checksum.update(100);
System.out.printf("%x%n", checksum.getValue()); // 92c80a31

void reset ()

Resets CRC-32C to initial value.

final var checksum = new CRC32C();
System.out.printf("%x%n", checksum.getValue()); // 0

checksum.update("abcd".getBytes());
System.out.printf("%x%n", checksum.getValue()); // 92c80a31

checksum.reset();
System.out.printf("%x%n", checksum.getValue()); // 0

void update (byte[] b, int off, int len)

Updates the CRC-32C checksum with the specified array of bytes.

final var b = "abcdXYZ".getBytes();
System.out.println(Arrays.toString(b)); // [97, 98, 99, 100, 88, 89, 90]

final var checksum = new CRC32C();

checksum.update(b, 0, 4);
System.out.printf("%x%n", checksum.getValue()); // 92c80a31

checksum.reset();
checksum.update(b, 4, 3);
System.out.printf("%x%n", checksum.getValue()); // 9b51fe4d
final var checksum = new CRC32C();

checksum.update("abcd".getBytes());
System.out.printf("%x%n", checksum.getValue()); // 92c80a31

checksum.reset();
checksum.update("XYZ".getBytes());
System.out.printf("%x%n", checksum.getValue()); // 9b51fe4d

void update (int b)

Updates the CRC-32C checksum with the specified byte (the low eight bits of the argument b).

Please see getValue().

void update (ByteBuffer buffer)

Updates the CRC-32C checksum with the bytes from the specified buffer.

final var buffer = ByteBuffer.wrap("abcd".getBytes());
System.out.println(buffer); // java.nio.HeapByteBuffer[pos=0 lim=4 cap=4]

final var checksum = new CRC32C();

checksum.update(buffer);
System.out.printf("%x%n", checksum.getValue()); // 92c80a31

System.out.println(buffer); // java.nio.HeapByteBuffer[pos=4 lim=4 cap=4]

Methods declared in Checksum

update

Please see the link below.


Related posts

To top of page