scriptella.util
Class IOUtils

java.lang.Object
  extended by scriptella.util.IOUtils

public final class IOUtils
extends java.lang.Object

I/O utility methods.

Version:
1.0
Author:
Fyodor Kupolov

Method Summary
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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

closeSilently

public static void closeSilently(java.io.Closeable closeable)
Silently closes data.

Parameters:
closeable - data to close

closeSilently

public static void closeSilently(java.lang.Iterable<? extends java.io.Closeable> closeables)
Silently closes a collection of objects.

Parameters:
closeables - iterable closeables. Null value allowed.
See Also:
closeSilently(java.io.Closeable)

toString

public static java.lang.String toString(java.io.Reader reader)
                                 throws java.io.IOException
Loads a reader content into a string.

Note:The content length is limited by MAX_LENGTH characters.

Parameters:
reader - reader to load content from. Closed at the end of the operation.
Returns:
string representation of reader content.
Throws:
java.io.IOException

toString

public static java.lang.String toString(java.io.Reader reader,
                                        long maxLength)
                                 throws java.io.IOException
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:
java.io.IOException

toByteArray

public static byte[] toByteArray(java.io.InputStream is)
                          throws java.io.IOException
Loads an input stream content into a byte array.

Note:The content length is limited by MAX_LENGTH bytes.

Parameters:
is - stream to load. Closed at the end of the operation.
Returns:
stream bytes
Throws:
java.io.IOException - if I/O error occurs or stream length exceeds the MAX_LENGTH.

toByteArray

public static byte[] toByteArray(java.io.InputStream is,
                                 long maxLength)
                          throws java.io.IOException
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:
java.io.IOException

getOutputStream

public static java.io.OutputStream getOutputStream(java.net.URL url)
                                            throws java.io.IOException
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:
java.io.IOException - if an I/O error occurs while creating the output stream.

getReader

public static java.io.Reader getReader(java.io.InputStream is,
                                       java.lang.String enc)
                                throws java.io.UnsupportedEncodingException
Returns:
buffered reader for specified input stream.
Throws:
java.io.UnsupportedEncodingException
See Also:
getReader(java.io.InputStream, String, boolean)

getReader

public static java.io.Reader getReader(java.io.InputStream is,
                                       java.lang.String enc,
                                       boolean buffered)
                                throws java.io.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:
java.io.UnsupportedEncodingException - If the named charset is not supported

asBuffered

public static java.io.BufferedReader asBuffered(java.io.Reader reader)
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

public static java.io.BufferedWriter asBuffered(java.io.Writer writer)
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

public static java.io.Writer getWriter(java.io.OutputStream os,
                                       java.lang.String enc)
                                throws java.io.IOException
Returns:
buffered writer for specified output stream.
Throws:
java.io.IOException
See Also:
getWriter(java.io.OutputStream, String, boolean)

getWriter

public static java.io.Writer getWriter(java.io.OutputStream os,
                                       java.lang.String enc,
                                       boolean buffered)
                                throws java.io.IOException
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:
java.io.UnsupportedEncodingException - If the named charset is not supported
java.io.IOException

toUrl

public static java.net.URL toUrl(java.io.File file)
                          throws java.net.MalformedURLException
A replacement for a deprecated File.toURL() method.

Parameters:
file - file to convert to URL.
Returns:
URL representing the file location.
Throws:
java.net.MalformedURLException - If a protocol handler for the URL could not be found, or if some other error occurred while constructing the URL.

resolve

public static java.net.URL resolve(java.net.URL base,
                                   java.lang.String uri)
                            throws java.net.MalformedURLException
Resolves specified uri to a specified base URL.

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.

Parameters:
base - base URL to use for resulution.
uri - a relative or an absolute URI.
Returns:
URL resolved relatively to the base URL.
Throws:
java.net.MalformedURLException - if specified URI cannot be resolved.


Copyright © Copyright 2006-2009 The Scriptella Project Team.