Java : Adler32 with Examples
Adler32 (Java SE 18 & JDK 18) API Examples.
You will find code examples on most Adler32 methods.
Summary
A class that can be used to compute the Adler-32 checksum of a data stream. An Adler-32 checksum is almost as reliable as a CRC-32 but can be computed much faster.
final byte[] b = "abcd".getBytes();
System.out.println(Arrays.toString(b)); // [97, 98, 99, 100]
final var checksum = new Adler32();
checksum.update(b);
System.out.printf("%x%n", checksum.getValue()); // 3d8018b
Constructors
Adler32 ()
Creates a new Adler32 object.
final var checksum = new Adler32();
System.out.printf("%x%n", checksum.getValue()); // 1
checksum.update(123);
System.out.printf("%x%n", checksum.getValue()); // 7c007c
Methods
long getValue ()
Returns the checksum value.
final var b = "abcd".getBytes();
System.out.println(Arrays.toString(b)); // [97, 98, 99, 100]
final var checksum = new Adler32();
checksum.update(b);
System.out.printf("%x%n", checksum.getValue()); // 3d8018b
final var checksum = new Adler32();
checksum.update(97);
System.out.printf("%x%n", checksum.getValue()); // 620062
checksum.update(98);
System.out.printf("%x%n", checksum.getValue()); // 12600c4
checksum.update(99);
System.out.printf("%x%n", checksum.getValue()); // 24d0127
checksum.update(100);
System.out.printf("%x%n", checksum.getValue()); // 3d8018b
void reset ()
Resets the checksum to initial value.
final var checksum = new Adler32();
System.out.printf("%x%n", checksum.getValue()); // 1
checksum.update("abcd".getBytes());
System.out.printf("%x%n", checksum.getValue()); // 3d8018b
checksum.reset();
System.out.printf("%x%n", checksum.getValue()); // 1
void update (byte[] b, int off, int len)
Updates the 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 Adler32();
checksum.update(b, 0, 4);
System.out.printf("%x%n", checksum.getValue()); // 3d8018b
checksum.reset();
checksum.update(b, 4, 3);
System.out.printf("%x%n", checksum.getValue()); // 217010c
final var checksum = new Adler32();
checksum.update("abcd".getBytes());
System.out.printf("%x%n", checksum.getValue()); // 3d8018b
checksum.reset();
checksum.update("XYZ".getBytes());
System.out.printf("%x%n", checksum.getValue()); // 217010c
void update (int b)
Updates the checksum with the specified byte (the low eight bits of the argument b).
Please see getValue().
void update (ByteBuffer buffer)
Updates the 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 Adler32();
checksum.update(buffer);
System.out.printf("%x%n", checksum.getValue()); // 3d8018b
System.out.println(buffer); // java.nio.HeapByteBuffer[pos=4 lim=4 cap=4]