Author: ggregory Date: Wed Apr 13 05:22:45 2011 New Revision: 1091653 URL: http://svn.apache.org/viewvc?rev=1091653&view=rev Log: Add IOUtils.toString(URI) and IOUtils.toString(URI, String)
Modified: 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/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=1091653&r1=1091652&r2=1091653&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 Wed Apr 13 05:22:45 2011 @@ -32,6 +32,7 @@ import java.io.PrintWriter; import java.io.Reader; import java.io.Writer; import java.net.Socket; +import java.net.URI; import java.net.URL; import java.util.ArrayList; import java.util.Collection; @@ -603,12 +604,41 @@ public class IOUtils { } /** + * Gets the contents at the given URI. + * + * @param uri + * The URI source. + * @return The contents of the URL as a String. + * @throws IOException if an I/O exception occurs. + * @since 2.1. + */ + public static String toString(URI uri) throws IOException { + return toString(uri, null); + } + + /** + * Gets the contents at the given URI. + * + * @param uri + * The URI source. + * @param encoding + * The encoding name for the URL contents. + * @return The contents of the URL as a String. + * @throws IOException if an I/O exception occurs. + * @since 2.1. + */ + public static String toString(URI uri, String encoding) throws IOException { + return toString(uri.toURL(), encoding); + } + + /** * Gets the contents at the given URL. * * @param url * The URL source. * @return The contents of the URL as a String. * @throws IOException if an I/O exception occurs. + * @since 2.1. */ public static String toString(URL url) throws IOException { return toString(url, null); @@ -623,6 +653,7 @@ public class IOUtils { * The encoding name for the URL contents. * @return The contents of the URL as a String. * @throws IOException if an I/O exception occurs. + * @since 2.1. */ public static String toString(URL url, String encoding) throws IOException { InputStream inputStream = url.openStream(); 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=1091653&r1=1091652&r2=1091653&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 Wed Apr 13 05:22:45 2011 @@ -27,6 +27,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; +import java.net.URI; import java.net.URL; import java.util.Arrays; import java.util.List; @@ -602,6 +603,36 @@ public class IOUtilsTestCase extends Fil } } + private void testURIToString(String encoding) + throws Exception + { + URI url = m_testFile.toURI(); + String out = IOUtils.toString(url, encoding); + assertNotNull(out); + assertEquals("Wrong output size", FILE_SIZE, out.length()); + } + + public void testURIToStringNoEncoding() + throws Exception + { + URI url = m_testFile.toURI(); + String out = IOUtils.toString(url); + assertNotNull(out); + assertEquals("Wrong output size", FILE_SIZE, out.length()); + } + + public void testURIToStringNullEncoding() + throws Exception + { + testURIToString(null); + } + + public void testURIToStringUsAciiEncoding() + throws Exception + { + testURIToString("US-ASCII"); + } + private void testURLToString(String encoding) throws Exception {