Hi All:
I am looking for a home for the code below. It seems to fit nicely in [io]
IOUtils because it reuses IOUtils. Because it is about URLs and URI it feels
like it should be in [net] perhaps.
Thoughts?
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import org.apache.commons.io.IOUtils;
/**
* URI utilities. TODO: Propose to Apache Commons IO or NET.
*/
public class URLUtils {
/**
* Gets the contents of the URL resource.
*
* @param url
* The URL source.
* @param charset
* The charset for the URL contents.
* @return The contents of the URL as a String.
* @throws IOException
*/
public static String getContentsByURL(URL url, String charset) throws
IOException {
InputStream inputStream = url.openStream();
try {
return IOUtils.toString(inputStream, charset);
} finally {
inputStream.close();
}
}
}
==
/**
* URI utilities. TODO: Propose to Apache Commons IO or NET.
*/
public class URIUtils {
/**
* Gets the contents of the URI resource.
*
* @param uriString
* The URI source.
* @param charset
* The charset for the URI contents.
* @return The contents of the URI as a String.
* @throws URISyntaxException
* @throws MalformedURLException
* @throws IOException
*/
public static String getContentsByURI(String uriString, String charset)
throws URISyntaxException, MalformedURLException, IOException {
return URIUtils.getContentsByURI(new URI(uriString), charset);
}
/**
* Gets the contents of the URI resource.
*
* @param uri
* The URI source.
* @param charset
* The charset for the URI contents.
* @return The contents of the URI as a String.
* @throws MalformedURLException
* @throws IOException
*/
public static String getContentsByURI(URI uri, String charset) throws
MalformedURLException, IOException {
return URLUtils.getContentsByURL(uri.toURL(), charset);
}
}
--
Thank you,
Gary
http://garygregory.wordpress.com/
http://garygregory.com/
http://people.apache.org/~ggregory/
http://twitter.com/GaryGregory