Hi,

I've made a small patch for the get task, allowing you to verify the size and/or MD5 sum of a file to be downloaded. In the case of a match, it skips the actual download. (If this seems like a good idea, the potential exists that we can do the check after the download as well, just to verify the downloaded file.)

The task now accepts two additional (optional) parameters, "size" and "md5". There is not yet a documentation update, I just want to feel the waters on this one before doing that as well. (If this patch is acceptable, I'll give it a stab.)

Greetings,
Jaco
--- src/main/org/apache/tools/ant/taskdefs/Get.java.orig        2003-06-25 
13:25:38.000000000 +0200
+++ src/main/org/apache/tools/ant/taskdefs/Get.java     2003-06-25 
13:34:50.000000000 +0200
@@ -55,12 +55,16 @@
 package org.apache.tools.ant.taskdefs;
 
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.HttpURLConnection;
 import java.net.URL;
 import java.net.URLConnection;
+import java.security.DigestInputStream;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
 import java.util.Date;
 import org.apache.tools.ant.BuildException;
 import org.apache.tools.ant.Project;
@@ -89,6 +93,11 @@
     private boolean ignoreErrors = false;
     private String uname = null;
     private String pword = null;
+    private long size = 0L;
+    private String md5 = null;
+
+    private String algorithm = "MD5";
+    private int readBufferSize = 8 * 1024;
 
 
     /**
@@ -133,6 +142,59 @@
                 hasTimestamp = true;
             }
 
+            // check the size and/or md5 if specified
+            if (dest.exists()) {
+                boolean matched = false;
+                if (size != 0L) {
+                   if (dest.length() == size) {
+                        log("Size test for " + dest + " passed");
+                        matched = true;
+                   }
+                   else {
+                       log("Size test for " + dest + " failed");
+                       matched = false;
+                   }
+                }
+                if (matched && (md5 != null)) {
+                    MessageDigest messageDigest = null;
+                    try {
+                        messageDigest = MessageDigest.getInstance(algorithm);
+                    } catch (NoSuchAlgorithmException noalgo) {
+                        throw new BuildException(noalgo, getLocation());
+                    }
+                    if (messageDigest == null) {
+                        throw new BuildException("Unable to create Message 
Digest",
+                                                 getLocation());
+                    }
+                    messageDigest.reset();
+                    log("Calculating " + algorithm + " checksum for " + dest);
+                    FileInputStream fis = new FileInputStream(dest);
+                    DigestInputStream dis = new DigestInputStream(fis,
+                                                                  
messageDigest);
+                    byte[] buf = new byte[readBufferSize];
+                    while (dis.read(buf, 0, readBufferSize) != -1) {
+                        ;
+                    }
+                    dis.close();
+                    fis.close();
+                    fis = null;
+                    byte[] fileDigest = messageDigest.digest ();
+                    String checksum = createDigestString(fileDigest);
+                    if (checksum.equals(md5)) {
+                        log(algorithm + " checksum test for " + dest + " 
passed");
+                        matched = true;
+                    }
+                   else {
+                       log(algorithm + " checksum test for " + dest + " 
failed");
+                        matched = false;
+                   }
+                }
+                if (matched) {
+                    log("Pre-download tests passed, not downloading file");
+                    return;
+                }
+            }
+
             //set up the URL connection
             URLConnection connection = source.openConnection();
             //modify the headers
@@ -148,7 +210,7 @@
                 try {
                     Object encoder =
                             
Class.forName("sun.misc.BASE64Encoder").newInstance();
-                    encoding = (String) 
+                    encoding = (String)
                             encoder.getClass().getMethod("encode", new Class[] 
{byte[].class})
                             .invoke(encoder, new Object[] {up.getBytes()});
 
@@ -352,6 +414,44 @@
         this.pword = p;
     }
 
+    /**
+     * size for download verification
+     *
+     * @since Ant 1.6
+     * @param s size for verification
+     */
+    public void setSize(int s) {
+        this.size = s;
+    }
+
+    /**
+     * md5 for download verification
+     *
+     * @since Ant 1.6
+     * @param m md5 for verification
+     */
+    public void setMd5(String m) {
+        this.md5 = m;
+    }
+
+    /**
+     * internal method to generate a string from a digest
+     *
+     * @since Ant 1.6
+     * @return digest string
+     */
+    private String createDigestString(byte[] fileDigest) {
+        StringBuffer checksumSb = new StringBuffer();
+        for (int i = 0; i < fileDigest.length; i++) {
+            String hexStr = Integer.toHexString(0x00ff & fileDigest[i]);
+            if (hexStr.length() < 2) {
+                checksumSb.append("0");
+            }
+            checksumSb.append(hexStr);
+        }
+        return checksumSb.toString();
+    }
+
     /*********************************************************************
     * BASE 64 encoding of a String or an array of bytes.
     *

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to