A regenerated binary artifact differs from its fixture. Instead of reporting only a failed equality check, give the maintainer the first offset to inspect. Java's Files.mismatch compares the file contents and returns a byte position, not a character position or a total difference count. It can also identify the end of a shorter file that otherwise matches byte for byte.

Prerequisites and fixture

Use JDK 21 with javac and java on your path. Save as MismatchDemo.java in a writable working directory. The example creates and removes two small binary files in a private temporary subdirectory. It does not compare any existing file.

import java.nio.file.Files;
import java.nio.file.Path;

public class MismatchDemo {
    static void check(String label, Path a, Path b, long expected) throws Exception {
        long actual = Files.mismatch(a, b);
        if (actual != expected) throw new AssertionError(label + ": " + actual);
        System.out.println(label + ": " + actual);
    }
    public static void main(String[] args) throws Exception {
        Path dir = Files.createTempDirectory(Path.of("."), "compare-");
        Path a = dir.resolve("expected.bin");
        Path b = dir.resolve("received.bin");
        try {
            Files.write(a, new byte[]{10, 20, 30});
            Files.write(b, new byte[]{10, 20, 30});
            check("identical", a, b, -1);
            Files.write(b, new byte[]{10, 99, 30});
            check("changed", a, b, 1);
            Files.write(b, new byte[]{10, 20});
            check("truncated", a, b, 2);
            check("extended", b, a, 2);
            Files.write(b, new byte[0]);
            check("empty versus nonempty", a, b, 0);
        } finally {
            Files.deleteIfExists(b);
            Files.deleteIfExists(a);
            Files.deleteIfExists(dir);
        }
    }
}

Compile and run

javac MismatchDemo.java
java MismatchDemo
identical: -1
changed: 1
truncated: 2
extended: 2
empty versus nonempty: 0

The first byte is at offset zero. In the changed case, both files have a byte at offset 1 and those bytes differ. In the truncated case, offset 2 is the first position with no corresponding byte in the shorter file. Reversing the arguments does not change the offset. It changes which side ends first, which is information the caller must supply when choosing a human-facing label.

Use it in an artifact check

Keep the return value in a long. Report equality only for the -1 sentinel, and do not treat zero as a missing result. If the files are stable, compare the lengths with the offset to annotate a prefix mismatch. If the files are being generated concurrently, first arrange to compare completed artifacts. The API is not an atomic snapshot of both inputs. Report I/O failures separately from content differences so unreadable files never look like equal artifacts. For text diagnostics, decide whether you want byte equality or a policy such as ignoring line-ending differences before choosing this API.

JDK 21 Files.mismatch documentation · Java Samples Java tutorials