Code example for Checksum

Methods: getValue, reset

0
     if ( size <= 0 ) {
       return 0; 
     } 
 
     if ( type == CHECKSUM_CRC32 ) {
       out.writeInt( (int) summer.getValue() );
     } else { 
       throw new IOException( "Unknown Checksum " + type );
     } 
      
     if ( reset ) {
       reset(); 
     } 
      
     return size;
   } 
    
   /** 
    * Writes the current checksum to a buffer. 
    * If <i>reset</i> is true, then resets the checksum. 
    * @return number of bytes written. Will be equal to getChecksumSize(); 
    */ 
    public int writeValue( byte[] buf, int offset, boolean reset )
                           throws IOException { 
      if ( size <= 0 ) {
        return 0; 
      } 
 
      if ( type == CHECKSUM_CRC32 ) {
        int checksum = (int) summer.getValue();
        buf[offset+0] = (byte) ((checksum >>> 24) & 0xff);
        buf[offset+1] = (byte) ((checksum >>> 16) & 0xff);
        buf[offset+2] = (byte) ((checksum >>> 8) & 0xff);
        buf[offset+3] = (byte) (checksum & 0xff);
      } else { 
        throw new IOException( "Unknown Checksum " + type );
      } 
       
      if ( reset ) {
        reset(); 
      } 
       
      return size;
    } 
    
   /** 
    * Compares the checksum located at buf[offset] with the current checksum. 
    * @return true if the checksum matches and false otherwise. 
    */ 
   public boolean compare( byte buf[], int offset ) {
     if ( size > 0 && type == CHECKSUM_CRC32 ) {
       int checksum = ( (buf[offset+0] & 0xff) << 24 ) | 
                      ( (buf[offset+1] & 0xff) << 16 ) |
                      ( (buf[offset+2] & 0xff) << 8 )  |
                      ( (buf[offset+3] & 0xff) );
       return checksum == (int) summer.getValue();
     } 
     return size == 0;
   } 
    
  private final int type;
  private final int size;
  private final Checksum summer;
  private final int bytesPerChecksum;
  private int inSum = 0;
   
  private DataChecksum( int checksumType, Checksum checksum,
                        int sumSize, int chunkSize ) {
    type = checksumType;
    summer = checksum;
    size = sumSize;
    bytesPerChecksum = chunkSize;
  } 
   
  // Accessors 
  public int getChecksumType() { 
    return type;
  } 
  public int getChecksumSize() { 
    return size;
  } 
  public int getBytesPerChecksum() { 
    return bytesPerChecksum;
  } 
  public int getNumBytesInSum() { 
    return inSum;
  } 
   
  public static final int SIZE_OF_INTEGER = Integer.SIZE / Byte.SIZE;
  static public int getChecksumHeaderSize() { 
    return 1 + SIZE_OF_INTEGER; // type byte, bytesPerChecksum int
  } 
  //Checksum Interface. Just a wrapper around member summer. 
  public long getValue() { 
    return summer.getValue();
  } 
  public void reset() { 
    summer.reset();
    inSum = 0;
  } 
  public void update( byte[] b, int off, int len ) {
    if ( len > 0 ) {
      summer.update( b, off, len );