org.ckkloverdos.io
Class IOUtil

java.lang.Object
  extended by org.ckkloverdos.io.IOUtil

public class IOUtil
extends java.lang.Object

IO-related utility methods.

Author:
Christos KK Loverdos

Constructor Summary
IOUtil()
           
 
Method Summary
static void copy(java.io.InputStream is, java.io.OutputStream os)
          Copies the bytes from is to os.
static java.lang.String getFileExtension(java.io.File file)
          Returns the filename extension of the given file.
static java.lang.String getFileExtension(java.lang.String name)
          Returns the extension of the given filename.
static java.lang.String getFileLine(java.io.File f, int line)
          Reads the line with the given line number.
static java.lang.String getFileLine(java.io.File f, int line, java.lang.String encoding)
          Reads the line with the given line number.
static byte[] readBytes(java.io.InputStream is)
          Given an stream, we read it exhaustively and return the resulting byte array.
static byte[] readBytes(java.io.InputStream is, boolean close)
          Given an stream, we read it exhaustively and return the resulting byte array.
static IReadLine readLineFromReader(java.io.BufferedReader br)
          Creates a IReadLine implementation from the specified buffered reader.
static IReadLine readLineFromReader(java.io.Reader r)
          Creates a IReadLine implementation from the specified reader.
static java.lang.String readString(java.io.File file)
          The file is read as a string, using StringUtil.UTF8.
static java.lang.String readString(java.io.File file, java.lang.String encoding)
          The file is read as a string, using the provided character encoding.
static java.lang.String readString(java.io.InputStream is)
          Given a stream, we read it exhaustively and return the characters contained in the stream as a string.
static java.lang.String readString(java.io.InputStream is, boolean close)
          Given a stream, we read it exhaustively and return the characters contained in the stream as a string.
static java.lang.String readString(java.io.InputStream is, java.lang.String encoding)
          Given an stream, we read it exhaustively and return the characters contained in the stream as a string.
static java.lang.String readString(java.io.InputStream is, java.lang.String encoding, boolean close)
          Given an stream, we read it exhaustively and return the characters contained in the stream as a string.
static void safeClose(java.io.InputStream is)
          Safely closes the input stream, even if it is null, without throwing any exceptions.
static void safeClose(java.io.Reader r)
          Safely closes the reader, even if it is null, without throwing any exceptions.
static java.io.InputStream safeOpenStream(java.net.URL url)
          Safely opens the stream from url.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

IOUtil

public IOUtil()
Method Detail

readBytes

public static byte[] readBytes(java.io.InputStream is,
                               boolean close)
                        throws java.io.IOException
Given an stream, we read it exhaustively and return the resulting byte array. The stream is closed after reading iff close is true.

Parameters:
is - The stream
Returns:
an array of bytes as the contents of the stream
Throws:
java.io.IOException

readBytes

public static byte[] readBytes(java.io.InputStream is)
                        throws java.io.IOException
Given an stream, we read it exhaustively and return the resulting byte array.

Parameters:
is - The stream
Returns:
an array of bytes as the contents of the stream
Throws:
java.io.IOException

readString

public static java.lang.String readString(java.io.InputStream is,
                                          boolean close)
                                   throws java.io.IOException
Given a stream, we read it exhaustively and return the characters contained in the stream as a string. The StringUtil.UTF8 character encoding is used for the translation of bytes to characters. The stream is closed after reading iff close is true.

Parameters:
is - The channel stream
Returns:
the string contents of the stream
Throws:
java.io.IOException

readString

public static java.lang.String readString(java.io.InputStream is)
                                   throws java.io.IOException
Given a stream, we read it exhaustively and return the characters contained in the stream as a string.

The StringUtil.UTF8 character encoding is used for the translation of bytes to characters.

Parameters:
is - The channel stream
Returns:
the string contents of the stream
Throws:
java.io.IOException

readString

public static java.lang.String readString(java.io.InputStream is,
                                          java.lang.String encoding,
                                          boolean close)
                                   throws java.io.IOException
Given an stream, we read it exhaustively and return the characters contained in the stream as a string. We use encoding for the translation of bytes to characters. The stream is closed after reading iff close is true.

Parameters:
is - The channel stream
encoding - The requested character encoding. Not null
Returns:
the string contents of the channel stream
Throws:
java.io.IOException

readString

public static java.lang.String readString(java.io.InputStream is,
                                          java.lang.String encoding)
                                   throws java.io.IOException
Given an stream, we read it exhaustively and return the characters contained in the stream as a string.

We use encoding for the translation of bytes to characters.

Parameters:
is - The channel stream
encoding - The requested character encoding. Not null
Returns:
the string contents of the channel stream
Throws:
java.io.IOException

copy

public static void copy(java.io.InputStream is,
                        java.io.OutputStream os)
                 throws java.io.IOException
Copies the bytes from is to os.

Parameters:
is -
os -
Throws:
java.io.IOException

readString

public static java.lang.String readString(java.io.File file)
                                   throws java.io.IOException
The file is read as a string, using StringUtil.UTF8.

Parameters:
file -
Throws:
java.io.IOException

readString

public static java.lang.String readString(java.io.File file,
                                          java.lang.String encoding)
                                   throws java.io.IOException
The file is read as a string, using the provided character encoding.

Parameters:
file -
Throws:
java.io.IOException

safeOpenStream

public static java.io.InputStream safeOpenStream(java.net.URL url)
Safely opens the stream from url. No exceptions are thrown. Returns null on error.

Parameters:
url -

safeClose

public static void safeClose(java.io.InputStream is)
Safely closes the input stream, even if it is null, without throwing any exceptions.

Parameters:
is -

getFileLine

public static java.lang.String getFileLine(java.io.File f,
                                           int line)
Reads the line with the given line number.

Note: Lines start from 1.

Parameters:
f -
line -

getFileLine

public static java.lang.String getFileLine(java.io.File f,
                                           int line,
                                           java.lang.String encoding)
Reads the line with the given line number. If encoding is null, StringUtil.UTF8 is used.

Note: Lines start from 1.

Parameters:
f -
line -
encoding -

safeClose

public static void safeClose(java.io.Reader r)
Safely closes the reader, even if it is null, without throwing any exceptions.

Parameters:
r -

readLineFromReader

public static IReadLine readLineFromReader(java.io.BufferedReader br)
Creates a IReadLine implementation from the specified buffered reader.

Parameters:
br -

readLineFromReader

public static IReadLine readLineFromReader(java.io.Reader r)
Creates a IReadLine implementation from the specified reader.

Parameters:
r -

getFileExtension

public static java.lang.String getFileExtension(java.io.File file)
                                         throws java.io.IOException
Returns the filename extension of the given file.

Parameters:
file -
Throws:
java.io.IOException

getFileExtension

public static java.lang.String getFileExtension(java.lang.String name)
                                         throws java.io.IOException
Returns the extension of the given filename.

Parameters:
name -
Throws:
java.io.IOException


Copyright © 1999-2007 Christos KK Loverdos. All Rights Reserved.