Compressing Streams: Parity Checksum : Checksum « File « Java Tutorial






import java.util.zip.Checksum;

public class ParityChecksum implements Checksum {
  long checksum = 0;

  public void update(int b) {
    int numOneBits = 0;
    for (int i = 1; i < 256; i *= 2) {
      if ((b & i) != 0)
        numOneBits++;
    }
    checksum = (checksum + numOneBits) % 2;
  }

  public void update(byte data[], int offset, int length) {
    for (int i = offset; i < offset + length; i++) {
      this.update(data[i]);
    }
  }

  public long getValue() {
    return checksum;
  }

  public void reset() {
    checksum = 0;
  }
}








11.69.Checksum
11.69.1.A single checksum calculation for multiple files
11.69.2.Compressing Streams: Parity Checksum
11.69.3.Compressing Streams: File Summer
11.69.4.Calculating the Checksum of a Byte Array (Compute Adler-32 checksum)
11.69.5.Compute CRC-32 checksum
11.69.6.Calculating the Checksum of a File
11.69.7.The java.util.zip package can be used to create a checksum.
11.69.8.Check sum for an InputStream