com.tenduke.io
Class IOUtils

java.lang.Object
  extended by com.tenduke.io.IOUtils

public final class IOUtils
extends java.lang.Object

Static utility methods for IO-related functionality.


Field Summary
static int DEFAULT_BUFFER_SIZE
          Default buffer size to use for reads and writes.
 
Method Summary
static long copy(java.io.File input, java.io.OutputStream output)
          Copy file content from input file to output stream.
static long copy(java.io.InputStream input, java.io.OutputStream output)
          Copy bytes from input file to output writer.
static long copy(java.io.InputStream input, java.io.OutputStream output, int bufferSize)
          Copy bytes from input file to output writer.
static long copy(java.lang.String filePath, java.io.OutputStream output)
          Copy a file's content to output stream.
static long fileToWriter(java.io.File input, java.io.Writer output)
          Copy bytes from input to output assuming UTF-8 character set.
static long fileToWriter(java.io.File input, java.io.Writer output, int bufferSize)
          Copy characters from input to output, assuming UTF-8 character set.
static long fileToWriter(java.io.File input, java.io.Writer output, java.lang.String charsetName)
          Copy bytes from input to output.
static long fileToWriter(java.io.File input, java.io.Writer output, java.lang.String charsetName, int bufferSize)
          Copy characters from input to output.
static java.nio.ByteBuffer getByteBufferFromString(java.lang.String message, java.lang.String encoding)
          Converts a string to ByteBuffer using the specified encoding.
static java.io.InputStream getInputStream(java.net.URI uri)
           Gets an input stream associated with the given URI.
static java.io.InputStream getInputStreamSafe(java.net.URI uri)
           
static java.io.OutputStream getOutputStream(java.net.URI uri)
           Gets an output stream associated with the given URI.
static java.io.OutputStream getOutputStreamSafe(java.net.URI uri)
           
static boolean isSupportedScheme(java.net.URI uri)
          Checks if the given URI has supported scheme, which are documented in BuiltInScheme enumeration.
static java.util.Date lastModified(java.net.URI uri)
          Date when content pointed by URI was last modified.
static java.lang.String readerToString(java.io.Reader reader)
          Read all contents returned by a Reader to String.
static java.lang.String readTextStream(java.io.InputStream inputStream)
          Reads text from input stream using UTF-8 character set.
static java.net.URI resolveRelativeUri(java.lang.String base, java.lang.String relative)
           Resolves a URI relative to a base URI.
static java.net.URI resolveRelativeUri(java.net.URI base, java.net.URI relative)
           Resolves a URI relative to a base URI.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_BUFFER_SIZE

public static final int DEFAULT_BUFFER_SIZE
Default buffer size to use for reads and writes.

See Also:
Constant Field Values
Method Detail

copy

public static long copy(java.io.File input,
                        java.io.OutputStream output)
                 throws java.io.IOException
Copy file content from input file to output stream.

Parameters:
input - The input file to copy.
output - the output writer to copy to.
Returns:
Number of bytes copied from file to output.
Throws:
java.io.IOException - for any IO errors while copying input to output.

copy

public static long copy(java.io.InputStream input,
                        java.io.OutputStream output)
                 throws java.io.IOException
Copy bytes from input file to output writer.

Parameters:
input - The input file to copy.
output - the output writer to copy to.
Returns:
Count of bytes copied.
Throws:
java.io.IOException - for any IO errors while copying input to output.

copy

public static long copy(java.io.InputStream input,
                        java.io.OutputStream output,
                        int bufferSize)
                 throws java.io.IOException
Copy bytes from input file to output writer.

Parameters:
input - The input file to copy.
output - the output writer to copy to.
bufferSize - size of buffer to use for copy operation.
Returns:
Count of bytes copied.
Throws:
java.io.IOException - for any IO errors while copying input to output.

copy

public static long copy(java.lang.String filePath,
                        java.io.OutputStream output)
                 throws java.io.IOException
Copy a file's content to output stream.

Parameters:
filePath - The path to the file to copy into output.
output - The output stream to copy to.
Returns:
Number of bytes copied from file to output.
Throws:
java.io.IOException - for error opening or reading file or writing output.

fileToWriter

public static long fileToWriter(java.io.File input,
                                java.io.Writer output)
                         throws java.io.UnsupportedEncodingException
Copy bytes from input to output assuming UTF-8 character set.

Parameters:
input - The input file to copy.
output - the output writer to copy to.
Returns:
number of bytes written.
Throws:
java.io.UnsupportedEncodingException - if given character set is not supported.

fileToWriter

public static long fileToWriter(java.io.File input,
                                java.io.Writer output,
                                int bufferSize)
                         throws java.io.UnsupportedEncodingException
Copy characters from input to output, assuming UTF-8 character set.

Parameters:
input - The input file to copy.
output - the output writer to copy to.
bufferSize - Size of read buffer.
Returns:
number of bytes written.
Throws:
java.io.UnsupportedEncodingException - if given character set is not supported.

