001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.types.selectors.modifiedselector;
020:
021: import java.util.zip.Checksum;
022: import java.util.zip.CRC32;
023: import java.util.zip.Adler32;
024: import java.util.zip.CheckedInputStream;
025: import java.io.File;
026: import java.io.FileInputStream;
027: import java.io.BufferedInputStream;
028: import java.security.NoSuchAlgorithmException;
029: import org.apache.tools.ant.BuildException;
030:
031: /**
032: * Computes a 'checksum' for the content of file using
033: * java.util.zip.CRC32 and java.util.zip.Adler32.
034: * Use of this algorithm doesn't require any additional nested <param>s.
035: * Supported <param>s are:
036: * <table>
037: * <tr>
038: * <th>name</th><th>values</th><th>description</th><th>required</th>
039: * </tr>
040: * <tr>
041: * <td> algorithm.algorithm </td>
042: * <td> ADLER | CRC ( default ) </td>
043: * <td> name of the algorithm the checksum should use </td>
044: * <td> no, defaults to CRC </td>
045: * </tr>
046: * </table>
047: *
048: * @version 2004-06-17
049: * @since Ant 1.7
050: */
051: public class ChecksumAlgorithm implements Algorithm {
052:
053: // ----- member variables -----
054:
055: /**
056: * Checksum algorithm to be used.
057: */
058: private String algorithm = "CRC";
059:
060: /**
061: * Checksum interface instance
062: */
063: private Checksum checksum = null;
064:
065: // ----- Algorithm-Configuration -----
066:
067: /**
068: * Specifies the algorithm to be used to compute the checksum.
069: * Defaults to "CRC". Other popular algorithms like "ADLER" may be used as well.
070: * @param algorithm the digest algorithm to use
071: */
072: public void setAlgorithm(String algorithm) {
073: this .algorithm = algorithm;
074: }
075:
076: /** Initialize the checksum interface. */
077: public void initChecksum() {
078: if (checksum != null) {
079: return;
080: }
081: if ("CRC".equalsIgnoreCase(algorithm)) {
082: checksum = new CRC32();
083: } else if ("ADLER".equalsIgnoreCase(algorithm)) {
084: checksum = new Adler32();
085: } else {
086: throw new BuildException(new NoSuchAlgorithmException());
087: }
088: }
089:
090: // ----- Logic -----
091:
092: /**
093: * This algorithm supports only CRC and Adler.
094: * @return <i>true</i> if all is ok, otherwise <i>false</i>.
095: */
096: public boolean isValid() {
097: return "CRC".equalsIgnoreCase(algorithm)
098: || "ADLER".equalsIgnoreCase(algorithm);
099: }
100:
101: /**
102: * Computes a value for a file content with the specified checksum algorithm.
103: * @param file File object for which the value should be evaluated.
104: * @return The value for that file
105: */
106: public String getValue(File file) {
107: initChecksum();
108: String rval = null;
109:
110: try {
111: if (file.canRead()) {
112: checksum.reset();
113: FileInputStream fis = new FileInputStream(file);
114: CheckedInputStream check = new CheckedInputStream(fis,
115: checksum);
116: BufferedInputStream in = new BufferedInputStream(check);
117: while (in.read() != -1) {
118: // Read the file
119: }
120: rval = Long.toString(check.getChecksum().getValue());
121: in.close();
122: }
123: } catch (Exception e) {
124: rval = null;
125: }
126: return rval;
127: }
128:
129: /**
130: * Override Object.toString().
131: * @return some information about this algorithm.
132: */
133: public String toString() {
134: StringBuffer buf = new StringBuffer();
135: buf.append("<ChecksumAlgorithm:");
136: buf.append("algorithm=").append(algorithm);
137: buf.append(">");
138: return buf.toString();
139: }
140: }
|