Java : Checksum with Examples
Checksum (Java SE 18 & JDK 18) API Examples.
You will find code examples on most Checksum methods.
Summary
final byte[] b = "abcd".getBytes();
System.out.println(Arrays.toString(b)); // [97, 98, 99, 100]
{
final Checksum checksum = new CRC32();
checksum.update(b);
System.out.printf("%x%n", checksum.getValue()); // ed82cd11
}
{
final Checksum checksum = new Adler32();
checksum.update(b);
System.out.printf("%x%n", checksum.getValue()); // 3d8018b
}
Methods
long getValue ()
final var b = "abcd".getBytes();
System.out.println(Arrays.toString(b)); // [97, 98, 99, 100]
final Checksum checksum = new CRC32();
checksum.update(b);
System.out.printf("%x%n", checksum.getValue()); // ed82cd11
final Checksum checksum = new CRC32();
checksum.update(97);
System.out.printf("%x%n", checksum.getValue()); // e8b7be43
checksum.update(98);
System.out.printf("%x%n", checksum.getValue()); // 9e83486d
checksum.update(99);
System.out.printf("%x%n", checksum.getValue()); // 352441c2
checksum.update(100);
System.out.printf("%x%n", checksum.getValue()); // ed82cd11
void reset ()
final Checksum checksum = new CRC32();
System.out.printf("%x%n", checksum.getValue()); // 0
checksum.update("abcd".getBytes());
System.out.printf("%x%n", checksum.getValue()); // ed82cd11
checksum.reset();
System.out.printf("%x%n", checksum.getValue()); // 0
default void update (byte[] b)
Please see getValue().
void update (byte[] b, int off, int len)
final var b = "abcdXYZ".getBytes();
System.out.println(Arrays.toString(b)); // [97, 98, 99, 100, 88, 89, 90]
final Checksum checksum = new CRC32();
checksum.update(b, 0, 4);
System.out.printf("%x%n", checksum.getValue()); // ed82cd11
checksum.reset();
checksum.update(b, 4, 3);
System.out.printf("%x%n", checksum.getValue()); // 7d29f8ed
final Checksum checksum = new CRC32();
checksum.update("abcd".getBytes());
System.out.printf("%x%n", checksum.getValue()); // ed82cd11
checksum.reset();
checksum.update("XYZ".getBytes());
System.out.printf("%x%n", checksum.getValue()); // 7d29f8ed
void update (int b)
Please see getValue().
default void update (ByteBuffer buffer)
final var buffer = ByteBuffer.wrap("abcd".getBytes());
System.out.println(buffer); // java.nio.HeapByteBuffer[pos=0 lim=4 cap=4]
final Checksum checksum = new CRC32();
checksum.update(buffer);
System.out.printf("%x%n", checksum.getValue()); // ed82cd11
System.out.println(buffer); // java.nio.HeapByteBuffer[pos=4 lim=4 cap=4]