public final class IOUtils extends Object
| Modifier and Type | Method and Description |
|---|---|
static BufferedReader |
asBuffered(Reader reader)
Optionally makes a buffered reader from the specified one.
|
static BufferedWriter |
asBuffered(Writer writer)
Optionally makes a buffered writer from the specified one.
|
static void |
closeSilently(Closeable closeable)
Silently closes data.
|
static void |
closeSilently(Iterable<? extends Closeable> closeables)
Silently closes a collection of objects.
|
static OutputStream |
getOutputStream(URL url)
Opens output stream for specified URL.
|
static Reader |
getReader(InputStream is,
String enc) |
static Reader |
getReader(InputStream is,
String enc,
boolean buffered)
Returns reader for specified input stream and charset name.
|
static Writer |
getWriter(OutputStream os,
String enc) |
static Writer |
getWriter(OutputStream os,
String enc,
boolean buffered)
Returns writer for specified output stream and charset name.
|
static URL |
resolve(URL base,
String uri)
Resolves specified uri to a specified base URL.
|
static byte[] |
toByteArray(InputStream is)
Loads an input stream content into a byte array.
|
static byte[] |
toByteArray(InputStream is,
long maxLength)
Loads an input stream content into a byte array.
|
static String |
toString(Reader reader)
Loads a reader content into a string.
|
static String |
toString(Reader reader,
long maxLength)
Loads a reader content into a string.
|
static URL |
toUrl(File file)
A replacement for a deprecated File.toURL() method.
|
public static void closeSilently(Closeable closeable)
closeable - data to closepublic static void closeSilently(Iterable<? extends Closeable> closeables)
closeables - iterable closeables. Null value allowed.closeSilently(java.io.Closeable)public static String toString(Reader reader) throws IOException
Note:The content length is limited by MAX_LENGTH characters.
reader - reader to load content from. Closed at the end of the operation.IOExceptionpublic static String toString(Reader reader, long maxLength) throws IOException
reader - reader to load content from. Closed at the end of the operation.maxLength - max number of characters to read before throwing a Content Too Long Exception.IOExceptionpublic static byte[] toByteArray(InputStream is) throws IOException
Note:The content length is limited by MAX_LENGTH bytes.
is - stream to load. Closed at the end of the operation.IOException - if I/O error occurs or stream length exceeds the MAX_LENGTH.public static byte[] toByteArray(InputStream is, long maxLength) throws IOException
is - stream to load. Closed at the end of the operation.maxLength - maxLength max number of bytes to read before throwing a Content Too Long Exception.IOExceptionpublic static OutputStream getOutputStream(URL url) throws IOException
This method is a helper for url.openConnection().getOutputStream(). Additionally a file: URLs are supported, see FileURLConnection doesn't implement getOutputStream()
url - URL to open an output stream.IOException - if an I/O error occurs while creating the output stream.public static Reader getReader(InputStream is, String enc) throws UnsupportedEncodingException
UnsupportedEncodingExceptiongetReader(java.io.InputStream, String, boolean)public static Reader getReader(InputStream is, String enc, boolean buffered) throws UnsupportedEncodingException
is - source input stream.enc - charset name, null means default.buffered - true if buffered reader should be used.UnsupportedEncodingException - If the named charset is not supportedpublic static BufferedReader asBuffered(Reader reader)
If specified reader is buffered the object is returned unchanged.
reader - reader to convert.public static BufferedWriter asBuffered(Writer writer)
If specified writer is buffered the object is returned unchanged.
writer - writer to convert.public static Writer getWriter(OutputStream os, String enc) throws IOException
IOExceptiongetWriter(java.io.OutputStream, String, boolean)public static Writer getWriter(OutputStream os, String enc, boolean buffered) throws IOException
os - source output stream.enc - charset name, null means default.buffered - true if buffered reader should be used.UnsupportedEncodingException - If the named charset is not supportedIOExceptionpublic static URL toUrl(File file) throws MalformedURLException
file - file to convert to URL.MalformedURLException - If a protocol handler for the URL could not be found,
or if some other error occurred while constructing the URL.public static URL resolve(URL base, String uri) throws MalformedURLException
This method use URL.URL(URL,String) constructor and handles additional checks
if URL cannot be resolved by a standard mechanism.
Typical example is handling windows absolute paths with forward slashes. These paths are malformed URIs, but Scriptella recognize them and converts to URL if no protocol handler has been registered.
In future we can add support for default URL stream handlers in addition to the ones supported by the JRE.
base - base URL to use for resulution.uri - a relative or an absolute URI.MalformedURLException - if specified URI cannot be resolved.Copyright © 2006-2012. All Rights Reserved.