Class IOUtils
- Version:
- 1.0
- Author:
- Fyodor Kupolov
-
Method Summary
Modifier and TypeMethodDescriptionstatic BufferedReaderasBuffered(Reader reader) Optionally makes a buffered reader from the specified one.static BufferedWriterasBuffered(Writer writer) Optionally makes a buffered writer from the specified one.static voidcloseSilently(Closeable closeable) Silently closes data.static voidcloseSilently(Iterable<? extends Closeable> closeables) Silently closes a collection of objects.static OutputStreamgetOutputStream(URL url) Opens output stream for specified URL.static ReadergetReader(InputStream is, String enc) static ReadergetReader(InputStream is, String enc, boolean buffered) Returns reader for specified input stream and charset name.static WritergetWriter(OutputStream os, String enc) static WritergetWriter(OutputStream os, String enc, boolean buffered) Returns writer for specified output stream and charset name.static URLResolves specified uri to a specified base URL.static byte[]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 StringLoads a reader content into a string.static StringLoads a reader content into a string.static URLA replacement for a deprecated File.toURL() method.
-
Method Details
-
closeSilently
Silently closes data.- Parameters:
closeable- data to close
-
closeSilently
Silently closes a collection of objects.- Parameters:
closeables- iterable closeables. Null value allowed.- See Also:
-
toString
Loads a reader content into a string.Note:The content length is limited by
MAX_LENGTHcharacters.- Parameters:
reader- reader to load content from. Closed at the end of the operation.- Returns:
- string representation of reader content.
- Throws:
IOException
-
toString
Loads a reader content into a string.- Parameters:
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.- Returns:
- string representation of reader content.
- Throws:
IOException
-
toByteArray
Loads an input stream content into a byte array.Note:The content length is limited by
MAX_LENGTHbytes.- Parameters:
is- stream to load. Closed at the end of the operation.- Returns:
- stream bytes
- Throws:
IOException- if I/O error occurs or stream length exceeds theMAX_LENGTH.
-
toByteArray
Loads an input stream content into a byte array.- Parameters:
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.- Returns:
- stream bytes
- Throws:
IOException
-
getOutputStream
Opens output stream for specified URL.This method is a helper for url.openConnection().getOutputStream(). Additionally a file: URLs are supported, see FileURLConnection doesn't implement getOutputStream()
- Parameters:
url- URL to open an output stream.- Returns:
- output stream for URL.
- Throws:
IOException- if an I/O error occurs while creating the output stream.
-
getReader
- Returns:
- buffered reader for specified input stream.
- Throws:
UnsupportedEncodingException- See Also:
-
getReader
public static Reader getReader(InputStream is, String enc, boolean buffered) throws UnsupportedEncodingException Returns reader for specified input stream and charset name.- Parameters:
is- source input stream.enc- charset name, null means default.buffered- true if buffered reader should be used.- Returns:
- reader for inputstream.
- Throws:
UnsupportedEncodingException- If the named charset is not supported
-
asBuffered
Optionally makes a buffered reader from the specified one.If specified reader is buffered the object is returned unchanged.
- Parameters:
reader- reader to convert.- Returns:
- buffered reader.
-
asBuffered
Optionally makes a buffered writer from the specified one.If specified writer is buffered the object is returned unchanged.
- Parameters:
writer- writer to convert.- Returns:
- buffered writer.
-
getWriter
- Returns:
- buffered writer for specified output stream.
- Throws:
IOException- See Also:
-
getWriter
Returns writer for specified output stream and charset name.- Parameters:
os- source output stream.enc- charset name, null means default.buffered- true if buffered reader should be used.- Returns:
- reader for inputstream.
- Throws:
UnsupportedEncodingException- If the named charset is not supportedIOException
-
toUrl
A replacement for a deprecated File.toURL() method.- Parameters:
file- file to convert to URL.- Returns:
- URL representing the file location.
- Throws:
MalformedURLException- If a protocol handler for the URL could not be found, or if some other error occurred while constructing the URL.
-
resolve
Resolves specified uri to a specified base URL.This method use
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.
- Parameters:
base- base URL to use for resulution.uri- a relative or an absolute URI.- Returns:
- URL resolved relatively to the base URL.
- Throws:
MalformedURLException- if specified URI cannot be resolved.
-