svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.installer / org.gvsig.installer.lib / org.gvsig.installer.lib.impl / src / main / java / org / gvsig / installer / lib / impl / utils / MD5BinaryFileUtils.java @ 40560
History | View | Annotate | Download (3.89 KB)
1 | 40560 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 40435 | jjdelcerro | *
|
4 | 40560 | jjdelcerro | * Copyright (C) 2007-2013 gvSIG Association.
|
5 | 40435 | jjdelcerro | *
|
6 | * This program is free software; you can redistribute it and/or
|
||
7 | * modify it under the terms of the GNU General Public License
|
||
8 | 40560 | jjdelcerro | * as published by the Free Software Foundation; either version 3
|
9 | 40435 | jjdelcerro | * of the License, or (at your option) any later version.
|
10 | *
|
||
11 | * This program is distributed in the hope that it will be useful,
|
||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
14 | * GNU General Public License for more details.
|
||
15 | *
|
||
16 | * You should have received a copy of the GNU General Public License
|
||
17 | * along with this program; if not, write to the Free Software
|
||
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||
19 | * MA 02110-1301, USA.
|
||
20 | *
|
||
21 | * For any additional information, do not hesitate to contact us
|
||
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | */
|
||
24 | package org.gvsig.installer.lib.impl.utils; |
||
25 | |||
26 | import java.io.BufferedReader; |
||
27 | import java.io.File; |
||
28 | import java.io.FileInputStream; |
||
29 | import java.io.InputStream; |
||
30 | import java.io.InputStreamReader; |
||
31 | import java.net.URL; |
||
32 | import java.net.URLConnection; |
||
33 | import java.security.MessageDigest; |
||
34 | |||
35 | import org.slf4j.Logger; |
||
36 | import org.slf4j.LoggerFactory; |
||
37 | |||
38 | |||
39 | /**
|
||
40 | * @author jldominguez
|
||
41 | *
|
||
42 | */
|
||
43 | public class MD5BinaryFileUtils { |
||
44 | |||
45 | private static final Logger LOG = LoggerFactory.getLogger(MD5BinaryFileUtils.class); |
||
46 | |||
47 | /**
|
||
48 | *
|
||
49 | * Gets the MD5 string of a md5 file which is associated with
|
||
50 | * the file provided.
|
||
51 | *
|
||
52 | * @param remote_file the file that has a ".md5" file next to it
|
||
53 | * @return
|
||
54 | */
|
||
55 | public static String getMD5InRemoteFile(URL remote_file) { |
||
56 | |||
57 | BufferedReader in = null; |
||
58 | InputStreamReader istr = null; |
||
59 | try {
|
||
60 | String _url = remote_file.toString() + ".md5"; |
||
61 | URL md5_url = new URL(_url); |
||
62 | |||
63 | URLConnection connection = md5_url.openConnection();
|
||
64 | connection.setUseCaches(false);
|
||
65 | connection.setConnectTimeout(30000);
|
||
66 | connection.setReadTimeout(20000);
|
||
67 | |||
68 | istr = new InputStreamReader(connection.getInputStream()); |
||
69 | in = new BufferedReader(istr); |
||
70 | String inputLine = in.readLine();
|
||
71 | |||
72 | int space = inputLine.indexOf(" "); |
||
73 | inputLine = inputLine.substring(0, space);
|
||
74 | return inputLine;
|
||
75 | } catch (Exception ex) { |
||
76 | LOG.info("Error while reading MD5 file: " + ex.getMessage());
|
||
77 | return null; |
||
78 | } finally {
|
||
79 | try {
|
||
80 | in.close(); |
||
81 | istr.close(); |
||
82 | } catch (Exception ex) { } |
||
83 | } |
||
84 | |||
85 | } |
||
86 | |||
87 | public static byte[] createChecksum(File the_file) throws Exception { |
||
88 | |||
89 | InputStream fis = new FileInputStream(the_file); |
||
90 | |||
91 | byte[] buffer = new byte[1024]; |
||
92 | MessageDigest complete = MessageDigest.getInstance("MD5"); |
||
93 | int numRead;
|
||
94 | |||
95 | do {
|
||
96 | numRead = fis.read(buffer); |
||
97 | if (numRead > 0) { |
||
98 | complete.update(buffer, 0, numRead);
|
||
99 | } |
||
100 | } while (numRead != -1); |
||
101 | |||
102 | fis.close(); |
||
103 | return complete.digest();
|
||
104 | } |
||
105 | |||
106 | /**
|
||
107 | * In case this is too slow for you:
|
||
108 | * There are faster ways to convert
|
||
109 | * a byte array to a HEX string
|
||
110 | *
|
||
111 | * @param the_file the input file
|
||
112 | * @return the given file's MD5 checksum as a hex string
|
||
113 | * @throws Exception
|
||
114 | */
|
||
115 | public static String getMD5Checksum(File the_file) throws Exception { |
||
116 | byte[] b = createChecksum(the_file); |
||
117 | String result = ""; |
||
118 | |||
119 | for (int i=0; i < b.length; i++) { |
||
120 | result += Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring( 1 ); |
||
121 | } |
||
122 | return result;
|
||
123 | } |
||
124 | |||
125 | } |