commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject svn commit: r1402280 - in /commons/proper/io/trunk/src: changes/changes.xml main/java/org/apache/commons/io/IOUtils.java test/java/org/apache/commons/io/IOUtilsCopyTestCase.java
Date Thu, 25 Oct 2012 19:15:57 GMT
Author: ggregory
Date: Thu Oct 25 19:15:57 2012
New Revision: 1402280

URL: http://svn.apache.org/viewvc?rev=1402280&view=rev
Log:
[IO-353] Add API IOUtils.copy(InputStream, OutputStream, int).

Modified:
    commons/proper/io/trunk/src/changes/changes.xml
    commons/proper/io/trunk/src/main/java/org/apache/commons/io/IOUtils.java
    commons/proper/io/trunk/src/test/java/org/apache/commons/io/IOUtilsCopyTestCase.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=1402280&r1=1402279&r2=1402280&view=diff
==============================================================================
--- commons/proper/io/trunk/src/changes/changes.xml (original)
+++ commons/proper/io/trunk/src/changes/changes.xml Thu Oct 25 19:15:57 2012
@@ -47,6 +47,9 @@ The <action> type attribute can be add,u
   <body>
     <!-- The release date is the date RC is cut -->
     <release version="2.5" date="201?-??-??" description="New features and bug fixes.">
+      <action issue="IO-353" dev="ggregory" type="add" due-to="ggregory">
+        Add API IOUtils.copy(InputStream, OutputStream, int)
+      </action>            
       <action issue="IO-349" dev="ggregory" type="add" due-to="scop">
         Add API with array offset and length argument to FileUtils.writeByteArrayToFile.
       </action>            

Modified: commons/proper/io/trunk/src/main/java/org/apache/commons/io/IOUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/IOUtils.java?rev=1402280&r1=1402279&r2=1402280&view=diff
==============================================================================
--- commons/proper/io/trunk/src/main/java/org/apache/commons/io/IOUtils.java (original)
+++ commons/proper/io/trunk/src/main/java/org/apache/commons/io/IOUtils.java Thu Oct 25 19:15:57
2012
@@ -1749,6 +1749,30 @@ public class IOUtils {
     }
 
     /**
+     * Copy bytes from an <code>InputStream</code> to an <code>OutputStream</code>
using an internal buffer of the
+     * given size.
+     * <p>
+     * This method buffers the input internally, so there is no need to use a <code>BufferedInputStream</code>.
+     * <p>
+     * 
+     * @param input
+     *            the <code>InputStream</code> to read from
+     * @param output
+     *            the <code>OutputStream</code> to write to
+     * @param bufferSize
+     *            the bufferSize used to copy from the input to the output
+     * @return the number of bytes copied
+     * @throws NullPointerException
+     *             if the input or output is null
+     * @throws IOException
+     *             if an I/O error occurs
+     * @since 2.5
+     */
+    public static long copy(InputStream input, OutputStream output, int bufferSize) throws
IOException {
+        return copyLarge(input, output, new byte[bufferSize]);
+    }
+
+    /**
      * Copy bytes from a large (over 2GB) <code>InputStream</code> to an
      * <code>OutputStream</code>.
      * <p>
@@ -1766,7 +1790,7 @@ public class IOUtils {
      */
     public static long copyLarge(InputStream input, OutputStream output)
             throws IOException {
-        return copyLarge(input, output, new byte[DEFAULT_BUFFER_SIZE]);
+        return copy(input, output, DEFAULT_BUFFER_SIZE);
     }
 
     /**

Modified: commons/proper/io/trunk/src/test/java/org/apache/commons/io/IOUtilsCopyTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/java/org/apache/commons/io/IOUtilsCopyTestCase.java?rev=1402280&r1=1402279&r2=1402280&view=diff
==============================================================================
--- commons/proper/io/trunk/src/test/java/org/apache/commons/io/IOUtilsCopyTestCase.java (original)
+++ commons/proper/io/trunk/src/test/java/org/apache/commons/io/IOUtilsCopyTestCase.java Thu
Oct 25 19:15:57 2012
@@ -86,6 +86,39 @@ public class IOUtilsCopyTestCase extends
         assertEquals(inData.length,count);
     }
 
+    public void testCopy_inputStreamToOutputStreamWithBufferSize() throws Exception {
+        testCopy_inputStreamToOutputStreamWithBufferSize(1);
+        testCopy_inputStreamToOutputStreamWithBufferSize(2);
+        testCopy_inputStreamToOutputStreamWithBufferSize(4);
+        testCopy_inputStreamToOutputStreamWithBufferSize(8);
+        testCopy_inputStreamToOutputStreamWithBufferSize(16);
+        testCopy_inputStreamToOutputStreamWithBufferSize(32);
+        testCopy_inputStreamToOutputStreamWithBufferSize(64);
+        testCopy_inputStreamToOutputStreamWithBufferSize(128);
+        testCopy_inputStreamToOutputStreamWithBufferSize(256);
+        testCopy_inputStreamToOutputStreamWithBufferSize(512);
+        testCopy_inputStreamToOutputStreamWithBufferSize(1024);
+        testCopy_inputStreamToOutputStreamWithBufferSize(2048);
+        testCopy_inputStreamToOutputStreamWithBufferSize(4096);
+        testCopy_inputStreamToOutputStreamWithBufferSize(8192);
+        testCopy_inputStreamToOutputStreamWithBufferSize(16384);
+    }
+
+    private void testCopy_inputStreamToOutputStreamWithBufferSize(int bufferSize) throws
Exception {
+        InputStream in = new ByteArrayInputStream(inData);
+        in = new YellOnCloseInputStream(in);
+
+        ByteArrayOutputStream baout = new ByteArrayOutputStream();
+        OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
+
+        long count = IOUtils.copy(in, out, bufferSize);
+        
+        assertEquals("Not all bytes were read", 0, in.available());
+        assertEquals("Sizes differ", inData.length, baout.size());
+        assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
+        assertEquals(inData.length,count);
+    }
+
     public void testCopy_inputStreamToOutputStream_nullIn() throws Exception {
         OutputStream out = new ByteArrayOutputStream();
         try {



Mime
View raw message