net.siefkes.nlstego.util
Class InOutUtils

java.lang.Object
  extended by net.siefkes.nlstego.util.InOutUtils

public final class InOutUtils
extends Object

A static class that provides utility constants and methods for I/O. No instances of this class can be created, only the static members should be used.

Version:
$Revision: 1.2 $, $Date: 2005/07/12 17:02:22 $, $Author: siefkes $
Author:
Christian Siefkes

Field Summary
static String LINE_SEPARATOR
          The line separator on the current operating system ("\n" on Unix).
static int STANDARD_BLOCK_SIZE
          The standard block size recommended for I/O.
static String STANDARD_HTTP_CHARSET
          The standard character set defined by the HTTP standard.
static String STANDARD_UNICODE_CHARSET
          The standard Unicode-compatible character set.
 
Method Summary
static byte[] compress(byte[] data)
          Compresses data using GZip for compression.
static FileContent[] listFileContents(String[] filePaths, boolean recurse)
          This method converts an array of file paths into an array of FileContent objects.
static Reader openReader(FileContent content)
          Opens a reader on a FileContent object.
static byte readExpectedByte(InputStream in)
          Reads a byte from an input stream, throwing an EOFException if the end of the stream has been reached.
static String readToString(Reader reader)
          Reads the contents of a reader into a string.
static boolean tryToClose(InputStream in)
          Convenience method for closing an input stream.
static boolean tryToClose(OutputStream out)
          Convenience method for closing an output stream.
static boolean tryToClose(Reader reader)
          Convenience method for closing a reader.
static boolean tryToClose(Writer writer)
          Convenience method for closing a writer.
static byte[] uncompress(byte[] compressedData)
          Uncompresses data using GZip.
static String userDir()
          Returns the user's current working directory.
static String userHome()
          Returns the user's home directory.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LINE_SEPARATOR

public static final String LINE_SEPARATOR
The line separator on the current operating system ("\n" on Unix).


STANDARD_BLOCK_SIZE

public static final int STANDARD_BLOCK_SIZE
The standard block size recommended for I/O.

See Also:
Constant Field Values

STANDARD_HTTP_CHARSET

public static final String STANDARD_HTTP_CHARSET
The standard character set defined by the HTTP standard.

See Also:
Constant Field Values

STANDARD_UNICODE_CHARSET

public static final String STANDARD_UNICODE_CHARSET
The standard Unicode-compatible character set.

See Also:
Constant Field Values
Method Detail

compress

public static byte[] compress(byte[] data)
                       throws IOException
Compresses data using GZip for compression.

Parameters:
data - the data to compress
Returns:
a byte array containing the compressed data
Throws:
IOException - if an I/O error occurs

listFileContents

public static FileContent[] listFileContents(String[] filePaths,
                                             boolean recurse)
                                      throws IOException
This method converts an array of file paths into an array of FileContent objects. It uses the Jakarta Commons Virtual File System (VFS) which allows accessing local files, URIs, ZIP entries and various other resources in a uniform way.

Parameters:
filePaths - the array of file paths
recurse - whether to recursively add the children of folders and other files containing nested entries
Returns:
an array of objects representing the contents of the resolved path names
Throws:
IOException - if an I/O error occurs, e.g. if one of the files does not exist

openReader

public static Reader openReader(FileContent content)
                         throws FileSystemException,
                                UnsupportedEncodingException
Opens a reader on a FileContent object. Uses the character set specified by FileContentInfo or the default character set of the current platform if no explicit charset is given.

Parameters:
content - the content to read
Returns:
a reader on the given object
Throws:
FileSystemException - if an error occurs while using the content object
UnsupportedEncodingException - if the specified charset is not available on the current platform

readToString

public static String readToString(Reader reader)
                           throws IOException
Reads the contents of a reader into a string. The reader is not closed by this method.

This method should be slightly more efficient than IOUtils.toString(java.io.Reader) since it uses an unsynchronized StringBuilder instead of a synchronized StringBuffer (hidden in a StringWriter).

Parameters:
reader - the reader to use
Returns:
the contents of the reader
Throws:
IOException - if an I/O error occurs

uncompress

public static byte[] uncompress(byte[] compressedData)
                         throws IOException
Uncompresses data using GZip.

Parameters:
compressedData - the data the uncompressed
Returns:
the uncompressed data
Throws:
IOException - if an I/O error occurs

readExpectedByte

public static byte readExpectedByte(InputStream in)
                             throws EOFException,
                                    IOException
Reads a byte from an input stream, throwing an EOFException if the end of the stream has been reached.

Parameters:
in - the stream to read from
Returns:
the read byte
Throws:
EOFException - if the end of the stream has been reached
IOException - if the end of the stream has been reached or if any other I/O error occurs

userDir

public static String userDir()
Returns the user's current working directory. This is just a shortcut for calling System.getProperty("user.dir").

Returns:
the current working directory

userHome

public static String userHome()
Returns the user's home directory. This is just a shortcut for calling System.getProperty("user.home").

Returns:
the home directory

tryToClose

public static boolean tryToClose(InputStream in)
Convenience method for closing an input stream. If the specified stream is null, this method does nothing. Any IOException thrown during closing is swallowed by this method.

Parameters:
in - the input stream to close (might be null)
Returns:
true if the stream was closed successfully, false otherwise (the stream was null or an exception was thrown during closing)

tryToClose

public static boolean tryToClose(OutputStream out)
Convenience method for closing an output stream. If the specified stream is null, this method does nothing. Any IOException thrown during closing is swallowed by this method.

Parameters:
out - the output stream to close (might be null)
Returns:
true if the stream was closed successfully, false otherwise (the stream was null or an exception was thrown during closing)

tryToClose

public static boolean tryToClose(Reader reader)
Convenience method for closing a reader. If the specified reader is null, this method does nothing. Any IOException thrown during closing is swallowed by this method.

Parameters:
reader - the reader to close (might be null)
Returns:
true if the reader was closed successfully, false otherwise (reader was null or an exception was thrown during closing)

tryToClose

public static boolean tryToClose(Writer writer)
Convenience method for closing a writer. If the specified writer is null, this method does nothing. Any IOException thrown during closing is swallowed by this method.

Parameters:
writer - the writer to close (might be null)
Returns:
true if the writer was closed successfully, false otherwise (writer was null or an exception was thrown during closing)


Copyright © 2003-2005 Christian Siefkes. All Rights Reserved.