ca.krasnay.scaffold.io
Class IOUtils

java.lang.Object
  extended by ca.krasnay.scaffold.io.IOUtils

public class IOUtils
extends Object

Utility methods for dealing with IO. None of these methods throw checked exceptions. Any IOException caught during their operation is wrapped in a RuntimeException and re-thrown.

Author:
John Krasnay

Field Summary
static int BYTE_BUFFER_SIZE
           
static int CHAR_BUFFER_SIZE
           
static String UTF_8
           
 
Constructor Summary
IOUtils()
           
 
Method Summary
static void close(Closeable c)
          Quietly loses the given closeable.
static void copy(Class<?> clazz, String resourceName, OutputStream out)
          Copies the given resource to an output stream.
static void copy(File in, File out)
          Copies a file.
static void copy(File in, OutputStream out)
          Copies the entire contents of a file to an output stream.
static void copy(InputStream in, File out)
          Copies the entire contents of an input stream to a file.
static void copy(InputStream in, OutputStream out)
          Copies the entire contents of an input stream to an output stream.
static void copy(InputStream in, OutputStream out, int bufferSize)
          Copies the entire contents of an input stream to an output stream.
static void copy(Reader reader, Writer writer)
          Copies the entire contents of a reader to a writer.
static void copy(Reader reader, Writer writer, int bufferSize)
          Copies the entire contents of a reader to a writer.
static void copy(Source source, Sink sink)
          Copies the contents of a source to a sink using the default buffer size.
static void copy(Source source, Sink sink, int bufferSize)
          Copies the contents of a source to a sink using a given buffer size.
static void copy(String s, OutputStream outputStream)
          Copies the given string to an output stream using UTF-8 encoding.
static byte[] toByteArray(Class<?> clazz, String resource)
          Loads the contents of the given resource into a byte array.
static byte[] toByteArray(InputStream inputStream)
          Copy the given input stream into a byte array.
static byte[] toByteArray(Source source)
          Loads the contents of the source into a byte array.
static byte[] toByteArray(URL url)
          Copy the contents of the given URL into a byte array.
static String toString(Class<?> clazz, String resource, String encoding)
          Loads the given resource as a string using the given encoding.
static String toString(File file, String encoding)
          Reads the contents of a file into a string.
static String toString(InputStream in, String encoding)
          Reads the contents of an InputStream into a string.
static String toString(Reader reader)
          Reads the contents of a Reader into a string.
static String toString(Source source, String encoding)
          Reads the contents of a Source into a string.
static String toString(URL url, String encoding)
          Copies the content of the given URL to a String.
static String toStringUtf8(Class<?> clazz, String resource)
          Loads the given resource as a string using UTF-8 encoding.
static String toStringUtf8(File file)
          Reads the contents of a file into a string using UTF-8 character encoding.
static String toStringUtf8(InputStream in)
          Reads the contents of an input stream into a string using UTF-8 character encoding.
static String toStringUtf8(URL url)
          Downloads the given URL into a string, using UTF-8 encoding.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

UTF_8

public static final String UTF_8
See Also:
Constant Field Values

BYTE_BUFFER_SIZE

public static final int BYTE_BUFFER_SIZE
See Also:
Constant Field Values

CHAR_BUFFER_SIZE

public static final int CHAR_BUFFER_SIZE
See Also:
Constant Field Values
Constructor Detail

IOUtils

public IOUtils()
Method Detail

close

public static void close(Closeable c)
Quietly loses the given closeable. Ignores null if passed, and catches and ignores any IOException thrown by the close method.

Parameters:
c - Closeable (e.g. InputStream, Reader) to be closed. May be null.

copy

public static void copy(Class<?> clazz,
                        String resourceName,
                        OutputStream out)
Copies the given resource to an output stream.

Parameters:
clazz - Class associated with the resource.
resourceName - Name of the resource relative to the given class.
out - OutputStream to receive the copy.

copy

public static void copy(File in,
                        OutputStream out)
Copies the entire contents of a file to an output stream.

Parameters:
in - File to be copied.
out - OutputStream to receive the copy.

copy

public static void copy(InputStream in,
                        File out)
