Author: ggregory Date: Tue Aug 30 17:22:38 2011 New Revision: 1163295 URL: http://svn.apache.org/viewvc?rev=1163295&view=rev Log: [IO-282] Add API FileUtils.copyFile(File input, OutputStream output)
Modified: commons/proper/io/trunk/src/changes/changes.xml commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsTestCase.java Modified: commons/proper/io/trunk/src/changes/changes.xml URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/changes/changes.xml?rev=1163295&r1=1163294&r2=1163295&view=diff ============================================================================== --- commons/proper/io/trunk/src/changes/changes.xml (original) +++ commons/proper/io/trunk/src/changes/changes.xml Tue Aug 30 17:22:38 2011 @@ -43,6 +43,9 @@ The <action> type attribute can be add,u <action dev="sebb" type="fix" issue="IO-280" due-to="sebb"> Dubious use of mkdirs() return code </action> + <action dev="ggregory" type="add" issue="IO-282" due-to="ggregory"> + Add API FileUtils.copyFile(File input, OutputStream output) + </action> <!-- TODO check where these fixes really belong --> <action dev="sebb" type="fix" issue="IO-274" due-to="Frank Grimes"> Modified: commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java?rev=1163295&r1=1163294&r2=1163295&view=diff ============================================================================== --- commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java (original) +++ commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java Tue Aug 30 17:22:38 2011 @@ -889,6 +889,32 @@ public class FileUtils { } /** + * Copy bytes from a <code>File</code> to an <code>OutputStream</code>. + * <p> + * This method buffers the input internally, so there is no need to use a <code>BufferedInputStream</code>. + * </p> + * + * @param input + * the <code>File</code> to read from + * @param output + * the <code>OutputStream</code> to write to + * @return the number of bytes copied + * @throws NullPointerException + * if the input or output is null + * @throws IOException + * if an I/O error occurs + * @since Commons IO 2.1 + */ + public static long copyFile(File input, OutputStream output) throws IOException { + final FileInputStream fis = new FileInputStream(input); + try { + return IOUtils.copyLarge(fis, output); + } finally { + fis.close(); + } + } + + /** * Internal copy file method. * * @param srcFile the validated source file, must not be <code>null</code> Modified: commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsTestCase.java URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsTestCase.java?rev=1163295&r1=1163294&r2=1163295&view=diff ============================================================================== --- commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsTestCase.java (original) +++ commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsTestCase.java Tue Aug 30 17:22:38 2011 @@ -16,6 +16,7 @@ */ package org.apache.commons.io; +import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; @@ -38,6 +39,7 @@ import java.util.zip.Checksum; import org.apache.commons.io.filefilter.NameFileFilter; import org.apache.commons.io.filefilter.WildcardFileFilter; import org.apache.commons.io.testtools.FileBasedTestCase; +import org.junit.Assert; /** * This is used to test FileUtils for correctness. @@ -836,6 +838,14 @@ public class FileUtilsTestCase extends F testFile1.lastModified() == destination.lastModified());*/ } + public void testCopyFileToOutputStream() throws Exception { + ByteArrayOutputStream destination = new ByteArrayOutputStream(); + FileUtils.copyFile(testFile1, destination); + assertEquals("Check Full copy size", testFile1Size, destination.size()); + byte[] expected = FileUtils.readFileToByteArray(testFile1); + Assert.assertArrayEquals("Check Full copy", expected, destination.toByteArray()); + } + public void IGNOREtestCopyFileLarge() throws Exception { File largeFile = new File(getTestDirectory(), "large.txt");