Skip to content

Commit 3bb6e81

Browse files
committed
Add IOUtils.toString(URI) and IOUtils.toString(URI, String)
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@1091653 13f79535-47bb-0310-9956-ffa450edef68
1 parent c569005 commit 3bb6e81

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

src/main/java/org/apache/commons/io/IOUtils.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.io.Reader;
3333
import java.io.Writer;
3434
import java.net.Socket;
35+
import java.net.URI;
3536
import java.net.URL;
3637
import java.util.ArrayList;
3738
import java.util.Collection;
@@ -602,13 +603,42 @@ public static String toString(Reader input) throws IOException {
602603
return sw.toString();
603604
}
604605

606+
/**
607+
* Gets the contents at the given URI.
608+
*
609+
* @param uri
610+
* The URI source.
611+
* @return The contents of the URL as a String.
612+
* @throws IOException if an I/O exception occurs.
613+
* @since 2.1.
614+
*/
615+
public static String toString(URI uri) throws IOException {
616+
return toString(uri, null);
617+
}
618+
619+
/**
620+
* Gets the contents at the given URI.
621+
*
622+
* @param uri
623+
* The URI source.
624+
* @param encoding
625+
* The encoding name for the URL contents.
626+
* @return The contents of the URL as a String.
627+
* @throws IOException if an I/O exception occurs.
628+
* @since 2.1.
629+
*/
630+
public static String toString(URI uri, String encoding) throws IOException {
631+
return toString(uri.toURL(), encoding);
632+
}
633+
605634
/**
606635
* Gets the contents at the given URL.
607636
*
608637
* @param url
609638
* The URL source.
610639
* @return The contents of the URL as a String.
611640
* @throws IOException if an I/O exception occurs.
641+
* @since 2.1.
612642
*/
613643
public static String toString(URL url) throws IOException {
614644
return toString(url, null);
@@ -623,6 +653,7 @@ public static String toString(URL url) throws IOException {
623653
* The encoding name for the URL contents.
624654
* @return The contents of the URL as a String.
625655
* @throws IOException if an I/O exception occurs.
656+
* @since 2.1.
626657
*/
627658
public static String toString(URL url, String encoding) throws IOException {
628659
InputStream inputStream = url.openStream();

src/test/java/org/apache/commons/io/IOUtilsTestCase.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.io.InputStream;
2828
import java.io.InputStreamReader;
2929
import java.io.Reader;
30+
import java.net.URI;
3031
import java.net.URL;
3132
import java.util.Arrays;
3233
import java.util.List;
@@ -602,6 +603,36 @@ public void testSkipFileInput() throws Exception{
602603
}
603604
}
604605

606+
private void testURIToString(String encoding)
607+
throws Exception
608+
{
609+
URI url = m_testFile.toURI();
610+
String out = IOUtils.toString(url, encoding);
611+
assertNotNull(out);
612+
assertEquals("Wrong output size", FILE_SIZE, out.length());
613+
}
614+
615+
public void testURIToStringNoEncoding()
616+
throws Exception
617+
{
618+
URI url = m_testFile.toURI();
619+
String out = IOUtils.toString(url);
620+
assertNotNull(out);
621+
assertEquals("Wrong output size", FILE_SIZE, out.length());
622+
}
623+
624+
public void testURIToStringNullEncoding()
625+
throws Exception
626+
{
627+
testURIToString(null);
628+
}
629+
630+
public void testURIToStringUsAciiEncoding()
631+
throws Exception
632+
{
633+
testURIToString("US-ASCII");
634+
}
635+
605636
private void testURLToString(String encoding)
606637
throws Exception
607638
{

0 commit comments

Comments
 (0)