Skip to content

Commit c569005

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

2 files changed

Lines changed: 74 additions & 10 deletions

File tree

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

Lines changed: 32 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.URL;
3536
import java.util.ArrayList;
3637
import java.util.Collection;
3738
import java.util.List;
@@ -601,6 +602,37 @@ public static String toString(Reader input) throws IOException {
601602
return sw.toString();
602603
}
603604

605+
/**
606+
* Gets the contents at the given URL.
607+
*
608+
* @param url
609+
* The URL source.
610+
* @return The contents of the URL as a String.
611+
* @throws IOException if an I/O exception occurs.
612+
*/
613+
public static String toString(URL url) throws IOException {
614+
return toString(url, null);
615+
}
616+
617+
/**
618+
* Gets the contents at the given URL.
619+
*
620+
* @param url
621+
* The URL source.
622+
* @param encoding
623+
* The encoding name for the URL contents.
624+
* @return The contents of the URL as a String.
625+
* @throws IOException if an I/O exception occurs.
626+
*/
627+
public static String toString(URL url, String encoding) throws IOException {
628+
InputStream inputStream = url.openStream();
629+
try {
630+
return toString(inputStream, encoding);
631+
} finally {
632+
inputStream.close();
633+
}
634+
}
635+
604636
/**
605637
* Get the contents of a <code>byte[]</code> as a String
606638
* using the default character encoding of the platform.

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

Lines changed: 42 additions & 10 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.URL;
3031
import java.util.Arrays;
3132
import java.util.List;
3233

@@ -137,17 +138,17 @@ public void testInputStreamToString()
137138
}
138139

139140
public void testReaderToString()
140-
throws Exception
141-
{
142-
FileReader fin = new FileReader( m_testFile );
143-
try {
144-
String out = IOUtils.toString( fin );
145-
assertNotNull( out );
146-
assertEquals( "Wrong output size", FILE_SIZE, out.length());
147-
} finally {
148-
fin.close();
141+
throws Exception
142+
{
143+
FileReader fin = new FileReader( m_testFile );
144+
try {
145+
String out = IOUtils.toString( fin );
146+
assertNotNull( out );
147+
assertEquals( "Wrong output size", FILE_SIZE, out.length());
148+
} finally {
149+
fin.close();
150+
}
149151
}
150-
}
151152

152153
@SuppressWarnings("deprecation") // testing deprecated method
153154
public void testStringToOutputStream()
@@ -600,4 +601,35 @@ public void testSkipFileInput() throws Exception{
600601
in.close();
601602
}
602603
}
604+
605+
private void testURLToString(String encoding)
606+
throws Exception
607+
{
608+
URL url = m_testFile.toURI().toURL();
609+
String out = IOUtils.toString(url, encoding);
610+
assertNotNull(out);
611+
assertEquals("Wrong output size", FILE_SIZE, out.length());
612+
}
613+
614+
public void testURLToStringNoEncoding()
615+
throws Exception
616+
{
617+
URL url = m_testFile.toURI().toURL();
618+
String out = IOUtils.toString(url);
619+
assertNotNull(out);
620+
assertEquals("Wrong output size", FILE_SIZE, out.length());
621+
}
622+
623+
public void testURLToStringNullEncoding()
624+
throws Exception
625+
{
626+
testURLToString(null);
627+
}
628+
629+
public void testURLToStringUsAciiEncoding()
630+
throws Exception
631+
{
632+
testURLToString("US-ASCII");
633+
}
634+
603635
}

0 commit comments

Comments
 (0)