fileToWriter

public static long fileToWriter(java.io.File input,
                                java.io.Writer output,
                                java.lang.String charsetName)
                         throws java.io.UnsupportedEncodingException
Copy bytes from input to output.

Parameters:
input - The input file to copy.
output - the output writer to copy to.
charsetName - Character set to use.
Returns:
number of bytes written.
Throws:
java.io.UnsupportedEncodingException - if given character set is not supported.

fileToWriter

public static long fileToWriter(java.io.File input,
                                java.io.Writer output,
                                java.lang.String charsetName,
                                int bufferSize)
                         throws java.io.UnsupportedEncodingException
Copy characters from input to output.

Parameters:
input - The input file to copy.
output - the output writer to copy to.
charsetName - Character set to use.
bufferSize - Size of read buffer.
Returns:
number of bytes written.
Throws:
java.io.UnsupportedEncodingException - if given character set is not supported.

getByteBufferFromString

public static java.nio.ByteBuffer getByteBufferFromString(java.lang.String message,
                                                          java.lang.String encoding)
                                                   throws java.io.UnsupportedEncodingException
Converts a string to ByteBuffer using the specified encoding.

Parameters:
message - string to converted to byte buffer
encoding - character encoding
Returns:
ByteBuffer containing bytes that represent the input string in the specified encoding. Null if input string is null.
Throws:
java.io.UnsupportedEncodingException - if requested encoding is not available.

getInputStream

public static java.io.InputStream getInputStream(java.net.URI uri)
                                          throws java.io.IOException

Gets an input stream associated with the given URI. It is client's responsibility to close the stream.

If the uri has no scheme defined, or if the scheme is empty, the file scheme is assumed. The uri passed to the corresponding scheme handler is not modified to contain the scheme, however.

Parameters:
uri - URI for which the input stream is opened
Returns:
Opened input stream
Throws:
java.io.IOException - If errors occurred while trying to open the stream exist or it is not ready

getInputStreamSafe

public static java.io.InputStream getInputStreamSafe(java.net.URI uri)

getOutputStream

public static java.io.OutputStream getOutputStream(java.net.URI uri)
                                            throws java.io.IOException

Gets an output stream associated with the given URI. It is client's responsibility to close the stream.

If the uri has no scheme defined, or if the scheme is empty, the file scheme is assumed. The uri passed to the corresponding scheme handler is not modified to contain the scheme, however.

Parameters:
uri - URI for which the output stream is opened
Returns:
Opened output stream
Throws:
java.io.IOException - If errors occurred while trying to open the stream exist or it is not ready

getOutputStreamSafe

public static java.io.OutputStream getOutputStreamSafe(java.net.URI uri)

isSupportedScheme

public static boolean isSupportedScheme(java.net.URI uri)
Checks if the given URI has supported scheme, which are documented in BuiltInScheme enumeration.

Parameters:
uri - URI
Returns:
true for supported schemes, false otherwise or if a scheme does not exist in the given uri

lastModified

public static java.util.Date lastModified(java.net.URI uri)
Date when content pointed by URI was last modified.

Parameters:
uri - URI for which last modifed date is retrieved
Returns:
Last modified date, or null if the scheme handler was not found or null was returned from the scheme handler

readerToString

public static java.lang.String readerToString(java.io.Reader reader)
                                       throws java.io.IOException
Read all contents returned by a Reader to String.

Parameters:
reader - Reader from which to read.
Returns:
Contents read from the Reader as String.
Throws:
java.io.IOException - Thrown if I/O error occurs.

readTextStream

public static java.lang.String readTextStream(java.io.InputStream inputStream)
Reads text from input stream using UTF-8 character set.

Parameters:
inputStream - Input stream
Returns:
Text read from inputStream until end

resolveRelativeUri

public static java.net.URI resolveRelativeUri(java.lang.String base,
                                              java.lang.String relative)
                                       throws java.net.URISyntaxException

Resolves a URI relative to a base URI.

Parameters:
base - Base URI.
relative - URI relative to the base URI.
Returns:
Resolved URI
Throws:
java.net.URISyntaxException - Throw if one of the URIs given as parameters is invalid.

resolveRelativeUri

public static java.net.URI resolveRelativeUri(java.net.URI base,
                                              java.net.URI relative)

Resolves a URI relative to a base URI.

This method uses the resolve(URI uri) method from Java URI class except that the root URI scheme is ignored when doing the resolving. This allows using scheme and relative path in the base URI, e.g., resolving a URI conf.xml against root URI file:conf/ resolves to file:conf/conf.xml. By URI standard the file:conf/ is opaque and thus conf.xml would be the resolving result if the scheme was not ignored.

Using an absolute URI, i.e. URI with scheme, in relative returns the original relative.

Parameters:
base - Base URI.
relative - URI relative to the base URI.
Returns:
Resolved URI