1 package org.simantics.fastlz;
\r
4 import java.io.FileInputStream;
\r
5 import java.io.IOException;
\r
6 import java.io.InputStream;
\r
8 import java.security.MessageDigest;
\r
9 import java.security.NoSuchAlgorithmException;
\r
12 * @author Tuukka Lehtonen
\r
14 public final class ChecksumUtil {
\r
16 public static byte[] computeSum(byte[] in) {
\r
18 throw new IllegalArgumentException("Input cannot be null!");
\r
20 MessageDigest md = MessageDigest.getInstance("MD5");
\r
21 md.update(in, 0, in.length);
\r
23 } catch (NoSuchAlgorithmException e) {
\r
24 throw new Error("MD5 digest must be supported by JVM");
\r
28 public static byte[] computeSum(InputStream in) throws IOException {
\r
30 throw new IllegalArgumentException("Input cannot be null!");
\r
34 md = MessageDigest.getInstance("MD5");
\r
35 } catch (NoSuchAlgorithmException e) {
\r
36 throw new Error("MD5 digest must be supported by JVM");
\r
38 byte[] data = new byte[64 * 1024];
\r
41 int read = in.read(data);
\r
45 md.update(data, 0, read);
\r
49 public static byte[] computeSum(File f) throws IOException {
\r
50 InputStream in = null;
\r
52 in = new FileInputStream(f);
\r
53 return computeSum(in);
\r
60 public static byte[] computeSum(URL url) throws IOException {
\r
61 InputStream in = null;
\r
63 in = url.openStream();
\r
64 return computeSum(in);
\r