This repository has been archived by the owner on Jan 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FileDifferenceProvider: calculate md5 + enable test case which was fa…
…iling previously due to missing md5 check
- Loading branch information
Showing
3 changed files
with
66 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/main/groovy/org/mockito/release/internal/util/Md5.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package org.mockito.release.internal.util; | ||
|
||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.math.BigInteger; | ||
import java.security.MessageDigest; | ||
import java.security.NoSuchAlgorithmException; | ||
|
||
/** | ||
* Md5 utilities | ||
*/ | ||
public class Md5 { | ||
|
||
/** | ||
* Calculates the md5 of a given file. | ||
* @param file the file to calculate the md5 for | ||
* @return the resulting md5 | ||
*/ | ||
public static String calculate(File file) { | ||
InputStream is = null; | ||
try { | ||
MessageDigest md = MessageDigest.getInstance("md5"); | ||
is = new FileInputStream(file); | ||
|
||
int bytesRead = 0; | ||
byte[] data = new byte[1024]; | ||
while ((bytesRead = is.read(data)) != -1) { | ||
md.update(data, 0, bytesRead); | ||
} | ||
return new BigInteger(1, md.digest()).toString(16); | ||
} catch (java.io.IOException e) { | ||
throw new RuntimeException("error while generating md5 for file " + file, e); | ||
} catch (NoSuchAlgorithmException e) { | ||
throw new RuntimeException("error while generating md5 for file " + file, e); | ||
} finally { | ||
if (is != null) { | ||
try { | ||
is.close(); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters