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 2011/04/13 07:14:25 UTC
svn commit: r1091652 - in /commons/proper/io/trunk/src:
main/java/org/apache/commons/io/IOUtils.java
test/java/org/apache/commons/io/IOUtilsTestCase.java
Author: ggregory
Date: Wed Apr 13 05:14:25 2011
New Revision: 1091652
URL: http://svn.apache.org/viewvc?rev=1091652&view=rev
Log:
Add IOUtils.toString(URL) and IOUtils.toString(URL, 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=1091652&r1=1091651&r2=1091652&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:14:25 2011
@@ -32,6 +32,7 @@ import java.io.PrintWriter;
import java.io.Reader;
import java.io.Writer;
import java.net.Socket;
+import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
@@ -602,6 +603,37 @@ public class IOUtils {
}
/**
+ * 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.
+ */
+ public static String toString(URL url) throws IOException {
+ return toString(url, null);
+ }
+
+ /**
+ * Gets the contents at the given URL.
+ *
+ * @param url
+ * The URL 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.
+ */
+ public static String toString(URL url, String encoding) throws IOException {
+ InputStream inputStream = url.openStream();
+ try {
+ return toString(inputStream, encoding);
+ } finally {
+ inputStream.close();
+ }
+ }
+
+ /**
* Get the contents of a <code>byte[]</code> as a String
* using the default character encoding of the platform.
*
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=1091652&r1=1091651&r2=1091652&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:14:25 2011
@@ -27,6 +27,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
+import java.net.URL;
import java.util.Arrays;
import java.util.List;
@@ -137,17 +138,17 @@ public class IOUtilsTestCase extends Fil
}
public void testReaderToString()
- throws Exception
- {
- FileReader fin = new FileReader( m_testFile );
- try {
- String out = IOUtils.toString( fin );
- assertNotNull( out );
- assertEquals( "Wrong output size", FILE_SIZE, out.length());
- } finally {
- fin.close();
+ throws Exception
+ {
+ FileReader fin = new FileReader( m_testFile );
+ try {
+ String out = IOUtils.toString( fin );
+ assertNotNull( out );
+ assertEquals( "Wrong output size", FILE_SIZE, out.length());
+ } finally {
+ fin.close();
+ }
}
- }
@SuppressWarnings("deprecation") // testing deprecated method
public void testStringToOutputStream()
@@ -600,4 +601,35 @@ public class IOUtilsTestCase extends Fil
in.close();
}
}
+
+ private void testURLToString(String encoding)
+ throws Exception
+ {
+ URL url = m_testFile.toURI().toURL();
+ String out = IOUtils.toString(url, encoding);
+ assertNotNull(out);
+ assertEquals("Wrong output size", FILE_SIZE, out.length());
+ }
+
+ public void testURLToStringNoEncoding()
+ throws Exception
+ {
+ URL url = m_testFile.toURI().toURL();
+ String out = IOUtils.toString(url);
+ assertNotNull(out);
+ assertEquals("Wrong output size", FILE_SIZE, out.length());
+ }
+
+ public void testURLToStringNullEncoding()
+ throws Exception
+ {
+ testURLToString(null);
+ }
+
+ public void testURLToStringUsAciiEncoding()
+ throws Exception
+ {
+ testURLToString("US-ASCII");
+ }
+
}