Author: scolebourne Date: Tue Aug 29 16:05:22 2006 New Revision: 438281 URL: http://svn.apache.org/viewvc?rev=438281&view=rev Log: IO-54 - Add FileUtils.checksum / FileUtils.checksumCRC32 to simplify calculating checksums from Emmanuel Bourg
Modified: jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt jakarta/commons/proper/io/trunk/project.xml jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java Modified: jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt URL: http://svn.apache.org/viewvc/jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt?rev=438281&r1=438280&r2=438281&view=diff ============================================================================== --- jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt (original) +++ jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt Tue Aug 29 16:05:22 2006 @@ -110,6 +110,8 @@ - add methods to check if a file is older (i.e. isFileOlder()) - counterparts to the existing isFileNewer() methods. +- FileUtils.checksum, FileUtils.checksumCRC32 + - add methods to create a checksum of a file Feedback Modified: jakarta/commons/proper/io/trunk/project.xml URL: http://svn.apache.org/viewvc/jakarta/commons/proper/io/trunk/project.xml?rev=438281&r1=438280&r2=438281&view=diff ============================================================================== --- jakarta/commons/proper/io/trunk/project.xml (original) +++ jakarta/commons/proper/io/trunk/project.xml Tue Aug 29 16:05:22 2006 @@ -187,6 +187,9 @@ <name>Nathan Beyer</name> </contributor> <contributor> + <name>Emmanuel Bourg</name> + </contributor> + <contributor> <name>Chris Eldredge</name> </contributor> <contributor> Modified: jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java?rev=438281&r1=438280&r2=438281&view=diff ============================================================================== --- jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java (original) +++ jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java Tue Aug 29 16:05:22 2006 @@ -30,6 +30,9 @@ import java.util.Date; import java.util.Iterator; import java.util.List; +import java.util.zip.CRC32; +import java.util.zip.CheckedInputStream; +import java.util.zip.Checksum; import org.apache.commons.io.filefilter.DirectoryFileFilter; import org.apache.commons.io.filefilter.FalseFileFilter; @@ -37,6 +40,7 @@ import org.apache.commons.io.filefilter.IOFileFilter; import org.apache.commons.io.filefilter.SuffixFileFilter; import org.apache.commons.io.filefilter.TrueFileFilter; +import org.apache.commons.io.output.NullOutputStream; /** * General file manipulation utilities. @@ -52,6 +56,7 @@ * <li>listing files and directories by filter and extension * <li>comparing file content * <li>file last changed date + * <li>calculating a checksum * </ul> * <p> * Origin of code: Excalibur, Alexandria, Commons-Utils @@ -1358,4 +1363,53 @@ } return file.lastModified() < timeMillis; } + + //----------------------------------------------------------------------- + /** + * Computes the checksum of a file using the CRC32 checksum routine. + * The value of the checksum is returned. + * + * @param file the file to checksum, not null + * @param checksum the checksum object to be used, not null + * @return the checksum value + * @throws NullPointerException if the file or checksum is null + * @throws IllegalArgumentException if the file is a directory + * @since Commons IO 1.3 + */ + public static long checksumCRC32(File file) throws IOException { + CRC32 crc = new CRC32(); + checksum(file, crc); + return crc.getValue(); + } + + /** + * Computes the checksum of a file using the specified checksum object. + * Multiple files may be checked using one <code>Checksum</code> instance + * if desired simply by reusing the same checksum object. + * For example: + * <pre> + * long csum = FileUtils.checksum(file, new CRC32()).getValue(); + * </pre> + * + * @param file the file to checksum, not null + * @param checksum the checksum object to be used, not null + * @return the checksum specified, updated with the content of the file + * @throws NullPointerException if the file or checksum is null + * @throws IllegalArgumentException if the file is a directory + * @since Commons IO 1.3 + */ + public static Checksum checksum(File file, Checksum checksum) throws IOException { + if (file.isDirectory()) { + throw new IllegalArgumentException("Checksums can't be computed on directories"); + } + InputStream in = null; + try { + in = new CheckedInputStream(new FileInputStream(file), checksum); + IOUtils.copy(in, new NullOutputStream()); + } finally { + IOUtils.closeQuietly(in); + } + return checksum; + } + } Modified: jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java?rev=438281&r1=438280&r2=438281&view=diff ============================================================================== --- jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java (original) +++ jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java Tue Aug 29 16:05:22 2006 @@ -30,6 +30,8 @@ import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.zip.CRC32; +import java.util.zip.Checksum; import junit.framework.Test; import junit.framework.TestSuite; @@ -944,6 +946,101 @@ IOUtils.LINE_SEPARATOR + "some text" + IOUtils.LINE_SEPARATOR; String actual = FileUtils.readFileToString(file, "US-ASCII"); assertEquals(expected, actual); + } + + //----------------------------------------------------------------------- + public void testChecksumCRC32() throws Exception { + // create a test file + String text = "Imagination is more important than knowledge - Einstein"; + File file = new File(getTestDirectory(), "checksum-test.txt"); + FileUtils.writeStringToFile(file, text, "US-ASCII"); + + // compute the expected checksum + Checksum expectedChecksum = new CRC32(); + expectedChecksum.update(text.getBytes("US-ASCII"), 0, text.length()); + long expectedValue = expectedChecksum.getValue(); + + // compute the checksum of the file + long resultValue = FileUtils.checksumCRC32(file); + + assertEquals(expectedValue, resultValue); + } + + public void testChecksum() throws Exception { + // create a test file + String text = "Imagination is more important than knowledge - Einstein"; + File file = new File(getTestDirectory(), "checksum-test.txt"); + FileUtils.writeStringToFile(file, text, "US-ASCII"); + + // compute the expected checksum + Checksum expectedChecksum = new CRC32(); + expectedChecksum.update(text.getBytes("US-ASCII"), 0, text.length()); + long expectedValue = expectedChecksum.getValue(); + + // compute the checksum of the file + Checksum testChecksum = new CRC32(); + Checksum resultChecksum = FileUtils.checksum(file, testChecksum); + long resultValue = resultChecksum.getValue(); + + assertSame(testChecksum, resultChecksum); + assertEquals(expectedValue, resultValue); + } + + public void testChecksumOnNullFile() throws Exception { + try { + FileUtils.checksum((File) null, new CRC32()); + fail(); + } catch (NullPointerException ex) { + // expected + } + } + + public void testChecksumOnNullChecksum() throws Exception { + // create a test file + String text = "Imagination is more important than knowledge - Einstein"; + File file = new File(getTestDirectory(), "checksum-test.txt"); + FileUtils.writeStringToFile(file, text, "US-ASCII"); + try { + FileUtils.checksum(file, (Checksum) null); + fail(); + } catch (NullPointerException ex) { + // expected + } + } + + public void testChecksumOnDirectory() throws Exception { + try { + FileUtils.checksum(new File("."), new CRC32()); + fail(); + } catch (IllegalArgumentException ex) { + // expected + } + } + + public void testChecksumDouble() throws Exception { + // create a test file + String text1 = "Imagination is more important than knowledge - Einstein"; + File file1 = new File(getTestDirectory(), "checksum-test.txt"); + FileUtils.writeStringToFile(file1, text1, "US-ASCII"); + + // create a second test file + String text2 = "To be or not to be - Shakespeare"; + File file2 = new File(getTestDirectory(), "checksum-test2.txt"); + FileUtils.writeStringToFile(file2, text2, "US-ASCII"); + + // compute the expected checksum + Checksum expectedChecksum = new CRC32(); + expectedChecksum.update(text1.getBytes("US-ASCII"), 0, text1.length()); + expectedChecksum.update(text2.getBytes("US-ASCII"), 0, text2.length()); + long expectedValue = expectedChecksum.getValue(); + + // compute the checksum of the file + Checksum testChecksum = new CRC32(); + FileUtils.checksum(file1, testChecksum); + FileUtils.checksum(file2, testChecksum); + long resultValue = testChecksum.getValue(); + + assertEquals(expectedValue, resultValue); } } --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]