dcsimg

How do I calculate the checksum of a byte array?

John Zukowski

The Adler32 class in the java.util.zip package can be used to calculate the sum:

byte buffer[] = ...
ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
CheckedInputStream cis = new CheckedInputStream(bais, new Adler32());
byte readBuffer[] = new byte[128];
while (cis.read(readBuffer) >= 0);
long value = cis.getChecksum().getValue();