org.jcoderz.commons.util
Class IoUtil

java.lang.Object
  extended by org.jcoderz.commons.util.IoUtil

public final class IoUtil
extends Object

Collects some I/O utility functions.

Author:
Michael Griffel, Andreas Mandel

Method Summary
static void close(Channel channel)
          Closes the channel (safe).
static void close(InputStream in)
          Closes the input stream (safe).
static void close(OutputStream out)
          Closes the output stream (safe).
static void close(RandomAccessFile randomAccessFile)
          Closes the RandomAccessFile (safe).
static void close(Reader reader)
          Closes the reader (safe).
static void close(Socket socket)
          Closes the socket (safe).
static void close(Writer writer)
          Closes the writer (safe).
static void copy(File src, File dest)
          Copies file src to file dest.
static void copy(InputStream in, OutputStream out)
          Reads all data from in and copies it to the out stream.
static byte[] readFully(InputStream in)
          Reads all bytes from the input stream in.
static byte[] readFully(InputStream in, int expectedLength)
          Reads expectedLength bytes from the input stream in.
static String readFully(Reader in)
          Reads all characters from the reader in.
static String readFullyNormalizeNewLine(Reader in)
          Reads all characters from the reader in and normalizes newlines to single '\n'.
static void skip(InputStream in, int bytes)
          Ensures that the given number of bytes are skipped from the given input stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

close

public static void close(InputStream in)
Closes the input stream (safe). This method tries to close the given input stream and if an IOException occurs a message with the level Level.FINE is logged. It's safe to pass a null reference for the argument.

Parameters:
in - the input stream that should be closed.

close

public static void close(OutputStream out)
Closes the output stream (safe). This method tries to close the given output stream and if an IOException occurs a message with the level Level.FINE is logged. It's safe to pass a null reference for the argument.

Parameters:
out - the output stream that should be closed.

close

public static void close(Reader reader)
Closes the reader (safe). This method tries to close the given reader and if an IOException occurs a message with the level Level.FINE is logged. It's safe to pass a null reference for the argument.

Parameters:
reader - the reader that should be closed.

close

public static void close(RandomAccessFile randomAccessFile)
Closes the RandomAccessFile (safe). This method tries to close the given RandomAccessFile and if an IOException occurs a message with the level Level.FINE is logged. It's safe to pass a null reference for the argument.

Parameters:
randomAccessFile - the randomAccessFile that should be closed.

close

public static void close(Writer writer)
Closes the writer (safe). This method tries to close the given writer and if an IOException occurs a message with the level Level.FINE is logged. It's safe to pass a null reference for the argument.

Parameters:
writer - the writer that should be closed.

close

public static void close(Channel channel)
Closes the channel (safe). This method tries to close the given channel and if an IOException occurs a message with the level Level.FINE is logged. It's safe to pass a null reference for the argument.

Parameters:
channel - the channel that should be closed.

close

public static void close(Socket socket)
Closes the socket (safe). This method tries to close the given socket and if an IOException occurs a message with the level Level.FINE is logged. It's safe to pass a null reference for the argument.

Parameters:
socket - the socket that should be closed.

readFully

public static byte[] readFully(InputStream in,
                               int expectedLength)
                        throws IOException
Reads expectedLength bytes from the input stream in.

Parameters:
in - the input stream to read from.
expectedLength - the expected size.
Returns:
an byte array with expectedLength bytes.
Throws:
IOException - in an I/O error occurs or if the read size is not the expected size.

readFully

public static byte[] readFully(InputStream in)
                        throws IOException
Reads all bytes from the input stream in.

Parameters:
in - the input stream to read from.
Returns:
an byte array with read bytes.
Throws:
IOException - in an I/O error occurs.

readFully

public static String readFully(Reader in)
                        throws IOException
Reads all characters from the reader in.

Parameters:
in - the Reader to read from.
Returns:
an String containing the read data
Throws:
IOException - in an I/O error occurs.

readFullyNormalizeNewLine

public static String readFullyNormalizeNewLine(Reader in)
                                        throws IOException
Reads all characters from the reader in and normalizes newlines to single '\n'.

Parameters:
in - the Reader to read from.
Returns:
an String containing the read data
Throws:
IOException - in an I/O error occurs.

copy

public static void copy(InputStream in,
                        OutputStream out)
                 throws IOException
Reads all data from in and copies it to the out stream.

Parameters:
in - the stream to read.
out - the stream to write.
Throws:
IOException - if an I/O issue occurs.

copy

public static void copy(File src,
                        File dest)
                 throws IOException
Copies file src to file dest.

Parameters:
src - the source file.
dest - the destination file.
Throws:
IOException - if an I/O issue occurs.

skip

public static void skip(InputStream in,
                        int bytes)
                 throws IOException
Ensures that the given number of bytes are skipped from the given input stream.

Parameters:
in - the input stream.
bytes - the number of bytes to skip.
Throws:
IOException - if the stream does not support seek, or if some other I/O error occurs.
See Also:
InputStream.skip(long)


Copyright 2007 The jCoderZ Project.