com.sodius.mdw.core.util.io
Class IOUtils

java.lang.Object
  extended by com.sodius.mdw.core.util.io.IOUtils

public final class IOUtils
extends Object

General I/O stream manipulation utilities.


Method Summary
static void closeQuietly(InputStream input)
          Unconditionally close an InputStream.
static void closeQuietly(OutputStream output)
          Unconditionally close an OutputStream.
static void closeQuietly(Reader reader)
          Unconditionally close a Reader.
static void closeQuietly(Writer writer)
          Unconditionally close a Writer.
static void copy(File input, File output)
          Copy bytes from one File to another File.
static void copy(InputStream input, OutputStream output)
          Copy bytes from an InputStream to an OutputStream.
static void copy(Reader reader, Writer writer)
          Copy characters from a Reader to a Writer.
static URI createURI(String uri)
          Parses an URI string.
static File getAbsoluteFile(File relativeFile, File directory)
          Returns the absolute form of the specified relative file, using the specified directory to compute this absolute path.
static String getFileExtension(File file)
          Returns the extension of the specified File.
static String getFileExtension(String filename)
          Returns the extension of the specified file name.
static byte[] toByteArray(File file)
          Get the contents of a File as a byte array.
static byte[] toByteArray(InputStream input)
          Get the contents of an InputStream as a byte array.
static String toString(File file, String charsetName)
          Get the contents of a File as a String using the specified character encoding.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getFileExtension

public static String getFileExtension(File file)
Returns the extension of the specified File.

The file extension portion is defined as the string following the last period (".") character in the name. If there is no period in the name, the path has no file extension portion. If the name ends in a period, the file extension portion is the empty string.

Returns:
the file extension, null if the file has no extension.

getFileExtension

public static String getFileExtension(String filename)
Returns the extension of the specified file name.

The file extension portion is defined as the string following the last period (".") character in the name. If there is no period in the name, the path has no file extension portion. If the name ends in a period, the file extension portion is the empty string.

Returns:
the file extension, null if the file has no extension.

getAbsoluteFile

public static File getAbsoluteFile(File relativeFile,
                                   File directory)
Returns the absolute form of the specified relative file, using the specified directory to compute this absolute path.

This method does the same job as File.getAbsoluteFile(), but uses the specified directory to compute the absolute file instead of the current user directory.

Parameters:
relativeFile - the relative file.
directory - the reference directory (to use in place of the current user directory).
Returns:
the absolute file.
Since:
2.1.0

toString

public static String toString(File file,
                              String charsetName)
                       throws IOException
Get the contents of a File as a String using the specified character encoding.

Parameters:
file - the file to read from.
charsetName - the encoding to use, null means platform default.
Returns:
the file contents as a String.
Throws:
IOException - if an I/O error occurs.

toByteArray

public static byte[] toByteArray(File file)
                          throws IOException
Get the contents of a File as a byte array.

Parameters:
file - the file to read from.
Returns:
the file contents as a byte array.
Throws:
IOException - if an I/O error occurs.

toByteArray

public static byte[] toByteArray(InputStream input)
                          throws IOException
Get the contents of an InputStream as a byte array.

Parameters:
input - the stream to read from.
Returns:
the stream contents as a byte array.
Throws:
IOException - if an I/O error occurs.

copy

public static void copy(InputStream input,
                        OutputStream output)
                 throws IOException
Copy bytes from an InputStream to an OutputStream.

This method buffers the input internally, so there is no need to use a BufferedInputStream.

Throws:
IOException - if an I/O error occurs
Since:
2.1.0

copy

public static void copy(File input,
                        File output)
                 throws IOException
Copy bytes from one File to another File.

Throws:
IOException - if an I/O error occurs
IllegalArgumentException - if input or output is a directory
Since:
MDWorkbench 3.0.0

copy

public static void copy(Reader reader,
                        Writer writer)
                 throws IOException
Copy characters from a Reader to a Writer.

This method buffers the input internally, so there is no need to use a BufferedReader.

Throws:
IOException - if an I/O error occurs
Since:
MDWorkbench 3.0.0

closeQuietly

public static void closeQuietly(InputStream input)
Unconditionally close an InputStream.

Equivalent to InputStream.close(), except any exceptions will be ignored. This is typically used in finally blocks.

Parameters:
input - the InputStream to close, may be null or already closed

closeQuietly

public static void closeQuietly(OutputStream output)
Unconditionally close an OutputStream.

Equivalent to OutputStream.close(), except any exceptions will be ignored. This is typically used in finally blocks.

Parameters:
output - the OutputStream to close, may be null or already closed

closeQuietly

public static void closeQuietly(Reader reader)
Unconditionally close a Reader.

Equivalent to Reader.close(), except any exceptions will be ignored. This is typically used in finally blocks.

Parameters:
reader - the Reader to close, may be null or already closed
Since:
MDWorkbench 3.0.0

closeQuietly

public static void closeQuietly(Writer writer)
Unconditionally close a Writer.

Equivalent to Writer.close(), except any exceptions will be ignored. This is typically used in finally blocks.

Parameters:
writer - the Writer to close, may be null or already closed
Since:
MDWorkbench 3.0.0

createURI

public static URI createURI(String uri)
Parses an URI string. This method first checks if the path resolves to an existing file (in terms of File.exists()). Otherwise URI.createURI(String) is called.

Since:
MDWorkbench 3.0.0
See Also:
URI#createURI(String)