Skip to content

Commit

Permalink
add a checksum verification
Browse files Browse the repository at this point in the history
  • Loading branch information
aozarov committed May 27, 2015
1 parent 7f03b07 commit 8352ecd
Showing 1 changed file with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import com.google.common.base.Strings;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
Expand All @@ -31,6 +32,7 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.math.BigInteger;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
Expand All @@ -42,6 +44,8 @@
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Locale;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
Expand All @@ -59,6 +63,7 @@ public class LocalGcdHelper {
public static final int PORT = 8080;
private static final String GCD = "gcd-v1beta2-rev1-2.1.2b";
private static final String GCD_FILENAME = GCD + ".zip";
private static final String MD5_CHECKSUM = "d84384cdfa8658e1204f4f8be51300e8";
private static final URL GCD_URL;

static {
Expand All @@ -69,7 +74,6 @@ public class LocalGcdHelper {
}
}


private static class ProcessStreamReader extends Thread {

private final Process process;
Expand Down Expand Up @@ -134,7 +138,7 @@ public void start() throws IOException, InterruptedException {

// check if we already have a local copy of the gcd utility and download it if not.
File gcdZipFile = new File(System.getProperty("java.io.tmpdir"), GCD_FILENAME);
if (!gcdZipFile.exists()) {
if (!gcdZipFile.exists() || !MD5_CHECKSUM.equals(md5(gcdZipFile))) {
ReadableByteChannel rbc = Channels.newChannel(GCD_URL.openStream());
FileOutputStream fos = new FileOutputStream(gcdZipFile);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
Expand Down Expand Up @@ -188,6 +192,22 @@ public void start() throws IOException, InterruptedException {
processReader = ProcessStreamReader.start(temp, "Dev App Server is now running");
}

private static String md5(File gcdZipFile) throws IOException {
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
try (InputStream is = new BufferedInputStream(new FileInputStream(gcdZipFile))) {
byte[] bytes = new byte[4 * 1024 * 1024];
int len;
while ((len = is.read(bytes)) >= 0) {
md5.update(bytes, 0, len);
}
}
return String.format("%032x",new BigInteger(1, md5.digest()));
} catch (NoSuchAlgorithmException e) {
throw new IOException(e);
}
}

private static boolean isWindows() {
return System.getProperty("os.name").toLowerCase(Locale.ENGLISH).indexOf("windows") > -1;
}
Expand Down

0 comments on commit 8352ecd

Please sign in to comment.