|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
MD5ChecksumProperties.java | 0% | 0% | 0% | 0% |
|
1 | /*BEGIN_COPYRIGHT_BLOCK | |
2 | * | |
3 | * Copyright (c) 2001-2010, JavaPLT group at Rice University (drjava@rice.edu) | |
4 | * All rights reserved. | |
5 | * | |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions are met: | |
8 | * * Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * * Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
13 | * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the | |
14 | * names of its contributors may be used to endorse or promote products | |
15 | * derived from this software without specific prior written permission. | |
16 | * | |
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
19 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
20 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | |
21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
24 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
25 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
26 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
27 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
28 | * | |
29 | * This software is Open Source Initiative approved Open Source Software. | |
30 | * Open Source Initative Approved is a trademark of the Open Source Initiative. | |
31 | * | |
32 | * This file is part of DrJava. Download the current version of this project | |
33 | * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/ | |
34 | * | |
35 | * END_COPYRIGHT_BLOCK*/ | |
36 | ||
37 | package edu.rice.cs.util; | |
38 | ||
39 | import java.io.*; | |
40 | import java.util.*; | |
41 | import java.util.jar.*; | |
42 | import java.security.*; | |
43 | ||
44 | /** Create a property file with MD5 checksums | |
45 | * @version $Id: MD5ChecksumProperties.java 5236 2010-04-27 01:43:36Z mgricken $ | |
46 | */ | |
47 | public class MD5ChecksumProperties extends Properties { | |
48 | public static final int BUFFER_SIZE = 10*1024; | |
49 | ||
50 | 0 | public MD5ChecksumProperties() { super(); } |
51 | 0 | public MD5ChecksumProperties(Properties p) { super(p); } |
52 | ||
53 | /** | |
54 | * Return the MD5 checksum for the data in the stream, while copying the data | |
55 | * into the output stream. The output stream is not closed. | |
56 | * @param is input stream | |
57 | * @param os output stream (or null if no copying desired) | |
58 | * @return MD5 checksum | |
59 | */ | |
60 | 0 | public static byte[] getMD5(InputStream is, OutputStream os) throws IOException { |
61 | 0 | try { |
62 | 0 | MessageDigest digest = MessageDigest.getInstance("MD5"); |
63 | 0 | DigestInputStream dis = new DigestInputStream(new BufferedInputStream(is), digest); |
64 | 0 | BufferedOutputStream bos = null; |
65 | 0 | if (os!=null) { |
66 | 0 | bos = new BufferedOutputStream(os); |
67 | } | |
68 | 0 | byte[] buf = new byte[BUFFER_SIZE]; |
69 | 0 | int bytesRead = 0; |
70 | 0 | while((bytesRead=dis.read(buf, 0, BUFFER_SIZE))!=-1) { |
71 | 0 | if (os!=null) { bos.write(buf, 0, bytesRead); } |
72 | } | |
73 | 0 | if (os!=null) { bos.flush(); } |
74 | 0 | dis.close(); |
75 | 0 | is.close(); |
76 | 0 | return digest.digest(); |
77 | } | |
78 | catch(NoSuchAlgorithmException nsae) { | |
79 | 0 | throw new UnexpectedException(nsae,"MD5 algorithm not available"); |
80 | } | |
81 | } | |
82 | ||
83 | /** | |
84 | * Return the MD5 checksum for the data in the stream | |
85 | * @param is input stream | |
86 | * @return MD5 checksum | |
87 | */ | |
88 | 0 | public static byte[] getMD5(InputStream is) throws IOException { |
89 | 0 | return getMD5(is, null); |
90 | } | |
91 | ||
92 | /** | |
93 | * Return the MD5 checksum as string for the data in the stream, while | |
94 | * copying the data into the output stream. The output stream is not closed. | |
95 | * @param is input stream | |
96 | * @param os output stream (or null if no copying desired) | |
97 | * @return MD5 checksum string | |
98 | */ | |
99 | 0 | public static String getMD5String(InputStream is, OutputStream os) throws IOException { |
100 | 0 | byte[] messageDigest = getMD5(is,os); |
101 | 0 | StringBuilder hexString = new StringBuilder(); |
102 | 0 | for (int i=0;i<messageDigest.length;i++) { |
103 | 0 | String oneByte = "0"+Integer.toHexString(0xFF & messageDigest[i]); |
104 | 0 | hexString.append(oneByte.substring(oneByte.length()-2,oneByte.length())); |
105 | } | |
106 | 0 | return hexString.toString(); |
107 | } | |
108 | ||
109 | /** | |
110 | * Return the MD5 checksum as string for the data in the stream. | |
111 | * @param is input stream | |
112 | * @return MD5 checksum string | |
113 | */ | |
114 | 0 | public static String getMD5String(InputStream is) throws IOException { |
115 | 0 | return getMD5String(is, null); |
116 | } | |
117 | ||
118 | 0 | public static byte[] getMD5(File f) throws IOException { |
119 | 0 | return getMD5(new FileInputStream(f)); |
120 | } | |
121 | ||
122 | 0 | public static String getMD5String(File f) throws IOException { |
123 | 0 | return getMD5String(new FileInputStream(f)); |
124 | } | |
125 | ||
126 | 0 | public static byte[] getMD5(byte[] b) throws IOException { |
127 | 0 | return getMD5(new ByteArrayInputStream(b)); |
128 | } | |
129 | ||
130 | 0 | public static String getMD5String(byte[] b) throws IOException { |
131 | 0 | return getMD5String(new ByteArrayInputStream(b)); |
132 | } | |
133 | ||
134 | /** Add the MD5 checksum for the data in the input stream to the | |
135 | * properties, using the specified key. | |
136 | * @param key key to store the checksum under | |
137 | * @param is input stream with the data | |
138 | * @param os output stream to copy to (or null if not wanted) | |
139 | * @return false if the new MD5 checksum didn't match an existing checksum */ | |
140 | 0 | public boolean addMD5(String key, InputStream is, OutputStream os) throws IOException { |
141 | 0 | String md5 = getMD5String(is, os); |
142 | 0 | Object prev = setProperty(key,md5); |
143 | 0 | return ((prev==null) || (prev.equals(md5))); |
144 | } | |
145 | ||
146 | /** Add the MD5 checksum for the data in the input stream to the | |
147 | * properties, using the specified key. | |
148 | * @param key key to store the checksum under | |
149 | * @param is input stream with the data | |
150 | * @return false if the new MD5 checksum didn't match an existing checksum */ | |
151 | 0 | public boolean addMD5(String key, InputStream is) throws IOException { |
152 | 0 | return addMD5(key, is, null); |
153 | } | |
154 | ||
155 | /** Add the MD5 checksum for the data in the file to the | |
156 | * properties, using the specified key. | |
157 | * @param key key to store the checksum under | |
158 | * @param f file with the data | |
159 | * @param os output stream to copy to (or null if not wanted) | |
160 | * @return false if the new MD5 checksum didn't match an existing checksum */ | |
161 | 0 | public boolean addMD5(String key, File f, OutputStream os) throws IOException { |
162 | 0 | return addMD5(key, new FileInputStream(f), os); |
163 | } | |
164 | ||
165 | /** Add the MD5 checksum for the data in the file to the | |
166 | * properties, using the specified key. | |
167 | * @param key key to store the checksum under | |
168 | * @param f file with the data | |
169 | * @return false if the new MD5 checksum didn't match an existing checksum */ | |
170 | 0 | public boolean addMD5(String key, File f) throws IOException { |
171 | 0 | return addMD5(key, f, null); |
172 | } | |
173 | ||
174 | /** Add the MD5 checksum for the data in the byte array to the | |
175 | * properties, using the specified key. | |
176 | * @param key key to store the checksum under | |
177 | * @param b byte array with the data | |
178 | * @param os output stream to copy to (or null if not wanted) | |
179 | * @return false if the new MD5 checksum didn't match an existing checksum */ | |
180 | 0 | public boolean addMD5(String key, byte[] b, OutputStream os) throws IOException { |
181 | 0 | return addMD5(key, new ByteArrayInputStream(b), os); |
182 | } | |
183 | ||
184 | /** Add the MD5 checksum for the data in the byte array to the | |
185 | * properties, using the specified key. | |
186 | * @param key key to store the checksum under | |
187 | * @param b byte array with the data | |
188 | * @return false if the new MD5 checksum didn't match an existing checksum */ | |
189 | 0 | public boolean addMD5(String key, byte[] b) throws IOException { |
190 | 0 | return addMD5(key, b, null); |
191 | } | |
192 | ||
193 | /** Add the MD5 checksum for the data in the input stream to the | |
194 | * properties, using the name of the file as key. | |
195 | * @param f file with the data | |
196 | * @param os output stream to copy to (or null if not wanted) | |
197 | * @return false if the new MD5 checksum didn't match an existing checksum */ | |
198 | 0 | public boolean addMD5(File f, OutputStream os) throws IOException { |
199 | 0 | return addMD5(f.getPath().replace('\\','/'), f, os); |
200 | } | |
201 | ||
202 | /** Add the MD5 checksum for the data in the input stream to the | |
203 | * properties, using the name of the file as key. | |
204 | * @param f file with the data | |
205 | * @return false if the new MD5 checksum didn't match an existing checksum */ | |
206 | 0 | public boolean addMD5(File f) throws IOException { |
207 | 0 | return addMD5(f, null); |
208 | } | |
209 | ||
210 | /** Main method. Usage: | |
211 | * no arguments --> input file list from standard in, output properties to standard out | |
212 | * <file1> --> input file list from standard in, append output properties to file1 | |
213 | * <file1> <file2> --> input file list from file1, append output properties to file 2 | |
214 | */ | |
215 | 0 | public static void main(String[] args) throws IOException { |
216 | 0 | InputStream is = System.in; |
217 | 0 | OutputStream os = System.out; |
218 | 0 | Properties prevp = new Properties(); |
219 | ||
220 | 0 | if (args.length==2) { |
221 | 0 | File outFile = new File(args[1]); |
222 | 0 | if (outFile.exists()) { |
223 | 0 | FileInputStream pis = new FileInputStream(outFile); |
224 | 0 | prevp.load(pis); |
225 | 0 | pis.close(); |
226 | } | |
227 | 0 | is = new FileInputStream(new File(args[0])); |
228 | 0 | os = new FileOutputStream(outFile); |
229 | } | |
230 | 0 | else if (args.length==1) { |
231 | 0 | File outFile = new File(args[0]); |
232 | 0 | if (outFile.exists()) { |
233 | 0 | FileInputStream pis = new FileInputStream(outFile); |
234 | 0 | prevp.load(pis); |
235 | 0 | pis.close(); |
236 | } | |
237 | 0 | os = new FileOutputStream(outFile); |
238 | } | |
239 | ||
240 | 0 | BufferedReader br = new BufferedReader(new InputStreamReader(is)); |
241 | 0 | MD5ChecksumProperties p = new MD5ChecksumProperties(); |
242 | 0 | p.putAll(prevp); |
243 | 0 | String line; |
244 | 0 | while((line = br.readLine())!=null) { |
245 | 0 | if (line.equals("")) break; |
246 | 0 | p.addMD5(new File(line)); |
247 | } | |
248 | 0 | p.store(os,"MD5 Checksums"); |
249 | } | |
250 | } |
|