A checksum integration needs agreement about the algorithm and the exact byte range. Java’s CRC32 and CRC32C are different algorithms even though both produce 32-bit values. When a storage or transport contract specifies CRC-32C, use that algorithm explicitly and test it before connecting a larger data pipeline.

This example computes a fixed checksum three ways: from a whole byte array, from two chunks and from a window inside a ByteBuffer. It also demonstrates that the buffer’s position changes when its remaining bytes are consumed.

Prerequisites

Use JDK 21 with both javac and java available. Save the complete source as ChecksumDemo.java. No dependencies or file downloads are required.

Complete example

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.zip.CRC32C;

public class ChecksumDemo {
    static void check(boolean condition) {
        if (!condition) throw new AssertionError();
    }
    public static void main(String[] args) {
        byte[] bytes = "123456789".getBytes(StandardCharsets.US_ASCII);
        CRC32C whole = new CRC32C();
        whole.update(bytes, 0, bytes.length);
        check(whole.getValue() == 0xe3069283L);
        CRC32C chunks = new CRC32C();
        chunks.update(bytes, 0, 4);
        chunks.update(bytes, 4, bytes.length - 4);
        check(chunks.getValue() == whole.getValue());
        byte[] framed = "x123456789y".getBytes(StandardCharsets.US_ASCII);
        ByteBuffer window = ByteBuffer.wrap(framed);
        window.position(1); window.limit(10);
        CRC32C buffer = new CRC32C();
        buffer.update(window);
        check(window.position() == window.limit());
        check(buffer.getValue() == whole.getValue());
        chunks.reset();
        check(chunks.getValue() == 0);
        System.out.printf("crc32c=%08x%n", whole.getValue());
        System.out.println("chunk, buffer and reset checks passed");
    }
}

Compile, run and expected output

javac ChecksumDemo.java
java ChecksumDemo
crc32c=e3069283
chunk, buffer and reset checks passed

Understand the byte ranges

The ASCII charset makes the fixture’s bytes explicit. In the array overload, the third argument is a length, so the second update starts at offset four and consumes the remaining five bytes. Updating in chunks preserves the checksum state and produces the same result as one update over the concatenated bytes.

The framed array has an extra byte at each end. Setting position to one and limit to ten selects the nine payload bytes. The ByteBuffer overload consumes that remaining range and advances position to limit. If another reader must retain its position, pass a duplicate buffer with an independently managed position and limit.

getValue() returns a long carrying the checksum value. Keeping the result as a long avoids accidentally presenting the same 32 bits as a negative signed integer. The eight-digit hexadecimal format provides a compact, consistent display.

Edge cases and troubleshooting

Reusing a checksum instance without reset continues the previous message’s calculation. Reset between independent payloads, or allocate one instance per operation. The example uses explicit checks that run without Java’s assertion-enabling flag.

If another implementation disagrees, first compare algorithm names and payload bytes. Check whether a newline, framing byte or encoded text prefix was included. A displayed hexadecimal checksum is also different from a protocol’s binary field: serialization byte order must follow that protocol’s specification.

CRC-32C can help detect accidental corruption. It does not authenticate the sender or provide protection against deliberate modification. This tutorial tests checksum mechanics only; it does not implement iSCSI framing or any storage service.

References and related tutorials

Use the official JDK 21 CRC32C documentation and RFC 3720 for the algorithm’s protocol context. Continue with Java tutorials.