- removed unused code - changed the getValue method to be slightly less verbose - added unit test
kev
Index: DigestAlgorithm.java
===================================================================
RCS file:
/home/cvspublic/ant/src/main/org/apache/tools/ant/types/selectors/modifiedselector/DigestAlgorithm.java,v
retrieving revision 1.7
diff -u -r1.7 DigestAlgorithm.java
--- DigestAlgorithm.java 10 Jul 2004 17:15:37 -0000 1.7
+++ DigestAlgorithm.java 31 Jan 2005 12:00:20 -0000
@@ -1,5 +1,5 @@
/*
- * Copyright 2003-2004 The Apache Software Foundation
+ * Copyright 2003-2005 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,11 +20,11 @@
import java.io.File;
import java.io.FileInputStream;
-import java.io.FileOutputStream;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
+
import org.apache.tools.ant.BuildException;
@@ -78,7 +78,7 @@
/**
* Size of the read buffer to use.
*/
- private int readBufferSize = 8 * 1024;
+ private static final int READ_BUFFER_SIZE = 8 * 1024;
// ----- Algorithm-Configuration -----
@@ -147,21 +147,17 @@
*/
// implementation adapted from ...taskdefs.Checksum, thanks to Magesh for
hint
public String getValue(File file) {
- initMessageDigest();
- String checksum = null;
- try {
- if (!file.canRead()) {
- return null;
- }
- FileInputStream fis = null;
- FileOutputStream fos = null;
- byte[] buf = new byte[readBufferSize];
+ String checksum = null;
+ if (file.canRead()) {
+ initMessageDigest();
+ FileInputStream fis = null;
+ byte[] buf = new byte[READ_BUFFER_SIZE];
try {
- messageDigest.reset();
+ messageDigest.reset();
fis = new FileInputStream(file);
DigestInputStream dis = new DigestInputStream(fis,
messageDigest);
- while (dis.read(buf, 0, readBufferSize) != -1) {
+ while (dis.read(buf, 0, READ_BUFFER_SIZE) != -1) {
// do nothing
}
dis.close();
@@ -178,13 +174,11 @@
}
checksum = checksumSb.toString();
} catch (Exception e) {
- return null;
+ //do nothing
}
- } catch (Exception e) {
- return null;
- }
- return checksum;
- }
+ }
+ return checksum;
+ }
/**
Index: DigestAlgorithmTest.java
===================================================================
RCS file: DigestAlgorithmTest.java
diff -N DigestAlgorithmTest.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ DigestAlgorithmTest.java 1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,80 @@
+package org.apache.tools.ant.types.selectors.modifiedselector;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.security.DigestInputStream;
+import java.security.MessageDigest;
+
+import junit.framework.TestCase;
+
+import org.apache.tools.ant.util.FileUtils;
+
+public class DigestAlgorithmTest extends TestCase {
+
+ private static final int READ_BUFFER_SIZE = 8 * 1024;
+ private DigestAlgorithm da = new DigestAlgorithm();
+ private MessageDigest messageDigest = null;
+ private FileUtils fu = FileUtils.getFileUtils();
+ private File dir = new File(".");
+
+ private File f = fu.createTempFile("TEST", "TEST", dir);
+
+ /*
+ * @see TestCase#setUp()
+ */
+ protected void setUp() throws Exception {
+ super.setUp();
+ }
+
+ /*
+ * @see TestCase#tearDown()
+ */
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ }
+
+ public String getValue(File file) {
+ String checksum = null;
+ if (file.canRead()) {
+ da.initMessageDigest();
+ FileInputStream fis = null;
+ byte[] buf = new byte[READ_BUFFER_SIZE];
+ try {
+ messageDigest.reset();
+ fis = new FileInputStream(file);
+ DigestInputStream dis = new DigestInputStream(fis,
+ messageDigest);
+ while (dis.read(buf, 0, READ_BUFFER_SIZE) != -1) {
+ // do nothing
+ }
+ dis.close();
+ fis.close();
+ fis = null;
+ byte[] fileDigest = messageDigest.digest();
+ 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);
+ }
+ checksum = checksumSb.toString();
+ } catch (Exception e) {
+ //do nothing
+ }
+ }
+ return checksum;
+ }
+
+ public void testGetValue() {
+ try {
+ DigestAlgorithm da = new DigestAlgorithm();
+ assertEquals(da.getValue(f), this.getValue(f));
+ } catch (Exception e) {
+ e.printStackTrace();
+ fail();
+ }
+ }
+
+}--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
