Author: pete
Date: Sat Nov  6 14:07:17 2010
New Revision: 1032060

URL: http://svn.apache.org/viewvc?rev=1032060&view=rev
Log:
make digest functionality in FileUpload more versatile

Modified:
    
wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/upload/FileUpload.java

Modified: 
wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/upload/FileUpload.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/upload/FileUpload.java?rev=1032060&r1=1032059&r2=1032060&view=diff
==============================================================================
--- 
wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/upload/FileUpload.java
 (original)
+++ 
wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/upload/FileUpload.java
 Sat Nov  6 14:07:17 2010
@@ -22,13 +22,14 @@ import java.io.InputStream;
 import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
 import java.util.ArrayList;
-import java.util.Iterator;
 import java.util.List;
 
 import org.apache.wicket.IClusterable;
 import org.apache.wicket.Session;
 import org.apache.wicket.WicketRuntimeException;
 import org.apache.wicket.util.file.Files;
+import org.apache.wicket.util.io.IOUtils;
+import org.apache.wicket.util.lang.Args;
 import org.apache.wicket.util.upload.FileItem;
 
 
@@ -69,18 +70,9 @@ public class FileUpload implements IClus
        {
                if (inputStreamsToClose != null)
                {
-                       for (Iterator<InputStream> inputStreamsIterator = 
inputStreamsToClose.iterator(); inputStreamsIterator.hasNext();)
+                       for (InputStream inputStream : inputStreamsToClose)
                        {
-                               InputStream inputStream = 
inputStreamsIterator.next();
-
-                               try
-                               {
-                                       inputStream.close();
-                               }
-                               catch (IOException e)
-                               {
-                                       // We don't care aobut the exceptions 
thrown here.
-                               }
+                               IOUtils.closeQuietly(inputStream);
                        }
 
                        // Reset the list
@@ -107,58 +99,62 @@ public class FileUpload implements IClus
        /**
         * Get the MD5 checksum.
         * 
-        * @return The MD5 checksum of the file
+        * @param algorithm the digest algorithm, e.g. MD5, SHA-1, SHA-256, 
SHA-512
+        *
+        * @return The cryptographic digest of the file
         */
-       public byte[] getMD5()
+       public byte[] getDigest(String algorithm)
        {
-               MessageDigest digest;
                try
                {
-                       digest = java.security.MessageDigest.getInstance("MD5");
-               }
-               catch (NoSuchAlgorithmException ex)
-               {
-                       throw new WicketRuntimeException(
-                               "Your java runtime does not support MD5 
digests. Please see java.security.MessageDigest.getInstance(\"MD5\"",
-                               ex);
-               }
+                       Args.notEmpty(algorithm, "algorithm");
+                       MessageDigest digest = 
java.security.MessageDigest.getInstance(algorithm);
 
-               if (item.isInMemory())
-               {
-                       digest.update(getBytes());
-                       return digest.digest();
-               }
-
-               InputStream in = null;
-               try
-               {
-                       in = item.getInputStream();
-                       byte[] buf = new byte[Math.min((int)item.getSize(), 
4096 * 10)];
-                       int len;
-                       while (-1 != (len = in.read(buf)))
+                       if (item.isInMemory())
                        {
-                               digest.update(buf, 0, len);
+                               digest.update(getBytes());
+                               return digest.digest();
                        }
-                       return digest.digest();
-               }
-               catch (IOException ex)
-               {
-                       throw new WicketRuntimeException("Error while reading 
input data for MD5 checksum", ex);
-               }
-               finally
-               {
-                       if (in != null)
+
+                       InputStream in = null;
+
+                       try
                        {
-                               try
+                               in = item.getInputStream();
+                               byte[] buf = new byte[Math.min((int) 
item.getSize(), 4096 * 10)];
+                               int len;
+                               while (-1 != (len = in.read(buf)))
                                {
-                                       in.close();
-                               }
-                               catch (IOException ex)
-                               {
-                                       // ignore
+                                       digest.update(buf, 0, len);
                                }
+                               return digest.digest();
+                       }
+                       catch (IOException ex)
+                       {
+                               throw new WicketRuntimeException("Error while 
reading input data for " + algorithm + " checksum", ex);
+                       }
+                       finally
+                       {
+                               IOUtils.closeQuietly(in);
                        }
                }
+               catch (NoSuchAlgorithmException ex)
+               {
+                       String error = String.format("Your java runtime does 
not support digest algorithm [%s]. " +
+                                       "Please see 
java.security.MessageDigest.getInstance(\"%s\")", algorithm, algorithm);
+
+                       throw new WicketRuntimeException(error, ex);
+               }
+       }
+
+       /**
+        * Get the MD5 checksum.
+        *
+        * @return The MD5 checksum of the file
+        */
+       public byte[] getMD5()
+       {
+               return getDigest("MD5");
        }
 
        /**
@@ -260,8 +256,7 @@ public class FileUpload implements IClus
         */
        public final File writeToTempFile() throws IOException
        {
-               File temp = File.createTempFile(Session.get().getId(),
-                       Files.cleanupFilename(item.getFieldName()));
+               File temp = File.createTempFile(Session.get().getId(), 
Files.cleanupFilename(item.getFieldName()));
                writeTo(temp);
                return temp;
        }


Reply via email to