Copies the entire contents of an input stream to a file.

Parameters:
in - InputStream to be copied.
out - File to receive the copy.

copy

public static void copy(File in,
                        File out)
Copies a file.

Parameters:
in - File to be copied.
out - File to receive the copy.

copy

public static void copy(InputStream in,
                        OutputStream out)
Copies the entire contents of an input stream to an output stream.

Parameters:
in - InputStream to be copied.
out - OutputStream to receive the copy.

copy

public static void copy(InputStream in,
                        OutputStream out,
                        int bufferSize)
Copies the entire contents of an input stream to an output stream.

Parameters:
in - InputStream to be copied.
out - OutputStream to receive the copy.
bufferSize - Size of buffer used for transfers.

copy

public static void copy(Source source,
                        Sink sink)
Copies the contents of a source to a sink using the default buffer size.


copy

public static void copy(Source source,
                        Sink sink,
                        int bufferSize)
Copies the contents of a source to a sink using a given buffer size.


copy

public static void copy(Reader reader,
                        Writer writer)
Copies the entire contents of a reader to a writer.

Parameters:
reader - Reader to be copied.
writer - Writer to receive the copy.

copy

public static void copy(Reader reader,
                        Writer writer,
                        int bufferSize)
Copies the entire contents of a reader to a writer.

Parameters:
reader - Reader to be copied.
writer - Writer to receive the copy.
bufferSize - Size of buffer used for transfers.

copy

public static void copy(String s,
                        OutputStream outputStream)
Copies the given string to an output stream using UTF-8 encoding.


toByteArray

public static byte[] toByteArray(InputStream inputStream)
Copy the given input stream into a byte array.

Parameters:
inputStream - Stream to copy.

toByteArray

public static byte[] toByteArray(Class<?> clazz,
                                 String resource)
Loads the contents of the given resource into a byte array.


toByteArray

public static byte[] toByteArray(Source source)
Loads the contents of the source into a byte array.


toByteArray

public static byte[] toByteArray(URL url)
Copy the contents of the given URL into a byte array.

Parameters:
url - URL to download.

toString

public static String toString(Class<?> clazz,
                              String resource,
                              String encoding)
Loads the given resource as a string using the given encoding. Returns null if the resource does not exist.

Parameters:
clazz - Class that is the base for the resource.
resource - Resource name to load.
encoding - Encoding to use.

toString

public static String toString(File file,
                              String encoding)
Reads the contents of a file into a string.

Parameters:
file - File to be loaded.
encoding - Text encoding of the file, e.g. "UTF-8".

toString

public static String toString(InputStream in,
                              String encoding)
Reads the contents of an InputStream into a string.

Parameters:
in - InputStream to read.
encoding - Text encoding of the stream, e.g. "UTF-8".

toString

public static String toString(Source source,
                              String encoding)
Reads the contents of a Source into a string.

Parameters:
source - Source to read.
encoding - Text encoding of the stream, e.g. "UTF-8".

toString

public static String toString(Reader reader)
Reads the contents of a Reader into a string.

Parameters:
reader - Reader to be read.

toString

public static String toString(URL url,
                              String encoding)
Copies the content of the given URL to a String. Note that this is simplistic. A better implementation would read the encoding from the HTTP response.

Parameters:
url - URL to download.
encoding - Encoding to use when converting bytes to characters.

toStringUtf8

public static String toStringUtf8(Class<?> clazz,
                                  String resource)
Loads the given resource as a string using UTF-8 encoding.

Parameters:
clazz - Class that is the base for the resource.
resource - Resource name to load.

toStringUtf8

public static String toStringUtf8(File file)
Reads the contents of a file into a string using UTF-8 character encoding.

Parameters:
file - File to be read.

toStringUtf8

public static String toStringUtf8(InputStream in)
Reads the contents of an input stream into a string using UTF-8 character encoding.

Parameters:
in - Input stream to be read.

toStringUtf8

public static String toStringUtf8(URL url)
Downloads the given URL into a string, using UTF-8 encoding.

Parameters:
url - URL to download.


Copyright © 2015. All rights reserved.