Create my own java.util.zip checksum class

Implement the Checksum interface. This interface defines the following methods:

   long getValue();
   void reset();
   void update(byte[] b, int off, int len);
   void update(int b);

The following example creates a checksum that is based on the following algorithm:

   take remainder of (the sum of the byte values divided by 999999)

The distribution of this checksum is pretty bad, but that’s not the point :)

Main.java:

import java.util.zip.*;
import java.io.*;
 
public class Main {
   public static void main(String args[]) throws Exception {
      CheckedOutputStream cis = new CheckedOutputStream(new FileOutputStream("testfile"), 
                                                        new MyChecksum());
 
      cis.write("abcdef".getBytes());
      cis.close();
 
      System.out.println("Checksum of outputstream = " + 
                               + cis.getChecksum().getValue());
    }
}
 
class MyChecksum implements Checksum
{
   long value = 0;
 
   public long getValue() {
      return value % 999999;
   }
 
   public void update(int b) {
      value += b;
   }
 
   public void update(byte[] b, int off, int len) {
      for (int i=0; i<len; i++) {
         value += (int) b[off+i];
      }
   }
 
   public void reset() {
      value = 0;
   }
}

outputs:

597 			(since "abcdef" = 97+98+99+100+101+102 = 597)