Statistics
| Revision:

svn-gvsig-desktop / tags / v2_0_0_Build_2060 / extensions / org.gvsig.installer / org.gvsig.installer.lib / org.gvsig.installer.lib.impl / src / main / java / org / gvsig / installer / lib / impl / utils / MD5BinaryFileUtils.java @ 39380

History | View | Annotate | Download (3.89 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.net.URLConnection;
32
import java.security.MessageDigest;
33

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

    
37

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

    
56
        BufferedReader in = null;
57
        InputStreamReader istr = null;
58
        try {
59
            String _url = remote_file.toString() + ".md5";
60
            URL md5_url = new URL(_url);
61
            
62
            URLConnection connection = md5_url.openConnection();
63
            connection.setUseCaches(false);
64
            connection.setConnectTimeout(30000);
65
            connection.setReadTimeout(20000);
66
            
67
            istr = new InputStreamReader(connection.getInputStream());
68
            in = new BufferedReader(istr);
69
            String inputLine = in.readLine();
70
            
71
            int space = inputLine.indexOf(" "); 
72
            inputLine = inputLine.substring(0, space);
73
            return inputLine;
74
        } catch (Exception ex) {
75
            LOG.info("Error while reading MD5 file: " + ex.getMessage());
76
            return null;
77
        } finally {
78
            try {
79
                in.close();
80
                istr.close();
81
            } catch (Exception ex) { }  
82
        }
83
        
84
    }
85
    
86
    public static byte[] createChecksum(File the_file) throws Exception {
87
        
88
        InputStream fis =  new FileInputStream(the_file);
89

    
90
        byte[] buffer = new byte[1024];
91
        MessageDigest complete = MessageDigest.getInstance("MD5");
92
        int numRead;
93

    
94
        do {
95
            numRead = fis.read(buffer);
96
            if (numRead > 0) {
97
                complete.update(buffer, 0, numRead);
98
            }
99
        } while (numRead != -1);
100

    
101
        fis.close();
102
        return complete.digest();
103
    }
104

    
105
    /**
106
     * In case this is too slow for you:
107
     * There are faster ways to convert
108
     * a byte array to a HEX string
109
     *  
110
     * @param the_file the input file
111
     * @return the given file's MD5 checksum as a hex string
112
     * @throws Exception
113
     */
114
    public static String getMD5Checksum(File the_file) throws Exception {
115
        byte[] b = createChecksum(the_file);
116
        String result = "";
117

    
118
        for (int i=0; i < b.length; i++) {
119
            result += Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring( 1 );
120
        }
121
        return result;
122
    }
123

    
124
}