source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/types/selectors/modifiedselector/HashvalueAlgorithm.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: 2.4 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;
22
23
24/**
25 * Computes a 'hashvalue' for the content of file using String.hashValue().
26 * Use of this algorithm doesn't require any additional nested <param>s and
27 * doesn't support any.
28 *
29 * @version 2003-09-13
30 * @since Ant 1.6
31 */
32public class HashvalueAlgorithm implements Algorithm {
33
34 /**
35 * This algorithm doesn't need any configuration.
36 * Therefore it's always valid.
37 * @return always true
38 */
39 public boolean isValid() {
40 return true;
41 }
42
43 /**
44 * Computes a 'hashvalue' for a file content.
45 * It reads the content of a file, convert that to String and use the
46 * String.hashCode() method.
47 * @param file The file for which the value should be computed
48 * @return the hashvalue or <i>null</i> if the file couldn't be read
49 */
50 // Because the content is only read the file will not be damaged. I tested
51 // with JPG, ZIP and PDF as binary files.
52 public String getValue(File file) {
53 try {
54 if (!file.canRead()) {
55 return null;
56 }
57 java.io.FileInputStream fis = new java.io.FileInputStream(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 /**
71 * Override Object.toString().
72 * @return information about this comparator
73 */
74 public String toString() {
75 return "HashvalueAlgorithm";
76 }
77
78}
Note: See TracBrowser for help on using the repository browser.