You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by se...@apache.org on 2013/11/25 03:59:41 UTC

svn commit: r1545141 - in /commons/proper/io/trunk/src: changes/changes.xml main/java/org/apache/commons/io/IOUtils.java test/java/org/apache/commons/io/IOUtilsTestCase.java

Author: sebb
Date: Mon Nov 25 02:59:40 2013
New Revision: 1545141

URL: http://svn.apache.org/r1545141
Log:
IO-410 Readfully() That Returns A Byte Array

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/IOUtilsTestCase.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=1545141&r1=1545140&r2=1545141&view=diff
==============================================================================
--- commons/proper/io/trunk/src/changes/changes.xml (original)
+++ commons/proper/io/trunk/src/changes/changes.xml Mon Nov 25 02:59:40 2013
@@ -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="2013-??-??" description="New features and bug fixes.">    
+      <action issue="IO-410" dev="sebb" type="add" due-to="BELUGA BEHR">
+         Readfully() That Returns A Byte Array
+      </action>
       <action issue="IO-395" dev="brentworden" type="add" due-to="BELUGA BEHR">
          Overload IOUtils buffer methods to accept buffer size
       </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=1545141&r1=1545140&r2=1545141&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 Mon Nov 25 02:59:40 2013
@@ -2969,6 +2969,25 @@ public class IOUtils {
     /**
      * Reads the requested number of bytes or fail if there are not enough left.
      * <p>
+     * This allows for the possibility that {@link InputStream#read(byte[], int, int)} may
+     * not read as many bytes as requested (most likely because of reaching EOF).
+     *
+     * @param input where to read input from
+     * @param length length to read, must be >= 0
+     *
+     * @throws IOException if there is a problem reading the file
+     * @throws IllegalArgumentException if length is negative
+     * @throws EOFException if the number of bytes read was incorrect
+     */
+    public static byte[] readFully(final InputStream input, final int length) throws IOException {
+        final byte[] buffer = new byte[length];
+        readFully(input, buffer, 0, buffer.length);
+        return buffer;
+    }
+
+    /**
+     * Reads the requested number of bytes or fail if there are not enough left.
+     * <p>
      * This allows for the possibility that {@link ReadableByteChannel#read(ByteBuffer)} may
      * not read as many bytes as requested (most likely because of reaching EOF).
      *

Modified: commons/proper/io/trunk/src/test/java/org/apache/commons/io/IOUtilsTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/java/org/apache/commons/io/IOUtilsTestCase.java?rev=1545141&r1=1545140&r2=1545141&view=diff
==============================================================================
--- commons/proper/io/trunk/src/test/java/org/apache/commons/io/IOUtilsTestCase.java (original)
+++ commons/proper/io/trunk/src/test/java/org/apache/commons/io/IOUtilsTestCase.java Mon Nov 25 02:59:40 2013
@@ -642,6 +642,17 @@ public class IOUtilsTestCase extends Fil
 
     }
 
+    public void testReadFully_InputStream__ReturnByteArray() throws Exception {
+        final byte[] bytes = "abcd1234".getBytes("UTF-8");
+        final ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
+        
+        final byte[] result = IOUtils.readFully(stream, bytes.length);
+        
+        IOUtils.closeQuietly(stream);
+        
+        assertEqualContent(result, bytes);
+    }
+
     public void testReadFully_InputStream_Offset() throws Exception {
         final byte[] bytes = "abcd1234".getBytes("UTF-8");
         final ByteArrayInputStream stream = new ByteArrayInputStream(bytes);