public final class IOUtils
extends java.lang.Object
Modifier and Type | Method and Description |
---|---|
static java.io.BufferedReader |
asBuffered(java.io.Reader reader)
Optionally makes a buffered reader from the specified one.
|
static java.io.BufferedWriter |
asBuffered(java.io.Writer writer)
Optionally makes a buffered writer from the specified one.
|
static void |
closeSilently(java.io.Closeable closeable)
Silently closes data.
|
static void |
closeSilently(java.lang.Iterable<? extends java.io.Closeable> closeables)
Silently closes a collection of objects.
|
static java.io.OutputStream |
getOutputStream(java.net.URL url)
Opens output stream for specified URL.
|
static java.io.Reader |
getReader(java.io.InputStream is,
java.lang.String enc) |
static java.io.Reader |
getReader(java.io.InputStream is,
java.lang.String enc,
boolean buffered)
Returns reader for specified input stream and charset name.
|
static java.io.Writer |
getWriter(java.io.OutputStream os,
java.lang.String enc) |
static java.io.Writer |
getWriter(java.io.OutputStream os,
java.lang.String enc,
boolean buffered)
Returns writer for specified output stream and charset name.
|
static java.net.URL |
resolve(java.net.URL base,
java.lang.String uri)
Resolves specified uri to a specified base URL.
|
static byte[] |
toByteArray(java.io.InputStream is)
Loads an input stream content into a byte array.
|
static byte[] |
toByteArray(java.io.InputStream is,
long maxLength)
Loads an input stream content into a byte array.
|
static java.lang.String |
toString(java.io.Reader reader)
Loads a reader content into a string.
|
static java.lang.String |
toString(java.io.Reader reader,
long maxLength)
Loads a reader content into a string.
|
static java.net.URL |
toUrl(java.io.File file)
A replacement for a deprecated File.toURL() method.
|
public static void closeSilently(java.io.Closeable closeable)
closeable
- data to closepublic static void closeSilently(java.lang.Iterable<? extends java.io.Closeable> closeables)
closeables
- iterable closeables. Null value allowed.closeSilently(java.io.Closeable)
public static java.lang.String toString(java.io.Reader reader) throws java.io.IOException
Note:The content length is limited by MAX_LENGTH
characters.
reader
- reader to load content from. Closed at the end of the operation.java.io.IOException
public static java.lang.String toString(java.io.Reader reader, long maxLength) throws java.io.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.java.io.IOException
public static byte[] toByteArray(java.io.InputStream is) throws java.io.IOException
Note:The content length is limited by MAX_LENGTH
bytes.
is
- stream to load. Closed at the end of the operation.java.io.IOException
- if I/O error occurs or stream length exceeds the MAX_LENGTH
.public static byte[] toByteArray(java.io.InputStream is, long maxLength) throws java.io.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.java.io.IOException
public static java.io.OutputStream getOutputStream(java.net.URL url) throws java.io.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.java.io.IOException
- if an I/O error occurs while creating the output stream.public static java.io.Reader getReader(java.io.InputStream is, java.lang.String enc) throws java.io.UnsupportedEncodingException
java.io.UnsupportedEncodingException
getReader(java.io.InputStream, String, boolean)
public static java.io.Reader getReader(java.io.InputStream is, java.lang.String enc, boolean buffered) throws java.io.UnsupportedEncodingException
is
- source input stream.enc
- charset name, null means default.buffered
- true if buffered reader should be used.java.io.UnsupportedEncodingException
- If the named charset is not supportedpublic static java.io.BufferedReader asBuffered(java.io.Reader reader)
If specified reader is buffered the object is returned unchanged.
reader
- reader to convert.public static java.io.BufferedWriter asBuffered(java.io.Writer writer)
If specified writer is buffered the object is returned unchanged.
writer
- writer to convert.public static java.io.Writer getWriter(java.io.OutputStream os, java.lang.String enc) throws java.io.IOException
java.io.IOException
getWriter(java.io.OutputStream, String, boolean)
public static java.io.Writer getWriter(java.io.OutputStream os, java.lang.String enc, boolean buffered) throws java.io.IOException
os
- source output stream.enc
- charset name, null means default.buffered
- true if buffered reader should be used.java.io.UnsupportedEncodingException
- If the named charset is not supportedjava.io.IOException
public static java.net.URL toUrl(java.io.File file) throws java.net.MalformedURLException
file
- file to convert to URL.java.net.MalformedURLException
- If a protocol handler for the URL could not be found,
or if some other error occurred while constructing the URL.public static java.net.URL resolve(java.net.URL base, java.lang.String uri) throws java.net.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.java.net.MalformedURLException
- if specified URI cannot be resolved.Copyright © Copyright 2006-2019 The Scriptella Project Team.