FileDocCategorySizeDatePackage
ChecksumAlgorithm.javaAPI DocApache Ant 1.704381Wed Dec 13 06:16:22 GMT 2006org.apache.tools.ant.types.selectors.modifiedselector

ChecksumAlgorithm

public class ChecksumAlgorithm extends Object implements Algorithm
Computes a 'checksum' for the content of file using java.util.zip.CRC32 and java.util.zip.Adler32. Use of this algorithm doesn't require any additional nested s. Supported s are:
namevaluesdescriptionrequired
algorithm.algorithm ADLER | CRC ( default ) name of the algorithm the checksum should use no, defaults to CRC
version
2004-06-17
since
Ant 1.7

Fields Summary
private String
algorithm
Checksum algorithm to be used.
private Checksum
checksum
Checksum interface instance
Constructors Summary
Methods Summary
public java.lang.StringgetValue(java.io.File file)
Computes a value for a file content with the specified checksum algorithm.

param
file File object for which the value should be evaluated.
return
The value for that file

        initChecksum();
        String rval = null;

        try {
            if (file.canRead()) {
                 checksum.reset();
                 FileInputStream fis = new FileInputStream(file);
                 CheckedInputStream check = new CheckedInputStream(fis, checksum);
                 BufferedInputStream in = new BufferedInputStream(check);
                 while (in.read() != -1) {
                     // Read the file
                 }
                 rval = Long.toString(check.getChecksum().getValue());
                 in.close();
            }
        } catch (Exception e) {
            rval = null;
        }
        return rval;
    
public voidinitChecksum()
Initialize the checksum interface.

        if (checksum != null) {
            return;
        }
        if ("CRC".equalsIgnoreCase(algorithm)) {
            checksum = new CRC32();
        } else if ("ADLER".equalsIgnoreCase(algorithm)) {
            checksum = new Adler32();
        } else {
            throw new BuildException(new NoSuchAlgorithmException());
        }
    
public booleanisValid()
This algorithm supports only CRC and Adler.

return
true if all is ok, otherwise false.

        return "CRC".equalsIgnoreCase(algorithm) || "ADLER".equalsIgnoreCase(algorithm);
    
public voidsetAlgorithm(java.lang.String algorithm)
Specifies the algorithm to be used to compute the checksum. Defaults to "CRC". Other popular algorithms like "ADLER" may be used as well.

param
algorithm the digest algorithm to use



    // -----  Algorithm-Configuration  -----


                                       
        
        this.algorithm = algorithm;
    
public java.lang.StringtoString()
Override Object.toString().

return
some information about this algorithm.

        StringBuffer buf = new StringBuffer();
        buf.append("<ChecksumAlgorithm:");
        buf.append("algorithm=").append(algorithm);
        buf.append(">");
        return buf.toString();