KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.File JavaDoc;
23
24
25 /**
26  * Computes a 'hashvalue' for the content of file using String.hashValue().
27  * Use of this algorithm doesn't require any additional nested <param>s and
28  * doesn't support any.
29  *
30  * @version 2003-09-13
31  * @since Ant 1.6
32  */

33 public class HashvalueAlgorithm implements Algorithm {
34
35     /**
36      * This algorithm doesn't need any configuration.
37      * Therefore it's always valid.
38      * @return always true
39      */

40     public boolean isValid() {
41         return true;
42     }
43
44     /**
45      * Computes a 'hashvalue' for a file content.
46      * It reads the content of a file, convert that to String and use the
47      * String.hashCode() method.
48      * @param file The file for which the value should be computed
49      * @return the hashvalue or <i>null</i> if the file couldn't be read
50      */

51      // Because the content is only read the file will not be damaged. I tested
52
// with JPG, ZIP and PDF as binary files.
53
public String JavaDoc getValue(File JavaDoc file) {
54         try {
55             if (!file.canRead()) {
56                 return null;
57             }
58             java.io.FileInputStream JavaDoc fis = new java.io.FileInputStream JavaDoc(file);
59             byte[] content = new byte[fis.available()];
60             fis.read(content);
61             fis.close();
62             String JavaDoc s = new String JavaDoc(content);
63             int hash = s.hashCode();
64             return Integer.toString(hash);
65         } catch (Exception JavaDoc e) {
66             return null;
67         }
68     }
69
70
71     /**
72      * Override Object.toString().
73      * @return information about this comparator
74      */

75     public String JavaDoc toString() {
76         return "HashvalueAlgorithm";
77     }
78
79 }
80
Popular Tags