You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2012/04/16 16:54:53 UTC

svn commit: r1326636 - 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: ggregory
Date: Mon Apr 16 14:54:53 2012
New Revision: 1326636

URL: http://svn.apache.org/viewvc?rev=1326636&view=rev
Log:
[IO-325] Add IOUtils.toByteArray methods to work with URL and URI.

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=1326636&r1=1326635&r2=1326636&view=diff
==============================================================================
--- commons/proper/io/trunk/src/changes/changes.xml (original)
+++ commons/proper/io/trunk/src/changes/changes.xml Mon Apr 16 14:54:53 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.4" date="2012-TDB-TDB" description="">
+      <action issue="IO-325" dev="ggregory" type="fix" due-to="raviprak">
+        Add IOUtils.toByteArray methods to work with URL and URI.
+      </action>            
       <action issue="IO-324" dev="ggregory" type="fix" due-to="raviprak">
         Add missing Charset sister APIs to method that take a String charset name.
       </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=1326636&r1=1326635&r2=1326636&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 Apr 16 14:54:53 2012
@@ -32,10 +32,12 @@ import java.io.PrintWriter;
 import java.io.Reader;
 import java.io.UnsupportedEncodingException;
 import java.io.Writer;
+import java.net.HttpURLConnection;
 import java.net.ServerSocket;
 import java.net.Socket;
 import java.net.URI;
 import java.net.URL;
+import java.net.URLConnection;
 import java.nio.channels.Selector;
 import java.nio.charset.Charset;
 import java.nio.charset.UnsupportedCharsetException;
@@ -151,6 +153,19 @@ public class IOUtils {
     }
 
     //-----------------------------------------------------------------------
+    
+    /**
+     * Closes a URLConnection.
+     * 
+     * @param conn the connection to close.
+     * @since 2.4
+     */
+    public static void close(URLConnection conn) {
+        if (conn instanceof HttpURLConnection) {
+            ((HttpURLConnection) conn).disconnect();
+        }
+    }
+
     /**
      * Unconditionally close an <code>Reader</code>.
      * <p>
@@ -586,6 +601,64 @@ public class IOUtils {
         return input.getBytes();
     }
 
+    /**
+     * Get the contents of a <code>URI</code> as a <code>byte[]</code>.
+     * 
+     * @param uri
+     *            the <code>URI</code> to read
+     * @return the requested byte array
+     * @throws NullPointerException
+     *             if the uri is null
+     * @throws IOException
+     *             if an I/O exception occurs
+     * @since 2.4
+     */
+    public static byte[] toByteArray(URI uri) throws IOException {
+        return IOUtils.toByteArray(uri.toURL());
+    }
+
+    /**
+     * Get the contents of a <code>URL</code> as a <code>byte[]</code>.
+     * 
+     * @param url
+     *            the <code>URL</code> to read
+     * @return the requested byte array
+     * @throws NullPointerException
+     *             if the input is null
+     * @throws IOException
+     *             if an I/O exception occurs
+     * @since 2.4
+     */
+    public static byte[] toByteArray(URL url) throws IOException {
+        URLConnection conn = url.openConnection();
+        try {
+            return IOUtils.toByteArray(conn);
+        } finally {
+            close(conn);
+        }
+    }
+
+    /**
+     * Get the contents of a <code>URLConnection</code> as a <code>byte[]</code>.
+     * 
+     * @param urlConn
+     *            the <code>URLConnection</code> to read
+     * @return the requested byte array
+     * @throws NullPointerException
+     *             if the urlConn is null
+     * @throws IOException
+     *             if an I/O exception occurs
+     * @since 2.4
+     */
+    public static byte[] toByteArray(URLConnection urlConn) throws IOException {
+        InputStream inputStream = urlConn.getInputStream();
+        try {
+            return IOUtils.toByteArray(inputStream);
+        } finally {
+            inputStream.close();
+        }
+    }
+
     // read char[]
     //-----------------------------------------------------------------------
     /**

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=1326636&r1=1326635&r2=1326636&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 Apr 16 14:54:53 2012
@@ -37,6 +37,7 @@ import java.net.ServerSocket;
 import java.net.Socket;
 import java.net.URI;
 import java.net.URL;
+import java.net.URLConnection;
 import java.nio.channels.Selector;
 import java.util.Arrays;
 import java.util.List;
@@ -440,6 +441,29 @@ public class IOUtilsTestCase extends Fil
         }
     }
 
+    public void testToByteArrayFromURI() throws Exception {
+        URI url = m_testFile.toURI();
+        byte[] actual = IOUtils.toByteArray(url);
+        Assert.assertEquals(FILE_SIZE, actual.length);
+    }
+
+    public void testToByteArrayFromURL() throws Exception {
+        URL url = m_testFile.toURI().toURL();
+        byte[] actual = IOUtils.toByteArray(url);
+        Assert.assertEquals(FILE_SIZE, actual.length);
+    }
+
+    public void testToByteArrayFromURLConnection() throws Exception {
+        URLConnection urlConn = m_testFile.toURI().toURL().openConnection();
+        byte[] actual;
+        try {
+            actual = IOUtils.toByteArray(urlConn);
+        } finally {
+            IOUtils.close(urlConn);
+        }
+        Assert.assertEquals(FILE_SIZE, actual.length);
+    }
+
     /**
      * Test for {@link IOUtils#toInputStream(CharSequence)} and {@link IOUtils#toInputStream(CharSequence, String)}.
      * Note, this test utilizes on {@link IOUtils#toByteArray(java.io.InputStream)} and so relies on