source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/types/selectors/modifiedselector/DigestAlgorithm.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

File size: 6.1 KB
Line 
1/*
2 * Copyright 2003-2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18package org.apache.tools.ant.types.selectors.modifiedselector;
19
20
21import java.io.File;
22import java.io.FileInputStream;
23import java.io.FileOutputStream;
24import java.security.DigestInputStream;
25import java.security.MessageDigest;
26import java.security.NoSuchAlgorithmException;
27import java.security.NoSuchProviderException;
28import org.apache.tools.ant.BuildException;
29
30
31/**
32 * Computes a 'hashvalue' for the content of file using
33 * java.security.MessageDigest.
34 * Use of this algorithm doesn't require any additional nested <param>s.
35 * Supported <param>s are:
36 * <table>
37 * <tr>
38 * <th>name</th><th>values</th><th>description</th><th>required</th>
39 * </tr>
40 * <tr>
41 * <td> algorithm.algorithm </td>
42 * <td> MD5 | SHA (default provider) </td>
43 * <td> name of the algorithm the provider should use </td>
44 * <td> no, defaults to MD5 </td>
45 * </tr>
46 * <tr>
47 * <td> algorithm.provider </td>
48 * <td> </td>
49 * <td> name of the provider to use </td>
50 * <td> no, defaults to <i>null</i> </td>
51 * </tr>
52 * </table>
53 *
54 * @version 2003-09-13
55 * @since Ant 1.6
56 */
57public class DigestAlgorithm implements Algorithm {
58
59
60 // ----- member variables -----
61
62
63 /**
64 * MessageDigest algorithm to be used.
65 */
66 private String algorithm = "MD5";
67
68 /**
69 * MessageDigest Algorithm provider
70 */
71 private String provider = null;
72
73 /**
74 * Message Digest instance
75 */
76 private MessageDigest messageDigest = null;
77
78 /**
79 * Size of the read buffer to use.
80 */
81 private int readBufferSize = 8 * 1024;
82
83
84 // ----- Algorithm-Configuration -----
85
86
87 /**
88 * Specifies the algorithm to be used to compute the checksum.
89 * Defaults to "MD5". Other popular algorithms like "SHA" may be used as well.
90 * @param algorithm the digest algorithm to use
91 */
92 public void setAlgorithm(String algorithm) {
93 this.algorithm = algorithm;
94 }
95
96
97 /**
98 * Sets the MessageDigest algorithm provider to be used
99 * to calculate the checksum.
100 * @param provider provider to use
101 */
102 public void setProvider(String provider) {
103 this.provider = provider;
104 }
105
106
107 /** Initialize the security message digest. */
108 public void initMessageDigest() {
109 if (messageDigest != null) {
110 return;
111 }
112
113 if ((provider != null) && !"".equals(provider) && !"null".equals(provider)) {
114 try {
115 messageDigest = MessageDigest.getInstance(algorithm, provider);
116 } catch (NoSuchAlgorithmException noalgo) {
117 throw new BuildException(noalgo);
118 } catch (NoSuchProviderException noprovider) {
119 throw new BuildException(noprovider);
120 }
121 } else {
122 try {
123 messageDigest = MessageDigest.getInstance(algorithm);
124 } catch (NoSuchAlgorithmException noalgo) {
125 throw new BuildException(noalgo);
126 }
127 }
128 }
129
130
131 // ----- Logic -----
132
133
134 /**
135 * This algorithm doesn't need any configuration.
136 * Therefore it's always valid.
137 * @return <i>true</i> if all is ok, otherwise <i>false</i>.
138 */
139 public boolean isValid() {
140 return true;
141 }
142
143
144 /**
145 * Computes a value for a file content with the specified digest algorithm.
146 * @param file File object for which the value should be evaluated.
147 * @return The value for that file
148 */
149 // implementation adapted from ...taskdefs.Checksum, thanks to Magesh for hint
150 public String getValue(File file) {
151 initMessageDigest();
152 String checksum = null;
153 try {
154 if (!file.canRead()) {
155 return null;
156 }
157 FileInputStream fis = null;
158 FileOutputStream fos = null;
159 byte[] buf = new byte[readBufferSize];
160 try {
161 messageDigest.reset();
162 fis = new FileInputStream(file);
163 DigestInputStream dis = new DigestInputStream(fis,
164 messageDigest);
165 while (dis.read(buf, 0, readBufferSize) != -1) {
166 // do nothing
167 }
168 dis.close();
169 fis.close();
170 fis = null;
171 byte[] fileDigest = messageDigest.digest();
172 StringBuffer checksumSb = new StringBuffer();
173 for (int i = 0; i < fileDigest.length; i++) {
174 String hexStr = Integer.toHexString(0x00ff & fileDigest[i]);
175 if (hexStr.length() < 2) {
176 checksumSb.append("0");
177 }
178 checksumSb.append(hexStr);
179 }
180 checksum = checksumSb.toString();
181 } catch (Exception e) {
182 return null;
183 }
184 } catch (Exception e) {
185 return null;
186 }
187 return checksum;
188 }
189
190
191 /**
192 * Override Object.toString().
193 * @return some information about this algorithm.
194 */
195 public String toString() {
196 StringBuffer buf = new StringBuffer();
197 buf.append("<DigestAlgorithm:");
198 buf.append("algorithm=").append(algorithm);
199 buf.append(";provider=").append(provider);
200 buf.append(">");
201 return buf.toString();
202 }
203}
Note: See TracBrowser for help on using the repository browser.