1 package fr.in2p3.jsaga.helpers;
2
3 import fr.in2p3.jsaga.Base;
4
5 import java.io.*;
6 import java.security.MessageDigest;
7 import java.security.NoSuchAlgorithmException;
8
9
10
11
12
13
14
15
16
17
18
19
20
21 public class MD5Digester {
22 public static boolean isSame(File refFile, byte[] data) throws IOException, NoSuchAlgorithmException {
23
24 boolean same;
25 byte[] md5 = MessageDigest.getInstance("MD5").digest(data);
26 if (refFile.exists()) {
27 byte[] ref = new byte[(int)refFile.length()];
28 InputStream in = new FileInputStream(refFile);
29 if (in.read(ref) > -1) {
30 same = equals(ref, md5);
31 } else {
32 same = false;
33 }
34 in.close();
35 } else {
36 same = false;
37 }
38
39
40 OutputStream out = new FileOutputStream(refFile);
41 out.write(md5);
42 out.close();
43
44
45 if (Base.DEBUG) {
46 File debugBaseDir = new File(refFile.getParentFile(), "debug");
47 if (!debugBaseDir.exists()) {
48 debugBaseDir.mkdir();
49 }
50 File debugFile = new File(debugBaseDir, refFile.getName()+".xml");
51 OutputStream f = new FileOutputStream(debugFile);
52 f.write(data);
53 f.close();
54 }
55 return same;
56 }
57
58 private static boolean equals(byte[] b1, byte[] b2) {
59 if (b1.length != b2.length) {
60 return false;
61 }
62 for (int i=0; i<b1.length && i<b2.length; i++) {
63 if (b1[i]!=b2[i]) {
64 return false;
65 }
66 }
67 return true;
68 }
69 }