KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > types > selectors > modifiedselector > ChecksumAlgorithm


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.types.selectors.modifiedselector;
20
21
22 import java.util.zip.Checksum JavaDoc;
23 import java.util.zip.CRC32 JavaDoc;
24 import java.util.zip.Adler32 JavaDoc;
25 import java.util.zip.CheckedInputStream JavaDoc;
26 import java.io.File JavaDoc;
27 import java.io.FileInputStream JavaDoc;
28 import java.io.BufferedInputStream JavaDoc;
29 import java.security.NoSuchAlgorithmException JavaDoc;
30 import org.apache.tools.ant.BuildException;
31
32
33 /**
34  * Computes a 'checksum' for the content of file using
35  * java.util.zip.CRC32 and java.util.zip.Adler32.
36  * Use of this algorithm doesn't require any additional nested <param>s.
37  * Supported <param>s are:
38  * <table>
39  * <tr>
40  * <th>name</th><th>values</th><th>description</th><th>required</th>
41  * </tr>
42  * <tr>
43  * <td> algorithm.algorithm </td>
44  * <td> ADLER | CRC ( default ) </td>
45  * <td> name of the algorithm the checksum should use </td>
46  * <td> no, defaults to CRC </td>
47  * </tr>
48  * </table>
49  *
50  * @version 2004-06-17
51  * @since Ant 1.7
52  */

53 public class ChecksumAlgorithm implements Algorithm {
54
55
56     // ----- member variables -----
57

58
59     /**
60      * Checksum algorithm to be used.
61      */

62     private String JavaDoc algorithm = "CRC";
63
64     /**
65      * Checksum interface instance
66      */

67     private Checksum JavaDoc checksum = null;
68
69
70     // ----- Algorithm-Configuration -----
71

72
73     /**
74      * Specifies the algorithm to be used to compute the checksum.
75      * Defaults to "CRC". Other popular algorithms like "ADLER" may be used as well.
76      * @param algorithm the digest algorithm to use
77      */

78     public void setAlgorithm(String JavaDoc algorithm) {
79         this.algorithm = algorithm;
80     }
81
82
83     /** Initialize the checksum interface. */
84     public void initChecksum() {
85         if (checksum != null) {
86             return;
87         }
88         if ("CRC".equalsIgnoreCase(algorithm)) {
89             checksum = new CRC32 JavaDoc();
90         } else if ("ADLER".equalsIgnoreCase(algorithm)) {
91             checksum = new Adler32 JavaDoc();
92         } else {
93             throw new BuildException(new NoSuchAlgorithmException JavaDoc());
94         }
95     }
96
97
98     // ----- Logic -----
99

100
101     /**
102      * This algorithm supports only CRC and Adler.
103      * @return <i>true</i> if all is ok, otherwise <i>false</i>.
104      */

105     public boolean isValid() {
106         return "CRC".equalsIgnoreCase(algorithm) || "ADLER".equalsIgnoreCase(algorithm);
107     }
108
109
110     /**
111      * Computes a value for a file content with the specified checksum algorithm.
112      * @param file File object for which the value should be evaluated.
113      * @return The value for that file
114      */

115     public String JavaDoc getValue(File JavaDoc file) {
116         initChecksum();
117         String JavaDoc rval = null;
118
119         try {
120             if (file.canRead()) {
121                  checksum.reset();
122                  FileInputStream JavaDoc fis = new FileInputStream JavaDoc(file);
123                  CheckedInputStream JavaDoc check = new CheckedInputStream JavaDoc(fis, checksum);
124                  BufferedInputStream JavaDoc in = new BufferedInputStream JavaDoc(check);
125                  while (in.read() != -1) {
126                      // Read the file
127
}
128                  rval = Long.toString(check.getChecksum().getValue());
129                  in.close();
130             }
131         } catch (Exception JavaDoc e) {
132             rval = null;
133         }
134         return rval;
135     }
136
137
138     /**
139      * Override Object.toString().
140      * @return some information about this algorithm.
141      */

142     public String JavaDoc toString() {
143         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
144         buf.append("<ChecksumAlgorithm:");
145         buf.append("algorithm=").append(algorithm);
146         buf.append(">");
147         return buf.toString();
148     }
149 }
150
Popular Tags