Mercurial > hg > LGDataverses
comparison src/main/java/edu/harvard/iq/dataverse/util/MD5Checksum.java @ 10:a50cf11e5178
Rewrite LGDataverse completely upgrading to dataverse4.0
| author | Zoe Hong <zhong@mpiwg-berlin.mpg.de> |
|---|---|
| date | Tue, 08 Sep 2015 17:00:21 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 9:5926d6419569 | 10:a50cf11e5178 |
|---|---|
| 1 /* | |
| 2 * To change this license header, choose License Headers in Project Properties. | |
| 3 * To change this template file, choose Tools | Templates | |
| 4 * and open the template in the editor. | |
| 5 */ | |
| 6 | |
| 7 package edu.harvard.iq.dataverse.util; | |
| 8 | |
| 9 import java.io.InputStream; | |
| 10 import java.io.FileInputStream; | |
| 11 import java.io.FileNotFoundException; | |
| 12 import java.io.IOException; | |
| 13 import java.security.MessageDigest; | |
| 14 import java.security.NoSuchAlgorithmException; | |
| 15 | |
| 16 /** | |
| 17 * (from v.3.6) | |
| 18 * @author xyang | |
| 19 */ | |
| 20 public class MD5Checksum implements java.io.Serializable { | |
| 21 | |
| 22 public MD5Checksum() { | |
| 23 } | |
| 24 | |
| 25 public synchronized String CalculateMD5 (String datafile) { | |
| 26 | |
| 27 FileInputStream fis = null; | |
| 28 try { | |
| 29 fis = new FileInputStream(datafile); | |
| 30 } catch (FileNotFoundException ex) { | |
| 31 throw new RuntimeException(ex); | |
| 32 } | |
| 33 | |
| 34 return CalculateMD5(fis); | |
| 35 /* | |
| 36 byte[] dataBytes = new byte[1024]; | |
| 37 | |
| 38 int nread; | |
| 39 try { | |
| 40 while ((nread = fis.read(dataBytes)) != -1) { | |
| 41 md.update(dataBytes, 0, nread); | |
| 42 } | |
| 43 } catch (IOException ex) { | |
| 44 throw new RuntimeException(ex); | |
| 45 } | |
| 46 | |
| 47 byte[] mdbytes = md.digest(); | |
| 48 StringBuilder sb = new StringBuilder(""); | |
| 49 for (int i = 0; i < mdbytes.length; i++) { | |
| 50 sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1)); | |
| 51 } | |
| 52 return sb.toString(); | |
| 53 */ | |
| 54 } | |
| 55 | |
| 56 public synchronized String CalculateMD5 (InputStream in) { | |
| 57 MessageDigest md = null; | |
| 58 try { | |
| 59 md = MessageDigest.getInstance("MD5"); | |
| 60 } catch (NoSuchAlgorithmException e) { | |
| 61 throw new RuntimeException(e); | |
| 62 } | |
| 63 | |
| 64 byte[] dataBytes = new byte[1024]; | |
| 65 | |
| 66 int nread; | |
| 67 try { | |
| 68 while ((nread = in.read(dataBytes)) != -1) { | |
| 69 md.update(dataBytes, 0, nread); | |
| 70 } | |
| 71 } catch (IOException ex) { | |
| 72 throw new RuntimeException(ex); | |
| 73 } finally { | |
| 74 try {in.close();} catch (Exception e) {} | |
| 75 } | |
| 76 | |
| 77 byte[] mdbytes = md.digest(); | |
| 78 StringBuilder sb = new StringBuilder(""); | |
| 79 for (int i = 0; i < mdbytes.length; i++) { | |
| 80 sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1)); | |
| 81 } | |
| 82 return sb.toString(); | |
| 83 } | |
| 84 } |
