Statistics
| Revision:

root / tags / v2_0_0_Build_2051 / extensions / org.gvsig.installer / org.gvsig.installer.lib / org.gvsig.installer.lib.impl / src / main / java / org / gvsig / installer / lib / impl / utils / MD5BinaryFileUtils.java @ 38753

History | View | Annotate | Download (3.55 KB)

1
/* gvSIG. Desktop Geographic Information System.
2
 *
3
 * Copyright ? 2007-2012 gvSIG Association
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18
 * MA  02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us
21
 * at info AT gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.installer.lib.impl.utils;
24

    
25
import java.io.BufferedReader;
26
import java.io.File;
27
import java.io.FileInputStream;
28
import java.io.InputStream;
29
import java.io.InputStreamReader;
30
import java.net.URL;
31
import java.security.MessageDigest;
32

    
33
import org.slf4j.Logger;
34
import org.slf4j.LoggerFactory;
35

    
36

    
37
/**
38
 * @author jldominguez
39
 *
40
 */
41
public class MD5BinaryFileUtils {
42
    
43
    private static final Logger LOG = LoggerFactory.getLogger(MD5BinaryFileUtils.class);
44
    
45
    /**
46
     * 
47
     * Gets the MD5 string of a md5 file which is associated with
48
     * the file provided.
49
     * 
50
     * @param remote_file the file that has a ".md5" file next to it
51
     * @return
52
     */
53
    public static String getMD5InRemoteFile(URL remote_file) {
54

    
55
        BufferedReader in = null;
56
        try {
57
            String _url = remote_file.toString() + ".md5";
58
            URL md5_url = new URL(_url);
59
            
60
            in = new BufferedReader(
61
            new InputStreamReader(md5_url.openStream()));
62
            String inputLine = in.readLine();
63
            
64
            int space = inputLine.indexOf(" "); 
65
            inputLine = inputLine.substring(0, space);
66
            return inputLine;
67
        } catch (Exception ex) {
68
            LOG.info("Error while reading MD5 file: " + ex.getMessage());
69
            return null;
70
        } finally {
71
            try {
72
                in.close();
73
            } catch (Exception ex) { }  
74
        }
75
        
76
    }
77
    
78
    public static byte[] createChecksum(File the_file) throws Exception {
79
        
80
        InputStream fis =  new FileInputStream(the_file);
81

    
82
        byte[] buffer = new byte[1024];
83
        MessageDigest complete = MessageDigest.getInstance("MD5");
84
        int numRead;
85

    
86
        do {
87
            numRead = fis.read(buffer);
88
            if (numRead > 0) {
89
                complete.update(buffer, 0, numRead);
90
            }
91
        } while (numRead != -1);
92

    
93
        fis.close();
94
        return complete.digest();
95
    }
96

    
97
    /**
98
     * In case this is too slow for you:
99
     * There are faster ways to convert
100
     * a byte array to a HEX string
101
     *  
102
     * @param the_file the input file
103
     * @return the given file's MD5 checksum as a hex string
104
     * @throws Exception
105
     */
106
    public static String getMD5Checksum(File the_file) throws Exception {
107
        byte[] b = createChecksum(the_file);
108
        String result = "";
109

    
110
        for (int i=0; i < b.length; i++) {
111
            result += Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring( 1 );
112
        }
113
        return result;
114
    }
115

    
116
}