01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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: import java.io.File;
22:
23: /**
24: * Computes a 'hashvalue' for the content of file using String.hashValue().
25: * Use of this algorithm doesn't require any additional nested <param>s and
26: * doesn't support any.
27: *
28: * @version 2003-09-13
29: * @since Ant 1.6
30: */
31: public class HashvalueAlgorithm implements Algorithm {
32:
33: /**
34: * This algorithm doesn't need any configuration.
35: * Therefore it's always valid.
36: * @return always true
37: */
38: public boolean isValid() {
39: return true;
40: }
41:
42: /**
43: * Computes a 'hashvalue' for a file content.
44: * It reads the content of a file, convert that to String and use the
45: * String.hashCode() method.
46: * @param file The file for which the value should be computed
47: * @return the hashvalue or <i>null</i> if the file couldn't be read
48: */
49: // Because the content is only read the file will not be damaged. I tested
50: // with JPG, ZIP and PDF as binary files.
51: public String getValue(File file) {
52: try {
53: if (!file.canRead()) {
54: return null;
55: }
56: java.io.FileInputStream fis = new java.io.FileInputStream(
57: file);
58: byte[] content = new byte[fis.available()];
59: fis.read(content);
60: fis.close();
61: String s = new String(content);
62: int hash = s.hashCode();
63: return Integer.toString(hash);
64: } catch (Exception e) {
65: return null;
66: }
67: }
68:
69: /**
70: * Override Object.toString().
71: * @return information about this comparator
72: */
73: public String toString() {
74: return "HashvalueAlgorithm";
75: }
76:
77: }
|